Answered by:
where do I write code to display data from database in page1

Question
-
User186897 posted
Hello All,
I have the code on Page1, after clicking submit it enters data to database and goes to page2...now from page2 there is a button"previous" if I clikc that it comes back to page1 but I want page1 textboxes to be populated with the datafrom the database...how do I do that? I know I can populate it using a select query but where do I write the code in pageload of page1 if yes how does it know that it can has come page2?
any examples please?
Wednesday, May 18, 2016 12:29 PM
Answers
-
User-1780421697 posted
You need to store data of form1 in Session before moving to Page2 and then retrive it from Session.
In Page1 Load event check the session value if it is not null then populate textboxes.
If(!IsPostBack) { //Use Try catch block txtBox.Text = Session["Value1"].ToString() }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 19, 2016 9:27 AM
All replies
-
User186897 posted
I have the below code but it doesnt display the data when i go to next page and come back...can someone plz tell me whats wrong with it
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString Dim con As New SqlConnection(strConnString) con.Open() Dim myReader As SqlDataReader = Nothing Dim myCommand As SqlCommand = New SqlCommand("select * from TMain where ID='" + Label1.Text + "'", con) myReader = myCommand.ExecuteReader() While myReader.Read() TextBox10.Text = (myReader("CTMName").ToString()) TextBox11.Text = (myReader("CTMEmail").ToString()) TextBox12.Text = (myReader("CTMPhone").ToString()) End While con.Close()
Wednesday, May 18, 2016 12:50 PM -
User-1672470423 posted
You can do that in several ways:
1. When you are redirecting back from page2, simply add page1.aspx?page=2
and in page1 pageload, you can check
if (!IsPostBack)
{
if (Request.QueryString["page"] != null)
{
// Query string value is there so now use it
int thePID = Convert.ToInt32(Request.QueryString["page"]);
if(thePID == 2)
{
PopulateData();
}
}
}
2. You can set Session["Page"] =2 before redirection from page2 and check that in page1
if (!IsPostBack)
{
if (Session["page"] != null)
{
// Query string value is there so now use it
int thePID = Convert.ToInt32(Session["page"]);
if(thePID == 2)
{
PopulateData();
Session["page"] = null;
}
}
}put your code to populate controls in PopulateData method.
Wednesday, May 18, 2016 2:02 PM -
User186897 posted
Hello I tried the above method of urs but still doesnt work
, below is my code
Protected Sub Button5_Click(sender As Object, e As EventArgs) Response.Redirect("Webform1.aspx?page=2") End Sub
If Not IsPostBack Then If Not Request.QueryString("page") Is Nothing Then Dim thePID As Integer = Convert.ToInt32(Request.QueryString("page")) If thePID = 2 Then Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString Dim con As New SqlConnection(strConnString) con.Open() Dim myReader As SqlDataReader = Nothing Dim myCommand As SqlCommand = New SqlCommand("select CTMName,CTMEmail,CTMPhone from TMain where ID='" + Label11.Text + "'", con) myReader = myCommand.ExecuteReader() While myReader.Read() TextBox10.Text = (myReader("CTMName").ToString()) TextBox11.Text = (myReader("CTMEmail").ToString()) TextBox12.Text = (myReader("CTMPhone").ToString()) End While con.Close() End If End If End If
Thursday, May 19, 2016 6:55 AM -
User36583972 posted
Hi Lexi85,
From your description, I found your the main problem is how to deliver value to the page 1, then you can display data from database in page1.
The following way for your reference.
1: Using a Query String
2: Getting Post Information from the Source Page
3: Using Session State
4: Getting Public Property Values from the Source Page
5: Getting Control Information from the Source Page in the Same Application
More detailed information in the below.
Pass Values Between ASP.NET Web Forms Pages:
https://msdn.microsoft.com/en-us/library/6c3yckfw.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3
Best Regards,
Yohann Lu
Thursday, May 19, 2016 7:59 AM -
User186897 posted
thanks, just wanted to know would it be not easier on pagelog of webform1 i say if past is postbac then get the data from database and display in the text boxes???
I know about this idea but now sure how to implement it, any examples or link which get the data from database and populate textboxes ??
Thursday, May 19, 2016 8:46 AM -
User-1780421697 posted
You need to store data of form1 in Session before moving to Page2 and then retrive it from Session.
In Page1 Load event check the session value if it is not null then populate textboxes.
If(!IsPostBack) { //Use Try catch block txtBox.Text = Session["Value1"].ToString() }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 19, 2016 9:27 AM -
User36583972 posted
Hi Lexi85
I suggest you can debug your code step by step. You will find which code may cause your problem.
Firstly, you should make sure you have received data form page2.
I think, when your page1 load. The controls in page1 values are empty. So, your following code in SQL command is incorrect. Because the condition is empty, the result of the query is empty. So, It doesn't display the data in page1 controls. You can get the condition values from page2.
Dim myCommand As SqlCommand = New SqlCommand("select CTMName,CTMEmail,CTMPhone from TMain where ID='" + Label11.Text + "'", con)
Best Regards,
Yohann Lu
Thursday, May 19, 2016 10:34 AM -
User-1672470423 posted
You mean to say in Webform1.aspx page_load nothing is there in Request.QueryString("page") when you redirect from other page. Strange.
Did you try the session approach ?
Thursday, May 19, 2016 1:26 PM -
User-1672470423 posted
I hope your problem is resolved. Please let us know if not.
Friday, May 20, 2016 4:52 PM