|
-
Oct 19th, 2005, 01:38 AM
#1
Thread Starter
Member
What is wrong with the code
Hi all,
i m new to c#, can anybody please tell me what is wrong with this code why it is throwing error, there is only one records in dataset, and i m trying to update the table with the values in the data set
Error Message is : Command contains unrecognised phrase/key words
Code:
try
{
foreach( DataRow dtrow in ds.Tables[0].Rows)
{
string mmicr,msortcode,maccount,msrl,mserial,mamount ;
mmicr = dtrow["micr"].ToString().PadLeft(6,'0') ;
maccount = dtrow["account"].ToString().PadLeft(6,'0') ;
msortcode = dtrow["sortcode"].ToString() ;
msrl = dtrow["srl"].ToString() ;
mamount = dtrow["amount"].ToString().PadLeft(13,'0') ;
mserial = dtrow["serial"].ToString() ;
OleDbDataAdapter danew = new OleDbDataAdapter(
"UPDATE " + dbfname + " set micr = '" + mmicr + "',account = '" + maccount + "',sortcode = '" + msortcode + "',amount = '" + mamount + "',srl = '" + msrl + "' where serial == '" + mserial , Dconn);
DataTable dtnew = new DataTable() ;
danew.Fill(dtnew) ;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message) ;
}
Last edited by newmember; Oct 19th, 2005 at 01:44 AM.
-
Oct 19th, 2005, 01:55 AM
#2
Re: What is wrong with the code
You're creating a DataAdapter and passing it the SQL for an UPDATE command. No, no, no! When creating a DataAdapter in that manner you must pass it the SQL for a query, i.e. a SELECT command. The SelectCommand property, which is created from the SQL that you pass, is used to Fill a DataTable and the DeleteCommand, InsertCommand and UpdateCommand are used to commit changes back to the database when you call Update. You need to read up in the help or on MSDN about the OleDbDataAdapter and OleDbCommand classes, and perhaps take a look at some of the tutorials and/or 101 samples from my signature.
-
Oct 19th, 2005, 02:31 AM
#3
Thread Starter
Member
Re: What is wrong with the code
Thanks for your reply,
i am on the wrong way, i think i have to read a lot . but please wait for my next reply .
-
Oct 19th, 2005, 02:46 AM
#4
Thread Starter
Member
Re: What is wrong with the code
hi jmcilhinney,
i have a doubt, the way i tried to update the table is wrong as you said and i must agree with that ,. but the fact is , the coding is updating the table (syntax error is there in query string) , it is a free table of foxpro . can you please say how can this happen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|