【问题标题】:how to made combobox drop down change its list in correspondence to another combobox?如何使组合框下拉更改其列表以对应另一个组合框?
【发布时间】:2016-04-01 14:22:19
【问题描述】:

我目前正在使用 Visual Studio 2015 和 SQL Server 为一个商业社会开展一个小型项目。我有两个组合框“AccountCode”和“AccountNo”。我想制作“AccountNo”组合框以根据从“AccountCode”下拉列表中选择文本来更改其值。但我收到以下错误:

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

附加信息:无法绑定多部分标识符“System.Data.DataRowView”。

private void cmBxAccountCode_DataEntry_SelectedIndexChanged(object sender, EventArgs e)
    {
        cmBxAccountNo_DataEntry.ResetText();
        cmBxAccountNo_DataEntry.Refresh();
        DataSet ds = new DataSet();
        da.SelectCommand = new SqlCommand("SELECT ID,ACCOUNTNO FROM  AccountHolder WHERE ACCOUNTCODE =" + cmBxAccountCode_DataEntry.Text, SQLConnection.con);
        da.Fill(ds);
        cmBxAccountNo_DataEntry.DataSource = ds.Tables[0];
        cmBxAccountNo_DataEntry.DisplayMember = "ACCOUNTNO";
        cmBxAccountNo_DataEntry.ValueMember = "ID";
    }

【问题讨论】:

    标签: c# wpf combobox


    【解决方案1】:

    您的查询应该是这样的:

    SELECT ID,ACCOUNTNO FROM  AccountHolder WHERE ACCOUNTCODE ='" + cmBxAccountCode_DataEntry.Text  + "'"
    

    但我强烈建议您使用参数化查询以避免注入;并且还为Text(但它是根据数据库中的值)取选定的值。参数化查询将是这样的:

    // Code here
    SqlCommand selectCommand=  new SqlCommand("SELECT ID,ACCOUNTNO FROM  AccountHolder WHERE ACCOUNTCODE =@accountCode", SQLConnection.con);
    selectCommand.Parameters.Add("@accountCode", SqlDbType.VarChar).Value = cmBxAccountCode_DataEntry.Text;
    // execute here
    

    【讨论】:

    • 感谢@un-lucky。我明白了,我的问题解决了! :)
    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多