Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get specific data to show up in gridview. I am trying to use a select where statement, but it is not giving any data back.

'fill funeral home comboboxes, this displays data in your combobox through an sql connection
VB
        Dim funcon As New SqlConnection("sqlconstring")


        Dim sqladapt As New SqlDataAdapter

        funcon.Open()

        Dim command As SqlCommand = New SqlCommand("Select county from [business] where [state] = @state", funcon)
      command.Parameters.AddWithValue("@State", Businesscombobox.SelectedItem)
        sqladapt.SelectCommand = command
        sqladapt.SelectCommand.ExecuteNonQuery()
        command.CommandTimeout = False
        Dim dts As New DataTable
        Dim dss As New DataSet
        sqladapt.Fill(dts)

        DataGridView1.DataSource = dts
funcon.close


Not sure why I cannot get data to appear

What I have tried:

I have tried to change the way the text is selected in the combobox. I even tried to use a state that is in the datatable to see it directly implanting a state into the statement would result in data feedback and still nothing. If I do not use the where statement it brings up all my data, where I am going wrong?
Posted
Updated 22-Sep-17 14:26pm
v2

1 solution

First, "@State" is not the same as "@state".

Next, Businesscombobox.SelectedItem doesn't return what you think it does. It's going to call .ToString on whatever that selected item is, and that may not return the string you want.

Do yourself a favor and move the Businesscombobox.SelectedItem into its own variable and inspect the content in the debugger.
VB
Dim selected As String = Businesscombobox.SelectedItem
command.Parameters.AddWithValue...

Put a breakpoint on the AddWithValue line and run the code. When the debugger stops on this line, you can hover the mouse over selected to see the content that you're passing to the Parameter object.
 
Share this answer
 
Comments
[no name] 24-Sep-17 6:33am    
According to MSDN: "Parameter names are not case sensitive". Anyway a good hint to use the same value to avoid confusions.
Dave Kreskowiak 24-Sep-17 10:39am    
Yeah, more of a "rule of thumb" to be used everywhere, not just parameters.

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