简体   繁体   中英

How to display data from one page to another in gridview on button click by using session on asp.net

Thanx in advance. I'm facing issue in transfering data from gridview of home page to another search reasult page. page showing blank only.no data displaying. Im using with master page. Im trying to fetch data from textbox for search result like source and destination from textbox.

for refernce please find below code of home.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=IT_APPS_SUPP;Initial Catalog=dotnet;Integrated Security=True; MultipleActiveResultSets=true");
        con.Open();
        string str1 = "Select * from busbooking where zone='" + txtSourceBus.Text + "' " + "and destination='" + txtDestBus.Text + "'";
        SqlCommand cmd1 = new SqlCommand(str1, con);
        cmd1.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(str1, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        Label1.Text = "";          
        SqlDataReader dr = cmd1.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();

            GridView1.Visible = true;
            dr.Close();
        }
        else
        {
            GridView1.Visible = true;
            Label1.Text = "Data not  found";
        }

        DataTable dt = GridView1.DataSource as DataTable;//set the datasource
        Session["GridData"] = dt;
        Response.Redirect("~/BusSearch.aspx",true);
    }

=========================================================

bussearch.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["GridData"] != null)
            {
                DataTable dt = (DataTable)Session["GridData"];
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }

================================================================== can anyone help for this., my second page showing blank only.

Try passing DataSet instead of DataTable like this

Session["GridData"] = ds.Tables[0];

All the other code can stay as it is.

Hello you can use like this In bussearch.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["GridData"] != null)
            {
                DataTable dt = new DataTable();
                dt = Session["GridData"] as DataTable;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM