Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds,

I want to read data from database and fill in textboxes.

I have 2 pages
Page1.aspx
Page2.aspx

In database I have Userdetails table. in which all the data is stored according to email address.

We need to retrieve data from database. when email entered and hits submit button.
Data must be read and displayed in page2.aspx

In page1.aspx i have textbox to enter emailaddress (TxtEmailAddress) and also submit button to view entered emailaddress details in page2.aspx.

In page2.aspx i have Textboxes to fill the data.

So, this is my code,

Please can you help me, how to read data from database according to enterd email address in page1.aspx

My code
=======
C#
SqlConnection conFill = new SqlConnection(_connString);
conFill.Open();
SqlCommand cmdFill = new SqlCommand("SELECT * from Userdetails", conFill);

SqlDataReader drFill = cmdFill.ExecuteReader();

            if (drFill.Read())
            {
                TxtFirstName.Text = drFill["FirstName"].ToString();
                TxtLastName.Text = drFill["LastName"].ToString();
                TxtDepartment.Text = drFill["Department"].ToString();
                TxtCompany.Text = drFill["Company"].ToString();
                TxtMobileNo.Text = drFill["MobileNo"].ToString();
                TxtOfficeTelNo.Text = drFill["OfficeTelNo"].ToString();
                TxtEmailAddress.Text = drFill["EmailAddress"].ToString();


            }


            else
            {
                TxtFirstName.Text = "";
                TxtLastName.Text = "";
                TxtDepartment.Text = "";
                TxtCompany.Text = "";
                TxtMobileNo.Text = "";
                TxtOfficeTelNo.Text = "";
                TxtEmailAddress.Text = "";

            }


Please help me. Thankyou.
Posted
Updated 19-Nov-14 0:16am
v3

very easy solution of this problem to use query string.

When ever in the page1 you are clicking the submit button redirect it to the page2.aspx with the inputted emailaddress. Like this

On button Click event

Response.Redirect("page2.aspx?email="+txtEmail.Text.Trim());


In the Page2.aspx Page_Load method grab the email and run the sql query.

C#
Page_Load()
{
  if(Request.QueryString["email"]!=null)
  {
    string email=Request.QueryString["email"].ToString();
    // Now run your query with this email. Or you can put this in another method
    // one line change in the following line.
    SqlCommand cmdFill = new SqlCommand("SELECT * from Userdetails where Email='"+email+"'", conFill);
    // write rest of the part of code
  }
}


You also can use session to do this but query string is better than session.
 
Share this answer
 
Comments
Arkadeep De 20-Nov-14 3:00am    
If the solution solved your problem then kindly accept the solution and make the question Solved.
You can use Query String..Page1.aspx
C#
Response.Redirect("page2.aspx?email="+txtEmail.Text.Trim());

or Session
C#
Session["UserEmail"]=txtEmail.text.tostring();


page2.aspx
C#
if(Session["UserEmail"]!=null && Session["UserEmail"].tostring()!="")
{
string Email=session["UserEmail"].tostring();
SqlConnection conFill = new SqlConnection(_connString);
conFill.Open();
SqlCommand cmdFill = new SqlCommand("SELECT * from Userdetails where UserEmail='"+Email+"'", conFill);
 
SqlDataReader drFill = cmdFill.ExecuteReader();
 
            if (drFill.Read())
            {
                TxtFirstName.Text = drFill["FirstName"].ToString();
                TxtLastName.Text = drFill["LastName"].ToString();
                TxtDepartment.Text = drFill["Department"].ToString();
                TxtCompany.Text = drFill["Company"].ToString();
                TxtMobileNo.Text = drFill["MobileNo"].ToString();
                TxtOfficeTelNo.Text = drFill["OfficeTelNo"].ToString();
                TxtEmailAddress.Text = drFill["EmailAddress"].ToString();
 

            }
 

            else
            {
                TxtFirstName.Text = "";
                TxtLastName.Text = "";
                TxtDepartment.Text = "";
                TxtCompany.Text = "";
                TxtMobileNo.Text = "";
                TxtOfficeTelNo.Text = "";
                TxtEmailAddress.Text = "";
 
            }
}
else
{
//redirect to the page1.aspx on session End
Response.Redirect("page1.aspx);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900