与数据库组合框在C#中选择索引值

时间:2019-01-27 12:46:14

标签: c#

我是C#的新手,我有一个问题。  我想从组合框中选择一个值,它应该在标签上显示它的年龄。

我是做什么的

 public void FillCombo()
        {
           SqlDataAdapter adap = new SqlDataAdapter("Select * from customers",con);

            DataTable dt = new DataTable();
            adap.Fill(dt);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";

        }

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();

           SqlCommand cmd1 = new SqlCommand("Select * from customers where name=@name ", con);
            cmd1.Parameters.AddWithValue("@name",comboBox1.SelectedItem));

           int i= cmd1.ExecuteNonQuery();

            if (i > 0)
            {
               SqlDataReader sqlrdr = cmd1.ExecuteReader();
            while (sqlrdr.Read())
            {
                String age= sqlrdr["age"].ToString();
                label1.Text = age;
            }

            }

 else{
MessageBox.Show("no value");
}

con.Close();
    }

即使我在数据库中有值,它也不会显示任何值消息。我该怎么办?

2 个答案:

答案 0 :(得分:2)

将数据源设置为数据表时,组合框中的每个项目均为DataRowView。因此,您已经在组合框中获得了有关当前客户年龄的信息。无需再次调用数据库。

您只需要使用SelectedItem属性来检索有关 age 字段或数据源中存在的任何其他字段的信息

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataRowView rv = l.SelectedItem as DataRowView;

    // For safety, always check for null. 
    // It is possible that SelectedIndexChanged 
    // will be called even when there is no selection in the combobox
    if(rv != null)
    {
        label1.Text = rv["age"].ToString();
        ....
    }
}

答案 1 :(得分:-1)

尝试此操作以获得索引,值和所选名称:

ghc-exactprint-0.5.8.2: configure
ghc-exactprint-0.5.8.2: build
Progress 1/2

--  While building package ghc-exactprint-0.5.8.2 using:
      /home/developer/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-2.4.0.1 build --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
    Logs have been written to: /home/developer/.stack/global-project/.stack-work/logs/ghc-exactprint-0.5.8.2.log

    Configuring ghc-exactprint-0.5.8.2...
    Preprocessing library for ghc-exactprint-0.5.8.2..
    Building library for ghc-exactprint-0.5.8.2..
    [ 1 of 13] Compiling Language.Haskell.GHC.ExactPrint.Types ( src/Language/Haskell/GHC/ExactPrint/Types.hs, .stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/Language/Haskell/GHC/ExactPrint/Types.o )
    [ 2 of 13] Compiling Language.Haskell.GHC.ExactPrint.Lookup ( src/Language/Haskell/GHC/ExactPrint/Lookup.hs, .stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/Language/Haskell/GHC/ExactPrint/Lookup.o )
    [ 3 of 13] Compiling Language.Haskell.GHC.ExactPrint.AnnotateTypes ( src/Language/Haskell/GHC/ExactPrint/AnnotateTypes.hs, .stack-work/dist/x86_64-linux/Cabal-2.4.0.1/build/Language/Haskell/GHC/ExactPrint/AnnotateTypes.o )
    <command line>: can't load .so/.DLL for: libtinfo.so (libtinfo.so: cannot open shared object file: No such file or directory)