hi everyone , can someone help me by this
i have a DB table and a form the following fields , combobox and textboxes, what i need is how to write a query to retrieve data from the table when the user select some record from combobox

thnx before and help me plz ,

Recommended Answers

All 6 Replies

The standard select statement has the form of

SELECT col_name1, col_name2, etc FROM table_name WHERE some_col = some_value;

So you will want to store the selected option from the combobox and then add it to the SQL statement. You haven't given us much to go on, do you know how to create a database connection with a connection string, put your SQL statement into a command object and execute the command? If not, type ".net database tutorial" into google and follow along.
Of course, if you have specific problems and questions post them back here and we'll help out.

commented: I will give credit where credit is due. +5

TY Hericles, but not that what i meant , what i need is how to make the data that store in sql appear in textboxes depending on some field that is it ,
example :
i have project tendering no in table and all information about it , and in form i have combobox bounded with this field , when the user select from combobox i need all save data about this project appear in textboxes in same form , help me if u can ...

please someone help me , i cant solve alone..

You can use what hericles has posted and append the following:

   Dim con As New SqlConnection("Connection string here")

    Dim sqls As String = "SELECT col_name1, col_name2 FROM table_name WHERE some_col = some_value;"

    Dim cmd As New SqlCommand(sqls, con)

    Dim da As New SqlDataAdapter(cmd)

    Dim ds As DataSet

    da.Fill(ds, "Values I Need")

    Dim dt As New DataTable

    dt = ds.Tables("Values I Need")

    If dt.Rows.Count > 0 Then
        For i = 0 To dt.Rows.Count - 1
            TextBox1.Text = dt.Rows(i).Item(0)  'col_name2'
            TextBox1.Text = dt.Rows(i).Item(1)  'col_name2'
    End If

well if you have a form having diff controls , like datetime picker , picture box , textboxes and you want to populate them by using combo box. you have to bind all your controls with your datasource , like this .

dim con as new sqlconnection("you connection string")
dim da as new sqldataadapter("select * from table1 where name ='" &      combobox1.selectedvalue & "'",con)
dim dt as new datatable
'take a binding source from tool box.
bindingsource1.datasource = dt 
'here you can bind your controls like this.

hope this code will help you ,

Best Regards

Something like this might help u...

     Dim myCommand As SqlCommand
     myCommand = New SqlCommand("SELECT  * FROM Tablename where ID='" & combobox1.Text & "'", Connection)
     Dim reader As SqlDataReader = myCommand.ExecuteReader
     While reader.Read
           txtID.Text = reader("ID")
           txtName.Text = reader("Name")
           txtCost.Text = reader("Cost")
     End While
Catch ex As Exception
     MsgBox("Error Connecting to Database: " & ex.Message)
End Try
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.