locked
dropdownlist item selected but textboxes are empties RRS feed

  • Question

  • User-2094959909 posted

    hello everyone,

    HTML code ===>

    <form id="form1" runat="server">
    <div>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" Height="17px" Width="249px">
    </asp:DropDownList>
    <br />
    <br />
    Name<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <br />
    Surname<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <br />
    Username<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <br />
    Password<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <br />
    Role<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

    </div>
    </form>

    behind page code ===>

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DropDownList1.Items.Add(new ListItem("--Select--", ""));
    DropDownList1.AppendDataBoundItems = true;
    String strConnString = ConfigurationManager
    .ConnectionStrings["parallelConnectionString"].ConnectionString;
    String strQuery = "select userid, name from tblusers";
    MySqlConnection con = new MySqlConnection(strConnString);
    MySqlCommand cmd = new MySqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
    con.Open();
    DropDownList1.DataSource = cmd.ExecuteReader();
    DropDownList1.DataTextField = "name";
    DropDownList1.DataValueField = "userid";
    DropDownList1.DataBind();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    con.Close();
    con.Dispose();
    }
    }
    }

    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
    String strConnString = ConfigurationManager
    .ConnectionStrings["parallelConnectionString"].ConnectionString;
    string strQuery = "select name, surname, username, password, role from tblusers where name = @name";
    MySqlConnection con = new MySqlConnection(strConnString);
    MySqlCommand cmd = new MySqlCommand();
    cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Value);
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;

    try
    {
    con.Open();
    MySqlDataReader sdr = cmd.ExecuteReader();

    while (sdr.Read())
    {
    TextBox1.Text = sdr[1].ToString();
    TextBox2.Text = sdr[2].ToString();
    TextBox3.Text = sdr[3].ToString();
    TextBox4.Text = sdr[4].ToString();
    TextBox5.Text = sdr[5].ToString();
    }
    }
    catch(Exception ex)
    {
    throw ex;
    }
    finally
    {
    con.Close();
    con.Dispose();
    }

    }

    -----------------------------------------------------

    when i run the code it shows the form and the dropdownlist is full of usernames but when an item is selected it doesn't give the other data of the item in the textboxes 

    if anyone can help please ?

    Thanks in advance.

    Wednesday, July 18, 2018 11:21 AM

Answers

  • User-1171043462 posted

    Problems

    1. You are binding UserId to Value in Name to Text in DropDown.
    2.  In SQL you are Passing Selected Value i.e. UserId to parameter while you are matching with Name field.

    cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Value);

    Change to

    cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 18, 2018 12:58 PM

All replies

  • User-369506445 posted

    hi

    it seems your code is <g class="gr_ gr_31 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="31" data-gr-id="31">correct ,</g> but maybe your query return null,

    please change

    while (sdr.Read())
    {
    TextBox1.Text = sdr[1].ToString();
    TextBox2.Text = sdr[2].ToString();
    TextBox3.Text = sdr[3].ToString();
    TextBox4.Text = sdr[4].ToString();
    TextBox5.Text = sdr[5].ToString();
    }

    to

     if (sdr.HasRows)
    
                {
    
                    while (sdr.Read())
                    {
                        TextBox1.Text = sdr[1].ToString();
                        TextBox2.Text = sdr[2].ToString();
                        TextBox3.Text = sdr[3].ToString();
                        TextBox4.Text = sdr[4].ToString();
                        TextBox5.Text = sdr[5].ToString();
                    }
                }
                else
                {
                    TextBox1.Text = "not found records";
                }

    Wednesday, July 18, 2018 11:44 AM
  • User-1171043462 posted

    Problems

    1. You are binding UserId to Value in Name to Text in DropDown.
    2.  In SQL you are Passing Selected Value i.e. UserId to parameter while you are matching with Name field.

    cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Value);

    Change to

    cmd.Parameters.AddWithValue("@name", DropDownList1.SelectedItem.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 18, 2018 12:58 PM
  • User-2094959909 posted
    Thank you mudassarkhan it worked verry well.
    Wednesday, July 18, 2018 1:26 PM
  • User-1171043462 posted

    My pleasure

    Wednesday, July 18, 2018 1:33 PM