You must Sign In to post a response.
  • Category: .NET

    How to retrieve value from database to textbox after checkbox is checked

    hi all
    i have two textboxes and one checkbox

    if i enter id no in textbox 1 and i check checkbox value of first name from database should be displayed in textbox 2
    how to do it plz help me or give me any examples
    i have written like this but not working

    protected void FirstName_CheckedChanged(object sender, EventArgs e)
    {

    string cs = "Data Source=HOME;Initial Catalog=NoticeBoard;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("select FirstName from [notice board] where Password ='" + TextBox1.Text + "'", con);
    TextBox2.Text = Convert.ToString(cmd);
    con.Open();
    cmd.ExecuteScalar();
    con.Close();


    }
  • #763658

    Hi,

    modify the below lines of code

    SqlCommand cmd = new SqlCommand("select FirstName from [notice board] where Password ='" + TextBox1.Text + "'", con);
    TextBox2.Text = Convert.ToString(cmd);


    with this

    SqlCommand cmd = new SqlCommand("select FirstName from [notice board] where Password ='" + TextBox1.Text + "'", con);
    SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataSet ds=new DataSet();
    da.Fill(ds);
    if(ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
    TextBox2.Text = ds.Tables[0].Rows[0].ToString();


    If you have any doubts please let me know...


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763669

    Hai VELIDIRAVIRAM,
    You can see you have done mistakes in your code. ExecuteScalar returns and object but you are directly assigning the cmd to your textbox.
    The correct code should be:

    SqlCommand cmd = new SqlCommand("select FirstName from [notice board] where Password ='" + TextBox1.Text + "'", con);
    con.Open();
    var result = cmd.ExecuteScalar();
    con.Close();
    TextBox2.Text = result.ToString();

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments