如何通过combobox selectionchange获取数据库值 [英] how to fetch database value by combobox selectionchange

查看:80
本文介绍了如何通过combobox selectionchange获取数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



请告诉我如何通过combobox selectionchenge从数据库中获取数据



以下是我的代码



Hi all

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

Below is my code

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..

推荐答案

我会做类似以下的事情:



I would do something similar to the following:

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.


解决方案指南2:



Solution Guide 2:

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





要检查的事项,我不知道!公司名称组合框的名称是什么?

将CompanyNameComoboBox更改为该控件的名称

此外,它是否实际显示公司名称在那个组合框中?

SQL语句可能是错误的,因为我不知道你正在搜索的列标题,所以如果它不同则改变它。



我在这里定义的前提应该有效但需要对你的确切情况进行一些调整,如果没有这方面的知识,我就不能这样做了。



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.


这篇关于如何通过combobox selectionchange获取数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆