Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Programmatically add a row to a dataset table with IDENTITY column
 

Programmatically add a row to a dataset table with IDENTITY column

Hi,

I want to programmatically add a row to a datatable in my dataset.  I have a primary key that is an IDENTITY column in my database.  How do I do this?  If I set the column value to DBNull.Value I get an error about it not accepting nulls.  If I set it to 'nothing' it tells me to use dbnull.  If I use "" I get a cast problem from string to integer.

Sample Code:
            Dim AddRow As DataRow
            AddRow = MyDS.Tables("tools").NewRow
            AddRow(0) = DBNull.Value '***problem line***
            AddRow(1) = "Desc"
            MyDS.Tables("tools").Rows.Add(AddRow)

I'm using VS .Net 2003.  Thanks in advance.
jds1982  Wednesday, January 04, 2006 7:31 PM
What I ended up doing was inserting a negative value and decrementing it as values where added.  The reason I had to do that was because I had a child table that would need to be updated as well with the same ID values as the parent.  That way I could insert a -1 for both ID's in the parent and child tables and when the queries were sent to the SQL server SCOPE_IDENTITY() would return the proper values for parent and then children.  I basically did what this said to: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/manidcrisis.asp
jds1982  Tuesday, January 10, 2006 11:53 PM
Dont set it at all
CommonGenius.com  Thursday, January 05, 2006 4:07 AM
What I ended up doing was inserting a negative value and decrementing it as values where added.  The reason I had to do that was because I had a child table that would need to be updated as well with the same ID values as the parent.  That way I could insert a -1 for both ID's in the parent and child tables and when the queries were sent to the SQL server SCOPE_IDENTITY() would return the proper values for parent and then children.  I basically did what this said to: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/manidcrisis.asp
jds1982  Tuesday, January 10, 2006 11:53 PM

You can use google to search for other answers

Custom Search

More Threads

• ENTER should behave like TAB in DataGridView
• Datagrid can´t load scrollbar after get data from webservice using Thread at Form.Shown
• Can't if (e.Control.TextChanged != null) , throws exception
• Using a form field to control the datasource of a combobox on a datagridview
• System.NullReferenceException: Object reference not set to an instance of an object
• RowValidating and the origional values in a DGV row
• DataGrid Vertical Scrollbar Problem
• Setting the value of a cell with data from another table by using a databound combobox
• bind DataGrid to 2 dimension array
• Help me! I want when I edit value in my datagrid, press Enter...