Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Could somebody tell me how to update table with datagridview with blank cell in it?
I have created a data entry form with datagridview with four columns in designer. I want to leave some cells of a column blank and save blank cells as zero in table. If there are no blank cells I can save datagridview content into table.



VB
Dim nonqueryCommand As SqlCommand = 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,col4) VALUES (@Col1, @Col2,@col3,@col4)"

            nonqueryCommand.Prepare()
        

            ' Data to be inserted
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then

                    nonqueryCommand.Parameters.Add("@Col1", SqlDbType.VarChar, 50).Value = DBNull.Value
                    nonqueryCommand.Parameters.Add("@Col2", SqlDbType.VarChar, 50).Value = DBNull.Value
                    nonqueryCommand.Parameters.Add("@Col3", SqlDbType.VarChar, 50).Value = DBNull.Value
                    nonqueryCommand.Parameters.Add("@Col4", SqlDbType.VarChar, 50).Value = DBNull.Value


                    nonqueryCommand.Parameters("@Col1").Value = row.Cells(1).Value.ToString()
                    nonqueryCommand.Parameters("@Col2").Value = row.Cells(2).Value.ToString()  
                    nonqueryCommand.Parameters("@Col3").Value = row.Cells(3).Value.ToString()
                    nonqueryCommand.Parameters("@Col4").Value = row.Cells(4).Value.ToString()
                    nonqueryCommand.ExecuteNonQuery()

                End If
            Next



        Catch ex As SqlException
            ' Display error
            Console.WriteLine("Error: " & ex.ToString())
        Finally
            ' Close Connection
            thisConnection.Close()
            Console.WriteLine("Connection Closed")

        End Try


What I have tried:

I don’t know if this is correct way to check for empty cell in order to save into table. I get an error when I place the code between try and Catch ex As SqlException

An OleDbParameter with ParameterName '@Col1' is not contained by this OleDbParameterCollection

VB
If row.Cells(0).Value.ToString IsNot Nothing Then
   nonqueryCommand.Parameters("@Col1").Value = row.Cells(0).Value.ToString()
else
   nonqueryCommand.Parameters("@Col1").Value = "0"
end if
Posted
Updated 15-May-16 19:21pm
Comments
Beginner Luck 16-May-16 1:18am    
are you using auto column or templete??

1 solution

Hi Josef

Your code for checking the particular cell value is empty and save with 0 is correct
but you will have to do some little change in your code while running the Sql Command..

if you see the code, you are keep on adding the command parameters inside the loop since the command object is declared outside the loop. it should be avoided.

Try adding the entire code inside the for each loop.
For every transaction the data base connection should be opened and closed, else it wont work as expected in case of large loop.

Try removing prepare() and check it.
Please read this[^] before using Prepare().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900