none
C# - TableAdapter.INSERT -- SIMPLE question RRS feed

  • Question

  • I am trying to learn the basics of SQL and C# and want to push a button and insert some data into a single Table in my dbase.

     

    So I set it all up and was going to use this command   "this.defaultTableAdapter.Insert"  -- the problem is that I set my table up to have 3 columns ID, Name, and Number.  The ID column is a Primary Key - Identifier that numbers automatically.

     

    So I tried to JUST put in the name and number in the above command  --  this.defaultTableAdapter.Insert ("Todd", 123);

     

    But got an error because it said that I had to insert 3 arguments... well I do not want to insert the ID column because it is an automated identifier.... 

     

    So you guys probably know what I am dealing with by now --- can someone help?

     

    Thanks much!

     

    ****Why did this get moved from the C# forum when this relates to how C# deals with bound controls and updating dbases?

    Friday, August 24, 2007 3:55 PM

Answers

  • I tried to reproduce your scenario.  I created a database with a table containing a primary key (Id) which is an identity that auto-increments, a name field and a number field.  I added a dataset which references the table and tableAdapter.  On my form, I added a dataGridView control bound to the dataset and a button to insert a record.

    In the button click event handler I wrote:  table1TableAdapter.Insert( and noted that Intellesense was only expecting two parameters: a string name and an int number. 

    Here is the complete button click event handler which when executed, displays the new data in the dataGridView without error.

            private void button1_Click(object sender, EventArgs e)

            {

                table1TableAdapter.Insert("Mack", 45);          

                this.table1TableAdapter.Fill(this.database1DataSet.Table1);

            }

    As to your problem, I wonder if it is possible that your tableAdapter was created before you defined the database table with the primary auto-increment identity field?  Either the tableAdapter that you have created is not referencing the correct table schema or the table definition (schema) is not what you think it should be.

     

    Friday, August 24, 2007 4:43 PM

All replies

  • I tried to reproduce your scenario.  I created a database with a table containing a primary key (Id) which is an identity that auto-increments, a name field and a number field.  I added a dataset which references the table and tableAdapter.  On my form, I added a dataGridView control bound to the dataset and a button to insert a record.

    In the button click event handler I wrote:  table1TableAdapter.Insert( and noted that Intellesense was only expecting two parameters: a string name and an int number. 

    Here is the complete button click event handler which when executed, displays the new data in the dataGridView without error.

            private void button1_Click(object sender, EventArgs e)

            {

                table1TableAdapter.Insert("Mack", 45);          

                this.table1TableAdapter.Fill(this.database1DataSet.Table1);

            }

    As to your problem, I wonder if it is possible that your tableAdapter was created before you defined the database table with the primary auto-increment identity field?  Either the tableAdapter that you have created is not referencing the correct table schema or the table definition (schema) is not what you think it should be.

     

    Friday, August 24, 2007 4:43 PM
  • THANKS SO MUCH for taking your time first of all!

     

    I think what you did different was add a Gridview.  I was thinking about that as well because when you add a gridview you can remove the ID column with no problems... SO I think the GridView adds the code needed for this problem.

     

    IF YOU CAN, can you try this WITHOUT adding a Gridview?  If not, maybe I will just add a GridView then remove it and see if it still works.

     

    Thanks much!!!

    Friday, August 24, 2007 5:12 PM
  • Wierd.... as it was, I drug a GridView out (did NOT get a bindingNavigator???) -- and could NOT leave the ID field blank.

     

    I was waiting for it to fill in but was getting a NON null exception when I tried to move to the next row.

     

    I deleted everything.   Drug the table back out onto the dataset designer surface.  Went to the Form designer and drug out a grid view then ran it.  At this point - every time I entered a new row the ID field was filled in first automatically.

     

    I went to the button and YES there was ONLY the string Name and int Number option and NO option or need to fill in the ID column.

     

    ** I even went back and deleted the Navigator and the gridview and checked the button stuff and it was still ok - so Im fixed!!!  thanks much!

     

    It must be one of two things.... either like you said my dataset was messed up to begin with AND/OR the gridview inserts the proper code to fix the button issue.

     

    THANKS!

     

    Friday, August 24, 2007 5:27 PM