data not transfering from one page to other page in session
iam taking data from one page gridview to another page in session but it is not displaying in another page gridview
please check my code and give suggestion which helps me
string constr = "Data Source=MAINSERVER;Initial Catalog=Inventory;User ID=sa;Password=nsg_ss_0103";
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
GridView1.PageIndex = 0;
bindata();
}
}
private void bindata()
{
SqlConnection con12 = new SqlConnection(constr);
SqlDataAdapter da12 = new SqlDataAdapter("SELECT [CategoryNameE], [ItemKey], [ItemKeyNameE], [CurrentQTY], [SalesPrice] FROM [CurrentInWH]", con12);
DataSet ds = new DataSet();
da12.Fill(ds, "t");
GridView1.DataSource = ds.Tables["t"];
GridView1.DataBind();
}
private void GetGridViewData()
{
DataTable dt;
if(Session["CheckedRecords"]!=null)
//if (ViewState["CheckedRecords"] != null)
dt = (DataTable)Session["CheckedRecords"];
else dt = CreateNewTable();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddNewRow(GridView1.Rows[i], dt);
}
else {
dt = RemoveRow(GridView1.Rows[i], dt);
}
}
Session["CheckedRecords"] = dt;
}
private DataTable CreateNewTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("CategoryNameE");
dt.Columns.Add("ItemKey");
dt.Columns.Add("ItemKeyNameE");
dt.Columns.Add("CurrentQTY");
dt.Columns.Add("SalesPrice");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");
dt.AcceptChanges();
return dt;
}
private DataTable AddNewRow(GridViewRow gvrow, DataTable dt)
{
DataRow[] dr = dt.Select("ItemKey = '" + gvrow.Cells[2].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["CategoryNameE"] = gvrow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["ItemKey"] = gvrow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["ItemKeyNameE"] = gvrow.Cells[3].Text;
dt.Rows[dt.Rows.Count - 1]["CurrentQTY"] = gvrow.Cells[4].Text;
dt.Rows[dt.Rows.Count - 1]["SalesPrice"] = gvrow.Cells[5].Text;
dt.Rows[dt.Rows.Count - 1]["Quantity"] = gvrow.Cells[6].Text;
dt.Rows[dt.Rows.Count - 1]["Total"] = gvrow.Cells[7].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvrow, DataTable dt)
{
DataRow[] dr = dt.Select("ItemKey= '" + gvrow.Cells[2].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GetGridViewData();
TextBox txtqty1; TextBox txttott; CheckBox ch = null;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
txtqty1 = (TextBox)GridView1.Rows[i].FindControl("txtqty");
txttott = (TextBox)GridView1.Rows[i].FindControl("txttot");
ch = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (ch.Checked)
{
txtqty1.Visible = true;
txttott.Visible = true;
}
else {
txtqty1.Visible = false;
txttott.Visible = false;
}
}
}
// in second page
DataTable dt = (DataTable)Session["CheckedRecords"];
GridView1.DataSource = dt;
GridView1.DataBind();