none
Using a DataAdapter Update - to insert a new row - trying to have a column that is in this new row to generate its own uniqeidentifier RRS feed

  • Question

  • Hello,

    I have a table called PackagingLines that has a column called LineID.  The column DataType is uniqueidentifier and I set the 'Default Value or Binding' to newid().

    I have a datagrid where the user can insert a new row.  The LineID column is not visible to the user.  When the user creates a new row and tries to save it to the database I get a exception from my dataadapter.Update saying Cannot insert NULL into that column.  I am using a DbCommandBuilder object to create the commands for my DbDataAdapter.  How can I get this to work?  

    Wednesday, September 22, 2010 3:33 PM

Answers

  • Hi Lavagin,

    It seems that I cannot reproduce your problem, could you please post your code ? The following code works for me.

    Public Class Form1
      Dim mydataset As New DataSet
      Dim da As SqlDataAdapter
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strConn As String = "Data Source = .\SQLExpress; Initial Catalog = Northwind; Integrated Security = True"
        Dim strSQL As String = "Select * from PackagingLines"
    
        da = New SqlDataAdapter(strSQL, strConn)
        Dim cb As New SqlCommandBuilder(da)
    
        da.Fill(mydataset, "PackagingLines")
        mydataset.Tables("PackagingLines").Columns("LineID").ColumnMapping = MappingType.Hidden
    
        Me.DataGridView1.DataSource = mydataset.Tables("PackagingLines")
      End Sub
    
      Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        da.Update(mydataset.Tables("PackagingLines"))
      End Sub
    End Class
    

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Lavagin Monday, September 27, 2010 3:45 PM
    Thursday, September 23, 2010 3:36 AM
    Moderator

All replies

  • Hi Lavagin,

    It seems that I cannot reproduce your problem, could you please post your code ? The following code works for me.

    Public Class Form1
      Dim mydataset As New DataSet
      Dim da As SqlDataAdapter
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strConn As String = "Data Source = .\SQLExpress; Initial Catalog = Northwind; Integrated Security = True"
        Dim strSQL As String = "Select * from PackagingLines"
    
        da = New SqlDataAdapter(strSQL, strConn)
        Dim cb As New SqlCommandBuilder(da)
    
        da.Fill(mydataset, "PackagingLines")
        mydataset.Tables("PackagingLines").Columns("LineID").ColumnMapping = MappingType.Hidden
    
        Me.DataGridView1.DataSource = mydataset.Tables("PackagingLines")
      End Sub
    
      Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        da.Update(mydataset.Tables("PackagingLines"))
      End Sub
    End Class
    

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Lavagin Monday, September 27, 2010 3:45 PM
    Thursday, September 23, 2010 3:36 AM
    Moderator
  • Hi Lavagin,

    I am writting to follow up the post. Does the above suggestion work ?

    Please feel free to let me know if you need help.

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 27, 2010 5:26 AM
    Moderator
  • Thanks Alex.

    I see what I was doing wrong.

    Monday, September 27, 2010 3:45 PM