2016-06-01 12 views
-1

リストボックスのリストをクリックしてテキストボックスに結果を表示するプロジェクトを作成しました。だから私はリストボックスの値をクリックすると、それは結果を取得する必要があります&データベースからの各列は、私が作成したテキストボックスに収まる必要があります。リストボックスからデータを取得してテキストボックスに表示

私はすでにいくつかのコードを書いていますが、動作しません。私はあなたの助けが必要

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(); 

    } 

私はつまり、データベース内の3つの列(食材、方法、レシピ)

+1

働いていないでしょうか? – Fruchtzwerg

+0

3つの列またはテーブルがありますか? –

+0

テーブルに3列ある – Vidya

答えて

0

は、フォーム負荷にリストボックスの内容を設定し、追加また、あなたの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(); 
} 
0

TextBoxのテキストを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(); 
+0

いいえ!何も働かなかった。テキストボックスは空です。結果がフェッチされていません。 – Vidya

+0

クエリは結果を返しますか?文字列s = "レシピから選択*"; – lvoros

+0

はい私は結果を得ます。私は "レシピからcol1、col2、col3を選択"として与えました – Vidya

関連する問題