良い一人ひとり。sqlからテキストボックスにオートコンプリートC#
私の質問は、別のテキストボックスに入力された特定のデータに基づいて別のテキストボックスに自動的に入力する方法です。さらに説明すると、私の最初のテキストボックスは、 "code"という名前のSQLテーブルから自動完成します。その列の横に「説明」と呼ばれるものがあります。コード列からそのデータが取り込まれたことに基づいて、最初のテキストボックスで選択された値に基づいて2番目のテキストボックスを自動入力するにはどうすればよいですか?私は本当に意味があると思っています。
これは、TextBox1テキストボックスを更新している私が持っているコードで、これが正常に動作している:
private void liguaneaRxToolStripMenuItem_Click(object sender, EventArgs e)
{
this.liguanea_LaneTableAdapter1.Fill(this.pharmaciesDataSet1.Liguanea_Lane);
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());
}
}
今、これは私がテキストボックスから選択した値に基づいて値を記入したいTextBox2をするコードです例えば、そうです。私が書いているクエリでは、 "SELECT description FROM dbo.Liguanea_Lane where code ="どんな値でも "どこの値がtextbox1から収集された入力であり、textbox2に添付されている説明を返す(移入する)
private void displayValIntoTextbox(string val) //this function will auto complete the other textbox
{
if (val == null) //this will check if the value is null
{
MessageBox.Show("please enter a the correct code");
}
else
{
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= '"+val +"'"; // this query
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
if (dr.HasRows) // check if any info exist in database base off the query
{
while (dr.Read())
{
mycollection.Add(dr.GetString(0));
}
textBox1.AutoCompleteCustomSource = mycollection;
}
else
{
MessageBox.Show(val);
}
con.Close();
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
私はああ、それはまた、Winフォームアプリケーション
displayValIntoTextbox(文字列valは)その後、第2テキストボックス即ちTextBox2をするためのものである場合、この関数はtextBox2.AutoCompleteCustomSource = mycollectionを有するべきです。 –
でも問題は解決していません。何も起こっていません –
私はdisplayBoxIntoTextbox(string val)をtextbox1 text changedイベントから呼び出すことを希望します。 –