Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear friends i am sory if this is simple or silly question i wanted to know
how to display result in another page

for EX:

index.aspx will have textbox where we enter value and submit button
as soon we click on submit button it should goto result.aspx page and display result.

i want only that code part as i know about sql gridview and button event fire

so please help me with this problem thanks in advance


index.aspx (textbox and button)
result.aspx (result in grid view)

in default.aspx

ASP.NET
<form id="form1"  runat="server">
 <div>
    <asp:Label ID="Label1" runat="server" Text="ENTER YOUR STUDENT ID: " 
        Font-Bold="True" Font-Names="Times New Roman" Font-Size="Small">
        
    <asp:TextBox ID="TextBox1" runat="server">
        
    <asp:Button ID="Button1" runat="server" Text="SUBMIT" 
       PostBackUrl="~/result.aspx"/><br /><br />
    <asp:Label ID="Label2" runat="server" Text="error label">
 </div>
</form>

in result.aspx

ASP.NET
<form id="form1" runat="server">
          <div>
          <tr>
  <td class="body" border="0" cellspacing="5" cellpadding="5">
  <br />
      <asp:Label ID="Label3" runat="server" Text="student id" Font-Size="Small">
  <br /><br />
      <asp:Label ID="Label4" runat="server" Text="student name" Font-Size="Small">
  <br /><br />
      <asp:Label ID="Label5" runat="server" Text="student class" Font-Size="Small">
  <br /><br />
      <asp:Label ID="Label6" runat="server" Text="student result" Font-Size="Small">
  <br /><br />
</td>
          </tr>
              <asp:GridView ID="GridView1" runat="server">
                        
          <br /><br />
              <asp:Label ID="Label2" runat="server" Text="error label">
          </div>
          </form>

in result.cs

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            TextBox txt1 = (TextBox)(Page.PreviousPage.FindControl("TextBox1.text"));
        }
        BindFiles();
    }
    private void BindFiles()
    {
        DataTable table = new DataTable();

        // get the connection
        SqlConnection _conn1 = new SqlConnection(@"conn string");
        {
            // write the sql statement to execute
            string sql = "select * from studsub where sid=TextBox1";
            
            // instantiate the command object to fire
            using (SqlCommand cmd = new SqlCommand(sql, _conn1))
            {
                // get the adapter object and attach the command object to it
                using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                {
                    // fire Fill method to fetch the data and fill into DataTable
                    ad.Fill(table);
                }
                // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection
            }
        }
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}

please check my code and tell me where to put which code and what code has to put
Posted
Updated 19-Apr-12 13:08pm
v3
Comments
Nelek 19-Apr-12 19:09pm    
Added code tags
karthikh87 19-Apr-12 23:33pm    
please tell me where you added code in the above thanks in adv
Nelek 20-Apr-12 18:49pm    
I didn't add code, I added the tags. When you post a snippet, you should wrap them using the programming language it fits better, from the menu "code" on the message board (in the center, where "bold", "italic", "underline" and so on). I just added them to make your code more readable.

you can use response.redirect to transfer to new page & use a query string to pass resultant value to next page.
 
Share this answer
 
Comments
karthikh87 19-Apr-12 8:47am    
can i get code part for query string..? i dont know how to pass resultant value to next page please help me
karthikh87 19-Apr-12 14:05pm    
i have pasted my entire code so can u plz tel me where n what to put plz as i am not able to understand where to put your code
Hi,

You can use Session to send data from index.aspx to result.aspx page :

In index.aspx save result in session:
C#
Session["Result"] = txtResult.Text;


In result.aspx get result from session
C#
string Result = Session["Result"].ToString();
 
Share this answer
 
Comments
Sebastian T Xavier 19-Apr-12 9:16am    
session is too heavy when compared to query string.....
karthikh87 19-Apr-12 14:03pm    
i have pasted my entire code so can u plz tel me where n what to put plz as i am not able to understand where to put your code
Below is the sample to pass query string from index.aspx

put the below code in index.aspx button click event
C#
Reponse.Redirect("result.aspx?Value=" + textbox.text)


remove your below code from result.aspx Page Load event
C#
if(Page.PreviousPage != null)
{
TextBox txt1 = (TextBox)(Page.PreviousPage.FindControl("TextBox1"));
}



use my below code in result.aspx Page Load event



C#
if (Request.QueryString.Get("Value") != null)
       string strValue = (Request.QueryString.Get("Value"))
 
Share this answer
 
v5
Comments
karthikh87 19-Apr-12 14:03pm    
i have pasted my entire code so can u plz tel me where n what to put plz as i am not able to understand where to put your code
There is two way to solve it out

one way is

put session this page value

C#
ex-session["value"]=textbox.text;
and get it result page
On formload event
LableResult=session["value"].Tostring();


second is

by Querystring

C#
Response.Redirect("Result.aspx ? Result="+txtbox.text);


get it formload on Resultpage

C#
LableResult=Requist.Querystrin[0].Tostring();
 
Share this answer
 
v2
Comments
karthikh87 19-Apr-12 14:04pm    
i have pasted my entire code so can u plz tel me where n what to put plz as i am not able to understand where to put your code
You can also use Cross Page Posting.
Set PostBackUrl of Submit button to "result.aspx".
On Page load of result.aspx check
if(Page.PreviousPage != null)
{
TextBox txt1 = (TextBox)(Page.PreviousPage.FindControl("TextBox1"));
}

You can use txt1 for getting value of your textbox.
 
Share this answer
 
Comments
karthikh87 19-Apr-12 14:04pm    
i have pasted my entire code so can u plz tel me where n what to put plz as i am not able to understand where to put your code
Hi,

You can write following code:

In Default.aspx.cs:

Inside Button1 click event write following code to store Student id in session
C#
Session["StudentID"] = TextBox1.Text;


In Result.asps.cs:

In page load event following code to get StudentID you entered in Default.aspx page
C#
string StudentID = Session["StudentID"].ToString();
 
Share this answer
 
Hi..

U can store the result in session.

then pass into next page.

like below...
HttpCookie cName = new HttpCookie("Name");
       cName.Value = TextBox1.Text;
       Response.Cookies.Add(cName);



       Response.Redirect("next.aspx");



next ppage:

C#
if (Request.Cookies["Name"] != null)
            TextBox1.Text  = Request.Cookies["Name"].Value;
 
Share this answer
 
v2
in default.aspx
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("result.aspx?Value=" + TextBox1.Text);
    }

in result.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Get("Value") != null)
        {
            string studentid =Request.QueryString.Get("Value");
            BindFiles();
        }
    } 

    private void BindFiles()
    {
        DataTable table = new DataTable();
 
        // get the connection
        SqlConnection _conn1 = new SqlConnection(@"conn string");
        {
            // write the sql statement to execute
            string sql = "select * from studsub where sid=studentid ";
            
            // instantiate the command object to fire
            using (SqlCommand cmd = new SqlCommand(sql, _conn1))
            {
                // get the adapter object and attach the command object to it
                using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                {
                    // fire Fill method to fetch the data and fill into DataTable
                    ad.Fill(table);
                }
                // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection
            }
        }
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}
 
Share this answer
 
v2
you can put it form load event
 
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