Results 1 to 10 of 10

Thread: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity Specification

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Resolved [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity Specification

    I created a simple table with an ID field and text string field. The ID field is Primary Key integer and set the Identity specification so as to auto increment the ID. I then used the Data Source Configuration wizard to create a strongly typed DataSet in the project. But now I can't figure out how to add a row to the table. If I don't include a value in the ID column I get a null exception. How do I do this properly?
    vb.net Code:
    1. Using taTestMe As New TestMeDataSetTableAdapters.ErrorsTableAdapter, dtErrors As New TestMeDataSet.ErrorsDataTable
    2.     Dim drNew As TestMeDataSet.ErrorsRow
    3.     drNew = dtErrors.NewErrorsRow
    4.     'drNew.ID = 1
    5.     drNew.Message = "Test 3"
    6.     dtErrors.Rows.Add(drNew)
    7.     taTestMe.Update(dtErrors)
    8. End Using
    Thank you for taking the time to read my post.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Add row to SQL Server table with Primary Key ID field using Identity Specificatio

    My guess is that your not working with the database that you think you are. If you added the database to your project and the IDE is in Debug mode, check the database in the "bin\debug" folder. Because if the "ID" field is an Identity field and autonumber, then your code should work fine.

  3. #3

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Add row to SQL Server table with Primary Key ID field using Identity Specificatio

    Roger. I'll start over from scratch. Am I creating the DataSet correctly by using that wizard?

  4. #4

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Add row to SQL Server table with Primary Key ID field using Identity Specificatio

    It wasn't that I wasn't connecting to a different database but that was the clue I needed to solve the problem. The initial table design had no identity specification and I created the DataSet using the wizard. After I change it to auto-increment I ran the wizard again but failed to create a new string. In my old DataSet I can see the AutoIncrement was false. This time I had it create a new connection string and my code works now. Thank you much for heading me in the right direction.

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    When using datasets created by the Wizard from a database, when you modify the database, sometimes its just best to delete the affected datatable from the dataset and then run the wizard and add it back. There are a few bugs with the wizard and sometimes it doesn't update the dataset correctly.

  6. #6

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    That's great advice. Thank you. Is it better not to use the Wizard? Is there a manual way that is better?

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    Quote Originally Posted by cory_jackson View Post
    That's great advice. Thank you. Is it better not to use the Wizard? Is there a manual way that is better?
    I use the wizard all the time but you have to make sure if you modify the database that the dataset gets updated. You can manually create a type dataset, just Project->Add new item->Dataset.

  8. #8

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    I have created DataSets that way before when I didn't rely on ouside data. I imagine then all one needs to do is create a DataAdapter. What is the easiest way to do that?

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    Lots of good information Here
    Last edited by wes4dbt; Jul 26th, 2015 at 08:11 PM.

  10. #10

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Add row to SQL Server table with Primary Key ID field using Identity S

    Oh nice. Thanks!

Tags for this Thread

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