C# 从列表框中检索数据并显示在文本框中

C# 从列表框中检索数据并显示在文本框中,c#,.net,listbox,C#,.net,Listbox,我编写了一个项目,通过单击列表框中的列表,在文本框中显示结果。因此,当我单击列表框中的一个值时,它应该检索结果&数据库中的每一列都应该放入我创建的文本框中 我已经写了一些代码,但它不起作用。我需要你的帮助 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { con.Open(); string s = "select * from recipe"; //SqlCommand

我编写了一个项目,通过单击列表框中的列表,在文本框中显示结果。因此,当我单击列表框中的一个值时,它应该检索结果&数据库中的每一列都应该放入我创建的文本框中

我已经写了一些代码,但它不起作用。我需要你的帮助

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    con.Open();
    string s = "select * from recipe";
    //SqlCommand cmd = new SqlCommand("select * from recipe where ('" + ListBox1.SelectedItem.ToString() + "')", con);
    SqlDataAdapter da = new SqlDataAdapter(s, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    ing_tx.Text = ListBox1.SelectedValue.ToString();
    mx_tx.text = ListBox1.SelectedValue.ToString();
    re_tx.text = ListBox1.SelectedValue.ToString();

    }

我在数据库中有三列,即(配料、方法、配方)

在表单加载上设置列表框的内容,并将其从SelectedIndexChanged事件中取出,同时在中添加列表框数据源

// Create the event 
public void Form_Load()
{
   con.Open();
    string s = "select * from recipe";
    //SqlCommand cmd = new SqlCommand("select * from recipe where ('" + ListBox1.SelectedItem.ToString() + "')", con);
    SqlDataAdapter da = new SqlDataAdapter(s, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    ListBox1.DisplayMember = <one of your recipe table fields (e.g. Name)>;
    ListBox1.ValueMember = <one of your recipe table fields (e.g. ID);

}

// Set your textboxes
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // These will all provide the same result, you may need to requery the database for your fields based on an ID.
    ing_tx.Text = ListBox1.SelectedValue.ToString();
    mx_tx.text = ListBox1.SelectedValue.ToString();
    re_tx.text = ListBox1.SelectedValue.ToString();
}
//创建事件
公共作废表格(加载)
{
con.Open();
字符串s=“从配方中选择*”;
//SqlCommand cmd=new SqlCommand(“从配方中选择*,其中(“”+ListBox1.SelectedItem.ToString()+”)”,con);
SqlDataAdapter da=新的SqlDataAdapter(s,con);
数据集ds=新数据集();
da.填充(ds);
ListBox1.DisplayMember=;

ListBox1.ValueMember=将文本框的文本设置为ListBox1.SelectedValue.ToString()

你应该:

ing_tx.Text = ds.Tables[0].Rows[0]["Ingredients"].ToString();
mx_tx.text = ds.Tables[0].Rows[0]["Methods"].ToString();
re_tx.text = ds.Tables[0].Rows[0]["Recipe"].ToString();

什么不起作用?你有三列或三张表吗?我有三列table@Fruchtzwerg我可以添加到列表中。但是从listbox中,没有任何内容被提取到textboxNope!没有任何结果。textbox为空。结果没有提取。查询是否给出结果?string s=“select*from recipe”;是,我得到了结果。我作为“从配方中选择col1、col2、col3”您写下的列名是:配料、方法、配方……是吗?还是它们是col1、col2、col3?它们是配料、方法、配方。