Results 1 to 4 of 4

Thread: What is wrong with the code

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    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 .

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    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
  •  



Click Here to Expand Forum to Full Width