How to show database values on pageload in asp.net c#

In this article , I am going to show database value on textbox on pageload event.


protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Session["username"] != null)
        {
      
      if (!IsPostBack)
            {
               Displayinformation();
            }
        }
    }


protected void displayinformation ()
    {
        try
        {
            string username = Session["username"].ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
            con.Open();
            string newVariable = "select * from registration where Username ='" + username + "'";
            SqlCommand command = new SqlCommand(newVariable, con);
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {

                txtfirstname.Text = dr["firstname"].ToString();
                txtlastname.Text = dr["lastname"].ToString();
                ddlgender.Text = dr["gender"].ToString();
                txtemail.Text = dr["email"].ToString();
                txtdob.Text = dr["dob"].ToString();
                txtcurrentcity.Text = dr["currentcity"].ToString();
                ddlnation.Text = dr["nationality"].ToString();
                con.Close();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);

        }
    }



How to show database values on pageload in asp.net c# How to show database values on pageload in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 12:16:00 PM Rating: 5

No comments:

Powered by Blogger.