2016-10-16 21 views
0

私はPOSシステムで作業しています。テキストボックスから製品descreptionsを選択していますが、同じ製品価格をどのように選択していますか?テキストボックスのデータベースからデータを読み取る方法C#

public void autoFill(TextBox abc) { 
      SqlCommand cmd = new SqlCommand("SELECT * FROM pProduct",cnn.con); 
      SqlDataReader rd;  

      try 
      { 

       cnn.con.Open(); 
       rd = cmd.ExecuteReader(); 
       while (rd.Read()) { 
        abc.AutoCompleteCustomSource.Add(rd["Descreption"].ToString()); 
       } 
       rd.Close();     
       cnn.con.Close(); 
      } 
      catch (Exception ex) { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
+0

同じ商品の価格ではどういう意味ですか? – Ivan

答えて

2

価格

public void autoFill(TextBox abc, TextBox prc) { 
      SqlCommand cmd = new SqlCommand("SELECT * FROM pProduct",cnn.con); 
      SqlDataReader rd;  

      try 
      { 

       cnn.con.Open(); 
       rd = cmd.ExecuteReader(); 
       while (rd.Read()) { 
        abc.AutoCompleteCustomSource.Add(rd["Descreption"].ToString()); 
        prc.AutoCompleteCustomSource.Add(rd["Price"].ToString()); 
       } 
       rd.Close();     
       cnn.con.Close(); 
      } 
      catch (Exception ex) { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
+0

価格のリストを表示していますが、選択した説明の価格を選択する方法はありますか? –

+0

okですので、別の価格選択テキストボックスを作成するのではなく、選択した説明の価格を選択することはできますか? – Mostafiz

0

のための別のTextBoxを使用して、あなたのフォームクラスにフィールドを追加します。もちろん

Dictionary<string, decimal> products = new Dictionary<string, decimal>(); 

をあなたの代わりにstringdecimalの独自のタイプを使用する必要があります。辞書に

挿入データのデータベースからの読み取り中:

using (var conn = new SqlConnection(connectionString)) 
{ 
    conn.Open(); 
    using (var cmd = new SqlCommand("SELECT * FROM pProduct", conn)) 
    using (var reader = cmd.ExecuteReader()) 
    { 
     while (reader.Read()) 
     { 
      string description = (string)reader["Description"]; 
      decimal price = (decimal)reader["Price"]; 

      products.Add(description, price); 
      descriptionTextBox.AutoCompleteCustomSource.Add(description); 
     } 
    } 
} 

using声明。例外の場合でもリソースを閉じて処分します。

TextChangedイベントに登録してください。

private void DescriptionTextBox_TextChanged(object sender, EventArgs e) 
{ 
    decimal price; 
    if (products.TryGetValue(descriptionTextBox.Text, out price)) 
    { 
     priceTextBox.Text = price.ToString(); 
    } 
    else 
    { 
     priceTextBox.Text = string.Empty; 
    } 
} 
関連する問題