Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi all ,

I am stuck in some issue while my practice in database programming.

Below is my purpose :
1) I have member id and amount with me.
2) First throught C# i ran one query "SELECT * FROM Table1 WHERE ......"
3) Then if record found i want to update the record
4) If record not found then i want to insert the record in the table.


Please refer below code :

C#
public static bool SaveMembersCollection(Int16 Id, double amt, DateTime dtofcollection)
       {
           OleDbCommand Com = new OleDbCommand();

           string sql;
           sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), " AND CollectionDate='", dtofcollection.ToString(), "'");
           Com.Connection = Conn;
           //OleDbParameter Param = new OleDbParameter("@titel", "");
           //Com.Parameters.Add(Param);
           //Param = new OleDbParameter("@locatie", c);
           //Com.Parameters.Add(Param);
           Com.ExecuteNonQuery();



           Conn.Close();


           return false;
       }
Posted
Updated 19-Apr-11 23:45pm
v2

Well, right off, I noticed that you're not passing your sql or the connection object to the cmd object.

Com.ExecuteNonQuery(sql, Conn);
 
Share this answer
 
Comments
RDBurmon 12-Jan-11 12:23pm    
Thanks , give me any solutions to resolve my problem.
Your missing a single quote mark ' just before the AND in your SELECT statement:
sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), " AND ...

should be:
sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), "' AND ...
 
Share this answer
 
v2
Comments
fjdiewornncalwe 12-Jan-11 14:33pm    
+5... Your post wasn't showing when I posted my answer... I should really hit refresh on a stale screen before I do something.
[Edit] I didn't see the previously posted answer when I posted this.... :doh: [\Edit]
The first thing I noticed right off is that where you write
sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), " AND CollectionDate='", dtofcollection.ToString(), "'");


you forgot to put in the closing single quote after Id.ToString().

sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), "' AND ...


For the insert part see John Simmons answer.
 
Share this answer
 
v2
Comments
Espen Harlinn 16-Jan-11 9:03am    
5+ Nicely spotted
Do you need help getting it started dealing with SQL, or do you need help with the logic?

You basically laid out the logic, so I don't know where you are stuck. If you need help with the query syntax I would use Google. Your steps 2-4 are the logic to use

Query for results
if results exist
update
else
insert

What exactly is it that you need help with?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900