Hello all ,
i am new to this forum and also a beginner in ASP.NET.I searched the forum for posiible answers or hints about my issue but was unable to do so.
I am basically in my form trying to check a customer against the database and return or precisely autofill the relevant results in the relevant textboxes such as the customer address , cell no and email.
my autoextender works fine checking against the database but i am unable to make it work to reflect the same in the textboxes if at all they do exist or else leave the textboxes for the user to fill up.
The code for the same is given below :
><asp:TextBox ID="customer_name_tb" runat="server" OnTextChanged="customer_name_tb_TextChanged" Width="400px" AutoPostBack="True"></asp:TextBox><ajaxToolkit:AutoCompleteExtender ServiceMethod="Search_database" OnClientPopulated="dd" OnClientItemSelected="itemSelected" MinimumPrefixLength="2" BehaviorID="AutoCompleteEx" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="customer_name_tb" ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false" UseContextKey="True"></ajaxToolkit:AutoCompleteExtender><asp:HiddenField ID="hfCustomerId" runat="server" /><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="customer_name_tb" ErrorMessage="Customer Name is Required" ForeColor="Red" SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="customer_name_tb" ErrorMessage="Incorrect Name." ForeColor="Red" ValidationExpression="^[a-z A-Z 0-9]*$" Display="Dynamic"></asp:RegularExpressionValidator>
<script type="text/javascript"> function itemSelected(sender,ev) { var index = $find("AutoCompleteEx")._selectIndex; var value = $find("AutoCompleteEx").get_completionList().childNodes[index]._value;$find("AutoCompleteEx").get_element().value = value;$get("<%=hfCustomerId%>").value = ev.get_value(); } function dd() { var comletionList = $find("AutoCompleteEx").get_completionList(); for (i = 0; i < comletionList.childNodes.length; i++) { var itemobj = new Object(); var _data = comletionList.childNodes[i]._value; itemobj.name = _data.substring(_data.lastIndexOf('|') + 1); // parse name as item value comletionList.childNodes[i]._value = itemobj.name; _data = _data.substring(0, _data.lastIndexOf('|')); itemobj.age = _data.substring(_data.lastIndexOf('|') + 1); comletionList.childNodes[i].innerHTML = "<div style='float:right'>" + itemobj.name + "</div>" + "<div style='float:left'>" + itemobj.age + "</div><br/>"; } } </script>
[System.Web.Script.Services.ScriptMethod()] [System.Web.Services.WebMethod] public static List<string> Search_database(string prefixText, int count) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select Customer_id,Customer_name,Customer_address from Customer where Customer_name like + @SearchText + '%'"; cmd.Parameters.AddWithValue("@SearchText", prefixText); cmd.Connection = conn; conn.Open(); List<string> customers = new List<string>(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { customers.Add(sdr["Customer_id"].ToString() + "|" + sdr["Customer_name"].ToString() + "|" + sdr["Customer_address"].ToString()); } } conn.Close(); return customers; } } }
protected void customer_name_tb_TextChanged(object sender, EventArgs e) { string customerId = hfCustomerId.Value.ToString(); SqlCommand cmd = new SqlCommand(); cmd.Connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); cmd.Connection.Open(); cmd.CommandText = "select customer_address,customer_cell,customer_email from medico.dbo.Customer where customer_id='" + hfCustomerId + "'"; SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { while(dr.Read()) { customer_address_tb.Text = dr["customer_address"].ToString(); customer_cell_no_tb.Text = dr["customer_cell"].ToString(); customer_email_tb.Text = dr["customer_email"].ToString(); } } }
Please help.
Also it would be great if you could help with how i could check in the same function if the customer exists and that i need the customer to insert into the database ( if its a new customer ) beacuse i am doing this on the button_click of the form and thus its somewhat complicated.
i want to keep it clean and remove my inefficiency.
Thank You.