如何在外键列的组合框中显示表数据 [英] How to show table data in combo box from a foreign key column

查看:92
本文介绍了如何在外键列的组合框中显示表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表 - tblState,tblDist

tblState-StateId,StateName(staeId -Primary Key)

tblDist - DistId,Dist,StateID(StateId - Foreign Key )

以上两个表绑定到两个组合框(cmbState,cmbDiist)



i想要显示Dist当cmbState selectedindexchange时cmbDist会显示数据,其中cmbState.selectedvalue = StateID



这是我的代码



I have two table - tblState, tblDist
tblState-StateId, StateName (staeId -Primary Key)
tblDist - DistId, Dist,StateID (StateId - Foreign Key)
above two table bind to two combobox (cmbState,cmbDiist)

i want to display the Dist When cmbState selectedindexchange the cmbDist will show the data where cmbState.selectedvalue = StateID

Here is my code

'LOAD SATE TABLE IN CMBSTATE
Public Sub loadstate()
        Con.Open()
        LdStateSql = "SELECT * FROM tblState ORDER BY StateName ASC"
        LdStateCmd = New SqlCommand(LdStateSql, Con)
        Dim adp = New SqlDataAdapter(LdStateCmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        cmbState.DataSource = dt
        cmbState.DisplayMember = "StateName"
        cmbState.ValueMember = "StateId"
        Con.Close()
    End Sub
'LOAD DIST TABLE IIN CMBDIST
    Public Sub loadDist()
        If ConnectionState.Open Then
            ConnectionClose()
        End If
        ConnectionOpen()
        Dim lddistsql = "SELECT * FROM tblDist WHERE StateId= '" & cmbState.SelectedValue & "'  ORDER BY DistName ASC"

        Dim lddistcmd As New SqlCommand(lddistsql, Con)
        Dim adp As New SqlDataAdapter(lddistcmd)

        Dim dt As New DataTable
        adp.Fill(dt)
        cmbDist.DisplayMember = "DistName"
        cmbDist.ValueMember = "DistId"
        cmbDist.DataSource = dt
        ConnectionClose()
    End Sub

Private Sub StudentRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
      loadstate() 
    End Sub

Private Sub cmbState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbState.SelectedIndexChanged
        loadDist()
    End Sub







此错误消息显示

未定义字符串SELECT * FROM的运算符'&' tblDist WHERE Stat和类型'DataRowView'。




This Error msg shows
Operator '&' is not defined for string "SELECT * FROM tblDist WHERE Stat" and type 'DataRowView'.

推荐答案

您选择的值是否包含'?你是字符串mashing SQL,它可以破解。您应该使用调试器来查看正在发生的事情,检查您生成的SQL,并在代码之外运行它以查看错误是什么。 string.Format不会解决这个问题。将使用参数化查询。在这个网站和其他地方有很多关于它们的文章。



所有我说的都是,但我认为这个问题是



cmbState.SelectedValue



SelectedValue应该是一个字符串,但我认为它是一个DataRowView。要么是问题,要么是另一条问题,问题是你需要从DataRowView获取值,运算符&确实没有定义,如果你添加一个DataRowView和一个字符串。
Does your selected value have a ' in it ? You are string mashing SQL, which can break. You should use the debugger to watch what is happening, check the SQL you generate, and run it outside of code to see what the error is. string.Format will not fix this. Using paramaterized queries, will. There's a number of articles on them, on this site and elsewhere.

All of what I said it true, but I think that the issue is

cmbState.SelectedValue

SelectedValue should be a string, but I think it's a DataRowView. Either that, or another line is the issue, and the issue is you need to get the value out of the DataRowView, operator & is indeed not defined if you add a DataRowView and a string.


这篇关于如何在外键列的组合框中显示表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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