Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to preserve the variable

how to preserve the variable

Scheduled Pinned Locked Moved ASP.NET
databasehelpdesignsysadmintutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swtlibra
    wrote on last edited by
    #1

    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();

    R D 2 Replies Last reply
    0
    • S swtlibra

      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();

      R Offline
      R Offline
      Robert_Pan
      wrote on last edited by
      #2

      Application

      S 1 Reply Last reply
      0
      • R Robert_Pan

        Application

        S Offline
        S Offline
        swtlibra
        wrote on last edited by
        #3

        C#.net application

        1 Reply Last reply
        0
        • S swtlibra

          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();

          D Offline
          D Offline
          droth17
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups