帮我纠正代码 [英] Help me to correct the code

查看:60
本文介绍了帮我纠正代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在使用VB.Net 2008和Access 2007.

我有Access数据库表名TableSrce。该表有两列Home / Office和BillMeetrNo。我的vb表单上有两个组合框cmbx1表示第1列,cmbx2表示第二列。

我想要当我从第一个组合框中选择一个项目时,第二个组合框填充了相应的数据。

例如,当我从第一个组合框中选择Home时,第二个组合框只显示与家庭无关的账单。



我有代码但是没有用

请指出我的错误并指导我。



  Imports  System.Data.OleDb 
公共 Form1



私人 Sub ComboBox1_SelectedIndexChanged( ByVal sender As System。 Object ByVal e 作为 Syst em.EventArgs)句柄 ComboBox1.SelectedIndexChanged
Dim cn 作为 OleDbConnection

Dim cmd 作为 OleDbCommand
Dim adapter 作为 OleDbDataAdapter
Dim dt < span class =code-keyword> As New DataTable
cn.ConnectionString =( Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\ UtilityBills.accdb;
cn.Open()
cmd.Connection = cn
cmd.CommandText = SEL ECT Home / Office FROM SrceElectricity WHERE Home / Office ='& ComboBox1.Text& '
cmd.CommandType = CommandType.Text
adapter.SelectCommand = cmd
adapter.Fill(dt)
ComboBox2.DataSource = dt
ComboBox2.ValueMember = EleMeeterNo
ComboBox2.DisplayMember = EleMeeterNo
结束 Sub
结束

解决方案

 EleMeeterNo 列。



您还提到该列的名称是

 BillMeetrNo 

而不是

 EleMeeterNo 





因此最终代码看起来像这样:



 cmd.CommandText =SELECT BillMeetrNo FROM SrceElectricity WHERE Home / Office ='& ComboBox1.Text& 


Hello
I am using VB.Net 2008 and Access 2007.
I have Access Data Base Table Name "TableSrce". This table having two column "Home/Office" and "BillMeetrNo". I have two combo box on my vb form "cmbx1" for 1st column and cmbx2 for second column.
I want that when i select an item from 1st combo box, the 2nd combo box populated with the corresponding data.
For example when i select Home from the 1st combo box , the 2nd combo box show only the bill meeter no related to home.

I have the code but it did not work
Please point out my mistake and guide me .

Imports System.Data.OleDb
Public Class Form1

   

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim cn As New OleDbConnection

        Dim cmd As New OleDbCommand
        Dim adapter As New OleDbDataAdapter
        Dim dt As New DataTable
        cn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\UtilityBills.accdb;")
        cn.Open()
        cmd.Connection = cn
        cmd.CommandText = "SELECT Home/Office FROM SrceElectricity WHERE Home/Office ='" & ComboBox1.Text & "'"
        cmd.CommandType = CommandType.Text
        adapter.SelectCommand = cmd
        adapter.Fill(dt)
        ComboBox2.DataSource = dt
        ComboBox2.ValueMember = "EleMeeterNo"
        ComboBox2.DisplayMember = "EleMeeterNo"
    End Sub
End Class

解决方案

The

EleMeeterNo

column does not exist in your Select statement.

Also you mentioned that the name of the column was

BillMeetrNo

and not

EleMeeterNo



Hence the final code shold look something like this:

cmd.CommandText = "SELECT BillMeetrNo FROM SrceElectricity WHERE Home/Office ='" & ComboBox1.Text & "'"


这篇关于帮我纠正代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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