how to preserve the variable
-
i have a code for moving to next record the problem is that i m using a variable named as rowIndex for increment and it naviagtes only through two records i think it refreshes with every postback so can anyone tell me how to preserve it????
public partial class sample : System.Web.UI.Page { OleDbConnection cn; OleDbCommand cd; OleDbDataReader rd; OleDbDataAdapter da; int rowIndex; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = GetData(); if (dt.Rows.Count > 0) { // Populate the TextBox with the first entry on page load TextBox1.Text = dt.Rows[0]["Employee_Name"].ToString(); //Then we store the DataTable in Session so that we will NOT //query the DB on every postbacks Session["dt"] = dt; } } } private DataTable GetData() { DataTable dt = new DataTable(); cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/employeedb.mdb")); try { cn.Open(); cd = new OleDbCommand("Select * from emptbl", cn); //SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); OleDbDataAdapter da = new OleDbDataAdapter(cd); da.Fill(dt); } catch (System.Data.OleDb.OleDbException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { cn.Close(); } return dt; } protected void Button1_Click(object sender, EventArgs e) { //int rowIndex = 0; rowIndex = +6; TextBox1.Text = rowIndex.ToString(); /*if (Session["dt"] != null) { DataTable dt = (DataTable)Session["dt"]; if (rowIndex <= dt.Rows.Count) { //get the next row entry on Button Click by setting the Row Index TextBox1.Text = dt.Rows[rowIndex]["Employee_Name"].ToString();
-
i have a code for moving to next record the problem is that i m using a variable named as rowIndex for increment and it naviagtes only through two records i think it refreshes with every postback so can anyone tell me how to preserve it????
public partial class sample : System.Web.UI.Page { OleDbConnection cn; OleDbCommand cd; OleDbDataReader rd; OleDbDataAdapter da; int rowIndex; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = GetData(); if (dt.Rows.Count > 0) { // Populate the TextBox with the first entry on page load TextBox1.Text = dt.Rows[0]["Employee_Name"].ToString(); //Then we store the DataTable in Session so that we will NOT //query the DB on every postbacks Session["dt"] = dt; } } } private DataTable GetData() { DataTable dt = new DataTable(); cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/employeedb.mdb")); try { cn.Open(); cd = new OleDbCommand("Select * from emptbl", cn); //SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); OleDbDataAdapter da = new OleDbDataAdapter(cd); da.Fill(dt); } catch (System.Data.OleDb.OleDbException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { cn.Close(); } return dt; } protected void Button1_Click(object sender, EventArgs e) { //int rowIndex = 0; rowIndex = +6; TextBox1.Text = rowIndex.ToString(); /*if (Session["dt"] != null) { DataTable dt = (DataTable)Session["dt"]; if (rowIndex <= dt.Rows.Count) { //get the next row entry on Button Click by setting the Row Index TextBox1.Text = dt.Rows[rowIndex]["Employee_Name"].ToString();
Application
-
Application
-
i have a code for moving to next record the problem is that i m using a variable named as rowIndex for increment and it naviagtes only through two records i think it refreshes with every postback so can anyone tell me how to preserve it????
public partial class sample : System.Web.UI.Page { OleDbConnection cn; OleDbCommand cd; OleDbDataReader rd; OleDbDataAdapter da; int rowIndex; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = GetData(); if (dt.Rows.Count > 0) { // Populate the TextBox with the first entry on page load TextBox1.Text = dt.Rows[0]["Employee_Name"].ToString(); //Then we store the DataTable in Session so that we will NOT //query the DB on every postbacks Session["dt"] = dt; } } } private DataTable GetData() { DataTable dt = new DataTable(); cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/employeedb.mdb")); try { cn.Open(); cd = new OleDbCommand("Select * from emptbl", cn); //SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); OleDbDataAdapter da = new OleDbDataAdapter(cd); da.Fill(dt); } catch (System.Data.OleDb.OleDbException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { cn.Close(); } return dt; } protected void Button1_Click(object sender, EventArgs e) { //int rowIndex = 0; rowIndex = +6; TextBox1.Text = rowIndex.ToString(); /*if (Session["dt"] != null) { DataTable dt = (DataTable)Session["dt"]; if (rowIndex <= dt.Rows.Count) { //get the next row entry on Button Click by setting the Row Index TextBox1.Text = dt.Rows[rowIndex]["Employee_Name"].ToString();
Do one of three things and then repopulate the variable when it reloads. 1. Add it to the viewstate 2. Add it to the Session 3. Add it to a hidden form field I usually just do something like this
Public Property VariableID() As Integer
Get
Return ViewState("VariableID")
End Get
Set(ByVal value As Integer)
ViewState("VariableID") = value
End Set
End Property