使用Combobox调用(ID_number)主键到文本框(类别描述)

时间:2017-08-24 03:06:13

标签: c# ms-access-2010

在C#中使用ms访问处理外键。

每次我从类别表中使用组合框选择类别描述(CatDesc)时,我想自动获取/调用文本框1中的类别ID(CatID)。

我的代码不起作用,也没有给出错误。

这是我的Sample_COde:

private void cmdcategoryy_TextChanged(object sender, EventArgs e)
{
       CategoryDesc();    
}

void CategoryDesc()

{
    OleDbConnection mycon = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\BlaBla\Desktop\Docsmonitorg\Try 081517\WindowsFormsApp6\WindowsFormsApp6\Database.accdb");

    string con;

    try
    {

        con = "Select CatID, CatDesc from Category where CatDesc = '" + cmdcategoryy.Text + "' ";
        mycon.Open();
        OleDbCommand cm = new OleDbCommand();
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable mydata1 = new DataTable();

        cm.CommandText = con;
        cm.Connection = mycon;

        int i;
        da.SelectCommand = cm;
        da.Fill(mydata1);

        for (i = 0; i <= mydata1.Rows.Count - 1;)
            textBox1.Text = (mydata1.Rows[i]["CatID"].ToString());

    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    finally
    {
        mycon.Close();
        mycon.Dispose();
    }


}

Here is the design

This is my Database 样品:

类别说明有Graphic Desginer,AutoCat,AX Staff 类别ID 1,2,3

我将隐藏该文本框1。谢谢,还有女神!

0 个答案:

没有答案