Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

VB
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
                da.Fill(ds, "customer")
                dt_sample.DataSource = ds.Tables(0)


                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


the output for that would be only the last cell being showed.

[^]]
Posted
Updated 22-Oct-19 5:07am

Try something like this:
VB.NET
Using command As New SqlCommand()
    command.Connection = conn
    
    Dim parameterNames As New List(Of String)(dt_data.RowCount - 2)
    For i As Integer = 0 To dt_data.RowCount - 3
        Dim parameterName As String = "@meter_num_" & i
        Dim meter_number As String = dt_data.Rows(i).Cells(3).Value
        command.Parameters.AddWithValue(parameterName, meter_number)
        parameterNames.Add(parameterName)
    Next
    
    command.CommandText = String.Format("SELECT * FROM customer WHERE meter_num IN ({0})", String.Join(",", parameterNames))
    
    Dim da As New SqlDataAdapter(command)
    Dim ds As New DataSet
    da.Fill(ds, "customer")
    dt_sample.DataSource = ds.Tables(0)
End Using
 
Share this answer
 
v2
Comments
Member 14153541 22-Oct-19 23:00pm    
Thanks this works like a charm, been looking for the solution for a month
Don't put these lines in your For loop:
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
da.Fill(ds, "customer")
dt_sample.DataSource = ds.Tables(0)
conn.Close()

And construct your query like this:
Dim query As String = "Select * from customer where meter_num IN @meter_numbers"
[Edit] see the solution by Richard Deeming, or try one of the methods mentioned here: sql server - Parameterize an SQL IN clause - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Richard Deeming 22-Oct-19 10:59am    
Unfortunately, you can't use a single parameter for an IN query.
RickZeeland 22-Oct-19 12:31pm    
Thanks for pointing that out, I will update the solution :)

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