Data not transfering to another page
-
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.Cell
-
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.Cell
First and foremost, edit your post and remove the connection details. Its not so good to post your SQL server's sa password on forums even if it is CodeProject. First step to solving this would be to step through your code and identify if the dt that your are persisting in the session has any values. Debug the application, put a break point on the statement
Session["CheckedRecords"] = dt;
. Check if it has any values before going into the session. If it has values then on the 2nd page check what you are getting out of the session. Again put a break point on the lineDataTable dt = (DataTable)Session["CheckedRecords"];
and check the values in dt. Some suggestions [read free advice :-D] 1. Data table is an reference type. You need not return it fromAddNewRow
andRemoveRow
methods. The dt in the calling function and the dt local toAddNewRow
andRemoveRow
methods point to the same datatable in the heap. Doing this way would just increase the over head on the GC and reduce maintainability of your code. 2. In theAddNewRow
method, rather than doing "dt.Rows.Count - 1
" every time, do it once and store it as rowNum and use it subsequently. This reduces the number of execution cycles. 3. Format your code. Proper indentation improves readability. HTH! -
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.Cell
check the reply for your post on: http://opexsolution.com/forum/viewtopic.php?f=15&t=27