Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using following code to read and insert a column from gridview to Mssql database. Their is no error in debugging but the data is not inserted. Pls advice.

Thanks in advance

Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        Dim dt As New DataTable()
        dt.Columns.Add("TDSMRANGE")
      

        For Each row As GridViewRow In gdTDSM.Rows
                Dim TDSMRANGE As Integer = row.Cells(0).Text
                dt.Rows.Add(TDSMRANGE)
        Next

        If dt.Rows.Count > 0 Then
            Try
                Using sqlBulkCopy As New SqlBulkCopy(connection)
                    'Set the database table name
                    sqlBulkCopy.DestinationTableName = "dbo.TDSM"

                    If connection.State = ConnectionState.Closed Then
                        connection.Open()
                    End If
                    Try
                        sqlBulkCopy.WriteToServer(dt)

                    Catch ex As Exception
                        fnMessage(Me, ex.Message)
                    End Try
                    connection.Close()

                End Using
            Catch EX As Exception
                fnMessage(Me, EX.Message)
            End Try
        End If


    End Sub
Posted
Comments
ZurdoDev 28-Jan-15 7:46am    
I'd step through line by line and make sure sqlBulkCopy.WriteToServer is actually running and no error. Also do a sql trace.
Umer Akram 28-Jan-15 7:48am    
are you looking at the right server & database to which your connection string is pointing to ?
atul sharma 5126 28-Jan-15 8:54am    
Thanks for the replies.
I have done line by line debugging with view over the output window and found no error.

And yes I have tested the server and connection in same code by adding a normal insert command instead of bulk.

Also to bring to notice that I have an identity field in datatable to which I am not sending any value.
Umer Akram 29-Jan-15 3:00am    
you should use column mappings something like

bulkCopy.ColumnMappings.Add("col1", "dt_col1");
bulkCopy.ColumnMappings.Add("col2", "dt_col2");
sqlBulkCopy.DestinationTableName = "dbo.TDSM";

and try it.
bulkCopy.WriteToServer(dt);

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