2016-06-01 4 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(); 

    } 

나는, 즉 데이터베이스에있는 세 개의 열 (재료, 방법, 레시피)

+1

작동하지 않습니다? – Fruchtzwerg

+0

세 개의 열이나 테이블이 있습니까? –

+0

테이블에 3 개의 열이 있습니다. – Vidya

답변

0

양식로드에 목록 상자의 내용을 설정하고 추가 또한, 귀하의 SelectedIndexChanged 이벤트에서 가져가 Listbox 데이터 소스.

// 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

텍스트 상자의 텍스트를 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

Nope! 아무것도 효과가 없습니다. 텍스트 상자가 비어 있습니다. 결과가 가져 오지 않습니다. – Vidya

+0

쿼리가 결과를 제공합니까? 문자열 s = "select * from recipe"; – lvoros

+0

예 결과가 나타납니다. 나는 "recipe에서 col1, col2, col3을 선택하십시오"라고 말했습니다. – Vidya

관련 문제