I have an Excel sheet loaded in dt_data
and I want to compare the values of dt_data
to my database and display it on another datagridview which is dt_sample
. Inside my database there is past 3 months record and I want to get them all to be displayed here is my code below:
Try For i As Integer = 0 To dt_data.RowCount - 3 Dim meter_number As String meter_number = dt_data.Rows(i).Cells(3).Value Dim query As String = "Select * from customer where meter_num = @meter_num" conn.Open() Dim command As New SqlCommand(query, conn) command.Parameters.AddWithValue("@meter_num", meter_number) Dim da As New SqlDataAdapter(command) Dim ds As New DataSet If i = dt_data.RowCount - 3 Then da.Fill(ds, "customer") dt_sample.DataSource = ds.Tables(0) End If conn.Close() Next Catch ex As SqlException MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error") Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error") 'End Try 'Catch ex As Exception 'MessageBox.Show(String.Format("Error: {0}", ex.Message), "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) End Try