2016-10-28 3 views
0

私の問題は、最初のテキストボックスに基づいてデータベースから2番目のテキストボックスを自動入力することです。SQLから引き出している最初のTextBoxに基づいて2番目のTextBoxを挿入する方法はありますか?

これはオートコンプリートTextBoxです。これは問題なく動作します。

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True"; 
     SqlConnection con = new SqlConnection(connectionString); 
     con.Open(); 
     string query = "SELECT Code FROM dbo.Liguanea_Lane"; 
     SqlCommand cmd = new SqlCommand(query, con); 

     SqlDataReader dr = cmd.ExecuteReader(); 
     AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection(); 
     while (dr.Read()) 
     { 
      mycollection.Add(dr.GetString(0)); 
     } 
     textBox1.AutoCompleteCustomSource = mycollection; 
     con.Close(); 
    } 
    catch (Exception ex) 
    {  
     MessageBox.Show(ex.ToString()); 
    }  
} 

これは、自動移入最初のTextBoxからの選択のベースと最初のテキストボックスに選択したオプションに基づいて、SQLからデータをフィルタリングします秒のTextBoxです。私がどこに間違っているのか分かりません。どうぞご覧ください:

private void textBox2_TextChanged(object sender, EventArgs e) 
{    
    try 
    { 
     string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True"; 
     SqlConnection con = new SqlConnection(connectionString); 
     con.Open(); 
     string query = "SELECT description FROM dbo.Liguanea_Lane where code= '" + textBox1.Text + "'"; // this query 
     SqlCommand cmd = new SqlCommand(query, con); 
     con.Close(); 
    } 
    catch (SqlException sql) 
    { 
     MessageBox.Show(sql.ToString()); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 
+0

TextBox1.Textchangedイベントごとにそのクエリを実行することはあまり賢明ではありません。クエリは常に同じで、テキストボックスに入力されたすべてのキーでTextChangedが実行されます。 – Steve

+0

これを以下で実行しましたが、まだ値は表示されません。以下はその下のコードです: SqlDataReader dr = cmd.ExecuteReader(); AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection(); con.Close(); } catch(SqlException sql) { MessageBox.Show(sql.ToString()); } catch(例外ex) { MessageBox.Show(ex.ToString()); } } – Jevon

答えて

0

私はこの問題を解決しました。私がやったことは、オートコンプリートのコンボボックスに切り替えることでした。私は、最初のSQLデータベース

void fillCombo() 
    { 
      try 
      { 

       string connectionString = "Data Source=JAVY26;Initial Catalog=Pharmacies;Integrated Security=True"; 
       SqlConnection con = new SqlConnection(connectionString); 
       con.Open(); 
       string query = "SELECT * FROM dbo.Liguanea_Lane"; 
       SqlCommand cmd = new SqlCommand(query, con); 

       SqlDataReader dr = cmd.ExecuteReader(); 
       while (dr.Read()) 
       { 
        string scode = dr.GetString(dr.GetOrdinal("code")); 
        comboBox2.Items.Add(scode); 
       } 
      } 
      catch (Exception ex) 
      { 

       MessageBox.Show(ex.ToString()); 
      }*/ 


    } 

からアイテムをそれぞれのコンボボックス移入fillComboメソッドを作成し、私は内「説明」欄の内容を格納する文字列変数を作成したコンボボックス内のコードを実装しSQLデータベースに格納され、次に、fillComboメソッドの選択に基づいて値を返すテキストボックスに割り当てられます。

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 

      string connectionString = "Data Source=JAVY26;Initial Catalog=Pharmacies;Integrated Security=True"; 
      string query = "SELECT * FROM dbo.Liguanea_Lane WHERE code = '" + comboBox2.Text + "' ; "; 
      SqlConnection con = new SqlConnection(connectionString); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand(query, con); 
      SqlDataReader dr = cmd.ExecuteReader(); 

      while (dr.Read()) 
      { 
       string sdes = dr.GetString(dr.GetOrdinal("description")); 
       textBox5.Text = sdes; 

      } 

     } 
     catch (Exception ex) 
     { 

      MessageBox.Show(ex.ToString()); 
     } 
関連する問題