Results 1 to 21 of 21

Thread: What is wrong with this code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    What is wrong with this code

    using System;
    using System.Data;
    using System.Data.OleDb;

    namespace practice
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    OleDbConnection sqlcn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;data source =Path of Database");
    sqlcn.Open();
    OleDbDataAdapter sqlda=new OleDbDataAdapter("SELECT Name FROM TABLE1",sqlcn);
    OleDbCommandBuilder cmdbl=new OleDbCommandBuilder(sqlda);
    DataSet ds=new DataSet();
    sqlda.Fill(ds,"Table1");
    Console.WriteLine(ds.Tables["Table1"].Rows[0]["Name"]);
    ds.Tables["Table1"].Rows[0]["Name"]="Haider";
    sqlda.Update(ds,"Table1");
    Console.WriteLine(ds.Tables["Table1"].Rows[0]["name"]);
    sqlcn.Close();
    Console.ReadLine();

    }
    }
    }

    I got exception that Update is not possible since of a query. The exception is thrown at line sqlda.Update(ds,"Table1")

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: What is wrong with this code

    Please check the inner exception also, what it is saying

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    I am giving you a screen shot, zoom it for your ease, Thanks for taking time for replying
    Plz check the attachment
    Attached Files Attached Files

  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: What is wrong with this code

    You are not telling the DataAdapter what the UPDATE string is. You must assign dataAdapter.updatecommand = cmdbl.GetUpdateCommand();. After this then you can do dataAdapter.Update();.
    Currently Using: VS 2005 Professional

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

    Re: What is wrong with this code

    Quote Originally Posted by tacoman667
    You are not telling the DataAdapter what the UPDATE string is. You must assign dataAdapter.updatecommand = cmdbl.GetUpdateCommand();. After this then you can do dataAdapter.Update();.
    I've seen so many people do that but it's completely unnecessary. The ONLY reason to call GetUpdateCommand is if you want to make changes to the Command object it returns. This:
    Code:
    dataAdapter.updatecommand = cmdbl.GetUpdateCommand();
    is of no use whatsoever.

    I'm quite sure that you didn't get an error message that said:
    Update is not possible since of a query
    Please post the actual error message because that is not really very close to what it would have said.

    The problem is that your query returns no primary key information. A CommandBuilder cannot generate SQL code unless it can uniquely identify each record, which it cannot do without a primary key. First of all you must set the MissingSchemaAction property to AddWithKey. That still won't help though unless your query returns the primary key column(s). Is the Name column your primary key? I'm guessing, and hoping, that it isn't. If you don't include the primary key in your query then you can't use a CommandBuilder, plain and simple.
    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

  6. #6
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: What is wrong with this code

    Silly me. They aren't pulling the Primary Key in order for the commandbuilder object to create the WHERE statement. I am still intermediate in the SQL language.

    JM, assuming this wasn't giving an error and was done properly, what should replace the "dataAdapter.updatecommand = cmdbl.GetUpdateCommand();" statement?
    Currently Using: VS 2005 Professional

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

    Re: What is wrong with this code

    Quote Originally Posted by tacoman667
    JM, assuming this wasn't giving an error and was done properly, what should replace the "dataAdapter.updatecommand = cmdbl.GetUpdateCommand();" statement?
    Nothing. Like I said, you don't call GetUpdateCommand, GetInsertCommand or GetDeleteCommand at all unless you specifically want to edit the Command objects they return. The CommandBuilder implicitly creates whatever SQL statements are required on demand. Something like this will work as is:
    Code:
    OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM MyTable", con);
    OleDbCommandBuilder bld = new OleDbCommandBuilder(adp);
    
    adp.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    adp.Fill(myDataTable);
    
    // Make changes to data here.
    
    adp.Update(myDataTable);
    When you call Update the CommandBuilder implicitly generates the required Commands and temporarily assigns them to the DataAdapter's properties. If you wanted to edit those Commands, like if you needed to enlist them in a transaction, only then would you need to generate them explicitly.
    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    Quote Originally Posted by jmcilhinney
    I've seen so many people do that but it's completely unnecessary. The ONLY reason to call GetUpdateCommand is if you want to make changes to the Command object it returns. This:
    Code:
    dataAdapter.updatecommand = cmdbl.GetUpdateCommand();
    is of no use whatsoever.

    I'm quite sure that you didn't get an error message that said:Please post the actual error message because that is not really very close to what it would have said.

    The problem is that your query returns no primary key information. A CommandBuilder cannot generate SQL code unless it can uniquely identify each record, which it cannot do without a primary key. First of all you must set the MissingSchemaAction property to AddWithKey. That still won't help though unless your query returns the primary key column(s). Is the Name column your primary key? I'm guessing, and hoping, that it isn't. If you don't include the primary key in your query then you can't use a CommandBuilder, plain and simple.
    I have given an screen shot of the exception in my previous message plz check that

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

    Re: What is wrong with this code

    You've attached a ZIP file. There's no reason for us to download and extract a ZIP to see an image. Just attach a JPG and it will be displayed right there in your post. Make it easy for us to help you and you're more likely to get help.

    Besides, I'm almost 100% sure that the information I have given already will solve your problem. Make sure that your query returns the PK or a CommandBuilder simply cannot work.
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    No still not working.
    I have tried all your methods
    Another thing that I was told about Data Adapter is it does not change a cell value, You must overwrite all the values in a row
    If it is true it is v bad for using a data Adapter

    I m also posting the exception NOT In zipped format but a whole image.
    Attached Images Attached Images  
    Last edited by Abbas Haider; Mar 8th, 2007 at 12:50 PM.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: What is wrong with this code

    You are selecting a single column from your table. And it is not a primary key. The adapter does not know how to reference the table and find out which row it needs to update. In your select statement, get the primary key along with [Name].

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

    Re: What is wrong with this code

    Quote Originally Posted by Abbas Haider
    I have tried all your methods
    In fact you've completely ignored my advice from post #5:
    Quote Originally Posted by jmcilhinney
    The problem is that your query returns no primary key information. A CommandBuilder cannot generate SQL code unless it can uniquely identify each record, which it cannot do without a primary key. First of all you must set the MissingSchemaAction property to AddWithKey. That still won't help though unless your query returns the primary key column(s). Is the Name column your primary key? I'm guessing, and hoping, that it isn't. If you don't include the primary key in your query then you can't use a CommandBuilder, plain and simple.
    Your problem is still exactly the same, as mendhak says. Your query is not returning any primary key information. In my first post I said that you need to set the MissingSchemaAction property and add the PK column to the query. You have done neither, thus you still have the same issue.
    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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    Any one please give a sample Program of C# Console which insert values in a record, by taking input from user i.e System.Console.read(); which pass it to dataset and ultimately database table.
    The program should be running as I have made a program but it is giving me exceptions and not writng to the DB
    Thanks

  14. #14
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: What is wrong with this code

    [Edit]
    Whoops, nm
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

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

    Re: What is wrong with this code

    You've already been told how to solve your problem and you've ignored the advice provided. People are trying to help you and you're refusing to accept that help. Why have you not done what we've already told you? Do you not understand what we're telling yo to do? If so then say so. If people try to help and you ignore their advice then they're unlikely to try to help to many more times.
    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

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    look
    I have read all your statements, but haven't you considered a situation in which you have no primary key as you stated that my columns aren't returning no primary key. Now if you have to insert a simple record in a simple with no primary key NECCESSARY table, how would you do it? I am just asking that and you are blaming me that I am ignoring your guide

    Even I have used alternative method of Command Builder to do my job but I am unable to do it

    Why cant you just paste a simple code here so I may match it with my code,
    I even gave a screen shot of the problem.

    Lastly I am saying again that I am NOT USING ANY PRIMARY KEY FIELD IN MY TABLE.
    Last edited by Abbas Haider; Oct 3rd, 2007 at 03:52 AM.

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

    Re: What is wrong with this code

    It took you until post #16 to tell us that your table doesn't actually have a primary key. Also, the screen shot you posted shows you trying to update a record, not insert one. Insertion should work fine with a CommandBuilder even without a primary key because insertion doesn't use a WHERE clause. As long as your query involves only one table then the InsertCommand can be generated.

    Now, in that screen shot you are editing an existing row in the table, i.e. you are performing an UPDATE. If it's your intention to perform an INSERT then you have to create a new row and add it to the table, not edit an existing row.
    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

  18. #18
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: What is wrong with this code

    Quote Originally Posted by MSDN
    DataAdapter.Update() : Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named "Table."
    In the screen-shot you provided, you are passing a Select query, so exactly what are you trying to update or insert???

    You passed SELECT query to DataAdpater,

    filled DataSet,

    then add 1 row to that DataSet using user input,

    but when you use the Update(), you are not telling the DataAdapter that you are INSERTing or UPDATEing your database now!!!
    Show Appreciation. Rate Posts.

  19. #19
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: What is wrong with this code

    Quote Originally Posted by Harsh Gupta
    In the screen-shot you provided, you are passing a Select query, so exactly what are you trying to update or insert???

    You passed SELECT query to DataAdpater,

    filled DataSet,

    then add 1 row to that DataSet using user input,

    but when you use the Update(), you are not telling the DataAdapter that you are INSERTing or UPDATEing your database now!!!
    Actually the Inserts and Updates can be generated automatically by just passing a select statement to the DataAdapter and using the OleDbCommandBuilder

    Now if the idiot would just put a primary key on the table.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: What is wrong with this code

    Actually the Inserts and Updates can be generated automatically by just passing a select statement to the DataAdapter and using the OleDbCommandBuilder

    Now if the idiot would just put a primary key on the table.

    Oh how nice of you to poke in here and comment.
    Well what can I say to the people like you? it is of no use for me to tell that I have tried all thse methods AND THEY ARE NOT AUTOMATICALLY WORKING.

    Please if you think that I am idiot DO NOT BOTHER TO WRITE HERE AND ANY OF MY THREADS
    Last edited by Abbas Haider; Oct 19th, 2007 at 12:14 PM.

  21. #21
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: What is wrong with this code

    Quote Originally Posted by Abbas Haider
    Oh how nice of you to poke in here and comment.
    Well what can I say to the people like you? it is of no use for me to tell that I have tried all thse methods AND THEY ARE NOT AUTOMATICALLY WORKING.

    Please if you think that I am idiot DO NOT BOTHER TO WRITE HERE AND ANY OF MY THREADS
    Did you ever put a primary key on the table or are you still ignoring the suggestion?

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