Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

Please tell me how to fetch the data from the database by combobox selectionchenge

Below is my code

VB
Dim cmd3 As New SqlCommand("select * from Company", connection)
          cmd3.ExecuteNonQuery()
          Dim da As New SqlDataAdapter(cmd3)
          da.Fill(dt)

        For Each dr As DataRow In dt.Rows
              txtCompanyCode.Text = dt.Rows(0)("CompanyCode").ToString()

              txtCompanyAlias.Text = dt.Rows(0)("Alias").ToString()
              txtAddress.Text = dt.Rows(0)("Address").ToString()
              cmbCity.Text = dt.Rows(0)("city").ToString()
              txtPincode.Text = dt.Rows(0)("Pincode").ToString()
              cmbState.Text = dt.Rows(0)("State").ToString()
              txtCountry.Text = dt.Rows(0)("Country").ToString()
              txtPh1.Text = dt.Rows(0)("Ph1").ToString()
              txtPh2.Text = dt.Rows(0)("Ph2").ToString()
              txtPh3.Text = dt.Rows(0)("Ph3").ToString()
              txtFax.Text = dt.Rows(0)("Fax").ToString()
              txtMobile.Text = dt.Rows(0)("Mobile").ToString()
              txtEmail.Text = dt.Rows(0)("Email").ToString()
              txtWebsite.Text = dt.Rows(0)("Website").ToString()


Its showing only one record.for the next selection it's not loading the values..
Posted
Comments
Pheonyx 2-May-13 3:55am    
I'm sorry, but what exactly are you trying to do? The controls you are showing in your code example cannot display more than one value at a time, secondly you are iterating through a For Each loop but never using the data.

I think you need to try and understand more about what you are doing first.
[no name] 2-May-13 3:57am    
i want to fetch the data from the database at combo box seelction change event..i binded the combobox with companyname
Pheonyx 2-May-13 4:04am    
So to confirm, you have a combo box populated with company name?
[no name] 2-May-13 4:06am    
yes..and while selecting each value i want to fetch the data in respective textboxes

I would do something similar to the following:

VB
Dim cmd3 As New SqlCommand("select * from Company Where CompanyName=@companyname", connection)
cmd3.Parameters.Addwithvalue("@companyname", mycombobox.SelectedValue)
cmd3.ExecuteNonQuery()
Dim da As New SqlDataAdapter(cmd3)
da.Fill(dt)

if dr.Rows.Count > 1 then
    messagebox.show("More than one record found for the company name")
    exit sub
end if 

if dr.Rows.Count = 0 then
    messsagebox.show("No records found")
    exit sub
end if

          For Each dr As DataRow In dt.Rows
                txtCompanyCode.Text = dr("CompanyCode").ToString()
             
                txtCompanyAlias.Text = dr("Alias").ToString()
                txtAddress.Text = dr("Address").ToString()
                cmbCity.Text = dr("city").ToString()
                txtPincode.Text = dr("Pincode").ToString()
                cmbState.Text = dr("State").ToString()
                txtCountry.Text = dr("Country").ToString()
                txtPh1.Text = dr("Ph1").ToString()
                txtPh2.Text = dr("Ph2").ToString()
                txtPh3.Text = dr("Ph3").ToString()
                txtFax.Text = dr("Fax").ToString()
                txtMobile.Text = dr("Mobile").ToString()
                txtEmail.Text = dr("Email").ToString()
                txtWebsite.Text = dr("Website").ToString()
Next


At least that would be my first attempt. I would later look at making a company class and introducing data binding.
 
Share this answer
 
Comments
[no name] 2-May-13 5:50am    
not working...
Pheonyx 2-May-13 5:53am    
Not working... thats great.. but what is not working? what issues are you having? It is not an exact science, I do not know your database structure, name of other controls on your form etc. I have provided you with a loose approach that you could use to achieve what you are after. Not working is an unhelpful comment.
[no name] 2-May-13 5:55am    
its not fetching any value...
Solution Guide 2:

VB
Dim sql as String = "Select * From Company Where CompanyName=@CompanyName"
Dim objDA As System.Data.SqlClient.SqlDataAdapter = _
    New System.Data.SqlClient.SqlDataAdapter(sql, connection)
objDA .Parameters.Add("@CompanyName", CompanyNameComoboBox.SelectedValue)

Dim objDS As New DataSet

Try
	objDA.Fill(objDS, "CompanyData")


	If objDS.Tables("CompanyData").Rows.Count = 0 Then
		Messagebox.Show("No Entries Found")
		Exit Sub
	ElseIf objDS.Tables("CompanyData").Rows.Count >1 Then
		Messagebox.Show("Results are not distinct!")
		Exit Sub
	EndIf

	For Each dr As DataRow In objDS.Tables("CompanyData").Rows
                txtCompanyCode.Text = dr ("CompanyCode").ToString()
                txtCompanyAlias.Text = dr ("Alias").ToString()
                txtAddress.Text = dr ("Address").ToString()
                cmbCity.Text = dr ("city").ToString()
                txtPincode.Text = dr ("Pincode").ToString()
                cmbState.Text = dr ("State").ToString()
                txtCountry.Text = dr ("Country").ToString()
                txtPh1.Text = dr ("Ph1").ToString()
                txtPh2.Text = dr ("Ph2").ToString()
                txtPh3.Text = dr ("Ph3").ToString()
                txtFax.Text = dr ("Fax").ToString()
                txtMobile.Text = dr ("Mobile").ToString()
                txtEmail.Text = dr ("Email").ToString()
                txtWebsite.Text = dr ("Website").ToString()
	Next
Catch ex As Exception

End Try


Things to check, which I do not know! What is the name of the combo box of company names called?
Change CompanyNameComoboBox to be that control's name
Also, is it actually displaying the name of the company in that combo box?
The SQL statement might be wrong because I do not know the column header you are searching on, so change that if it is different.

The premise of what I have defined here should work but will need some tweaking to your exact situation and without that knowledge I cannot do that bit.
 
Share this answer
 

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