#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    inserting null values from datagridview to table


    I am trying to insert null values from datagridview cell to ms access table . the code works but when I leave one cell empty it gives me error " Object reference not set to an instance of an object." I have checked validation rule under property sheet is blank and required field is set to no in ms access table.
    can someone tell me how to insert null value of a text box into table?
    Thanks


    Code:
             'Create Command object
            Dim nonqueryCommand As OleDbCommand = thisConnection.CreateCommand()
    
            Try
                ' Open Connection
                thisConnection.Open()
                Console.WriteLine("Connection Opened")
    
                ' Create INSERT statement with named parameters
                nonqueryCommand.CommandText = _
                   "INSERT  INTO Table3 (Col1, Col2,col3) VALUES (@Col1, @Col2,@col3)"
    
                nonqueryCommand.Prepare()
    
                ' Data to be inserted
                For Each row As DataGridViewRow In DataGridView1.Rows
                    If Not row.IsNewRow Then
    
                        nonqueryCommand.Parameters.Add("@Col1", OleDbType.VarChar, 50)
                        nonqueryCommand.Parameters.Add("@Col2", OleDbType.VarChar, 50)
                        nonqueryCommand.Parameters.Add("@Col3", OleDbType.VarChar, 50)
    
    
                        nonqueryCommand.Parameters("@Col1").Value = row.Cells(0).Value.ToString()
                        nonqueryCommand.Parameters("@Col2").Value = row.Cells(1).Value.ToString()
                        nonqueryCommand.Parameters("@Col3").Value = row.Cells(2).Value.ToString()
    
                        nonqueryCommand.ExecuteNonQuery()
                     
    
                    End If
                Next
    
    
    
            Catch ex As OleDbException
                ' Display error
                Console.WriteLine("Error: " & ex.ToString())
            Finally
                'Close Connection
                thisConnection.Close()
  2. #2
  3. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Aug 2008
    Location
    USA
    Posts
    1,606
    Rep Power
    347
    Could you please provide the exact error line and error statement , this will help us to help you quickly
    if you found this post is useful click Give Rep button (bottom side on this reply ) and agree

    Jquery Shake Effect ,
    Andriod KitKat


    Thank You,
    KiranK

Similar Threads

  1. Showing Values that don't Equal field or Null as a Null
    By ipicus in forum ASP Development
    Replies: 1
    Last Post: November 19th, 2010, 01:26 AM
  2. Inserting NULL into blank fields in table
    By zeetec1 in forum .NET Development
    Replies: 13
    Last Post: June 24th, 2010, 08:05 AM
  3. update not inserting null value
    By -Dman100- in forum ASP Development
    Replies: 1
    Last Post: February 3rd, 2005, 01:01 PM
  4. Replies: 6
    Last Post: September 23rd, 2004, 02:49 AM
  5. Inserting NULL into DB HELP....
    By aspnewbee in forum ASP Development
    Replies: 6
    Last Post: July 12th, 2004, 02:26 PM

IMN logo majestic logo threadwatch logo seochat tools logo