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());
}
}
TextBox1.Textchangedイベントごとにそのクエリを実行することはあまり賢明ではありません。クエリは常に同じで、テキストボックスに入力されたすべてのキーでTextChangedが実行されます。 – Steve
これを以下で実行しましたが、まだ値は表示されません。以下はその下のコードです: SqlDataReader dr = cmd.ExecuteReader(); AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection(); con.Close(); } catch(SqlException sql) { MessageBox.Show(sql.ToString()); } catch(例外ex) { MessageBox.Show(ex.ToString()); } } – Jevon