I have a problem trying to link my my datagrid view to my sql server stored procedure. The way it should work is that when I select an item in a list from my combo box it returns a set of rows on the datagrid view specific only to that item .
This is the code on the server side
CREATE PROCEDURE spCompanySearch @Comp VARCHAR(50) AS BEGIN SELECT * FROM Company WHERE [COMPANY NAME] LIKE '%' + @Comp + '%'' END

The @comp is supposed to be each item in the combobox list but I don't know how to do it or how to go about it so please if anybody can help me I will be very grateful. Examples will be appreciated

for ComboBox, where the data came from? database?

yes sql server table

here is an anology :

i have 1 table in database, name was "characters" (let we say that table hold detail of any character from action movie, example : Superman, Batgirl, Megathron and Wolverine), so, i add some field/column/detail in that table, the field/column/detail is :

"nameOfChar"
"gender"
"strengthLevel"
"weakness"

in my form, i put a ComboBox and a DataGridView (DGV).

i want that my ComboBox fill with the "nameOfChar", so, i do this to my FormLoad event :

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        Using conn As New SqlConnection("YOUR CONNECTION STRING HERE")
            conn.Open()
            Dim command As New SqlCommand("select * from characters", conn)
            Dim reader As SqlDataReader = command.ExecuteReader
            While reader.Read()
                Me.ComboBox1.Items.Add(reader("nameOfChar").ToString)
            End While
            reader.Close()
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

for DGV, i add 3 columns through its properties (minus "nameOfChar" because ComboBox has already hold it), that 3 columns to hold "gender", "strengthLevel" and "weakness".

in ComboBox1 SelectedIndexChanged Event i put this code :

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
        Using conn As New SqlConnection("YOUR CONNECTION STRING HERE")
            conn.Open()
            Dim command As New SqlCommand("select * from characters where nameOfChar = '" & Me.ComboBox1.SelectedItem & "'", conn)
            Dim reader As SqlDataReader = command.ExecuteReader
            While reader.Read
                Me.DataGridView1.Rows(0).Cells(0).Value = reader("gender").ToString
                Me.DataGridView1.Rows(0).Cells(1).Value = reader("strengthlevel").ToString
                Me.DataGridView1.Rows(0).Cells(2).Value = reader("weakness").ToString
            End While
            reader.Close()
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

and here is the result :

Result 1

Result 2

hope give you some imagination :D

regards

Thanks, you got my question spot on and the answer worked fro me....halfway. The thing is at the point where it is read into the datagrid, It throws an exception

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name Index"

This is my segment for that part

 Me.dtgCompanyPriceList.Rows(0).Cells(0).Value = Reader("COMPANY ID").ToString
   Me.dtgCompanyPriceList.Rows(0).Cells(1).Value = Reader("SYMBOL").ToString
   Me.dtgCompanyPriceList.Rows(0).Cells(2).Value = Reader("SECURITY").ToString
 Me.dtgCompanyPriceList.Rows(0).Cells(3).Value = Reader("OPENING PRICE").ToString
Me.dtgCompanyPriceList.Rows(0).Cells(4).Value = Reader("CLOSING PRICE").ToString
 Me.dtgCompanyPriceList.Rows(0).Cells(5).Value = Reader("PUBDATE").ToString
Me.dtgCompanyPriceList.Rows(0).Cells(6).Value = Reader("DESCRIPTION").ToString

so do you have an Idea where it all went wrong.Thanks

yes i have an idea sir :D

as you can see.. in my example above, i use 3 columns, and you have 7 columns, sure it will out of index. so, add more column to your DGV.

hey sorry for not responding earlier, I had forgotten to create the column headings so everything was smooth sailing from there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.