how to dropdown related data in textbox based on a another textbox value

61 Views Asked by At

In my Project, There are one or many Address_code are assigned to the particular Customer_Name. I have a one textbox Which hold Customer_Name. When I select particular Customer_Name which is populated using AutoCompleteExtender. Then I want to display Address_code related to that Customer_Name in the next Textbox.

Here is the code for select Customer, Which Works fine..

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = dbConnection.fnConnectionString();
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = " SELECT CustomerCode,CustomerName FROM tblCustomer where " +
            "CustomerName 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())
                {
                    String Code = sdr["CustomerCode"].ToString();
                    String Name = sdr["CustomerName"].ToString();
                    Name = Name + " ("+Code + ")";
                    customers.Add(Name);

                }
            }
            conn.Close();
            return customers;
        }
    }
}

Here is code to display Addresscode in the another Textbox..Which is not working..

 [System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchAddress(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = dbConnection.fnConnectionString();
        using (SqlCommand cmd = new SqlCommand())
        {

            cmd.CommandText = "select Addresscode from BName_Addresscode where Addresscode like '" + prefixText + "%' ";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers1 = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                   // String Code = sdr["City"].ToString();
                    String Name = sdr["Addresscode"].ToString();
                   // Name = Code + "(" + Name + ")";
                    customers1.Add(Name);

                }
            }
            conn.Close();
            return customers1;
        }
    }
}
1

There are 1 best solutions below

3
H.Mikhaeljan On

The parameter actually doesn't do anything and your code is open for sql injection.

The first part was correct where you select from tblCustomer.

Change to the way you used before and it should work correctly

cmd.CommandText = "select Addresscode from BName_Addresscode where Addresscode like '@SearchText%' ";
cmd.Parameters.AddWithValue("@SearchText", prefixText);