2016-10-27 4 views
0

良い一人ひとり。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フォームアプリケーション

+1

displayValIntoTextbox(文字列valは)その後、第2テキストボックス即ちTextBox2をするためのものである場合、この関数はtextBox2.AutoCompleteCustomSource = mycollectionを有するべきです。 –

+0

でも問題は解決していません。何も起こっていません –

+0

私はdisplayBoxIntoTextbox(string val)をtextbox1 text changedイベントから呼び出すことを希望します。 –

答えて

0

idを持つ 'txtAutoComplete'

<asp:TextBox ID="txtSearch" runat="server" /> 
<asp:HiddenField ID="hfCustomerId" runat="server" /> 
をTextBoxコントロールを作成するDBからの自動完全なテキストボックスの場合this-

を試してみてくださいです。誰もが、次のを願って

その後、アヤックス

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script> 

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script> 

のためのjQueryプラグインを追加し、データベースから名前のリストを取得するにはC#のコードを記述し、Default.aspxの/ GetCustomers方法でオートコンプリート

$(function() { 
    $("[id$=txtSearch]").autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>', 
       data: "{ 'prefix': '" + request.term + "'}", 
       dataType: "json", 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        response($.map(data.d, function (item) { 
         return { 
          label: item.split('-')[0], 
          val: item.split('-')[1] 
         } 
        })) 
       }, 
       error: function (response) { 
        alert(response.responseText); 
       }, 
       failure: function (response) { 
        alert(response.responseText); 
       } 
      }); 
     }, 
     select: function (e, i) { 
      $("[id$=hfCustomerId]").val(i.item.val); 
     }, 
     minLength: 1 
    }); 
}); 

ために、このAjaxのスクリプトを追加します。

CSSのこのスタイルシートの参照を追加

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css" 
rel="Stylesheet" type="text/css" /> 
関連する問題