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. General Programming
  3. C#
  4. navigation through records

navigation through records

Scheduled Pinned Locked Moved C#
databasecsharpdesignsysadminhelp
5 Posts 2 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 am using Access database with C#.net i have the following code for moving through the records on a button click but its only going to the second record not more than that plz can any one tell the reason 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++; 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(); } } } }

    N 1 Reply Last reply
    0
    • S swtlibra

      i am using Access database with C#.net i have the following code for moving through the records on a button click but its only going to the second record not more than that plz can any one tell the reason 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++; 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(); } } } }

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      When page refreshes each time, the variable rowIndex gets cleared. That's how ASP.NET works. You need to persist the variable's value in ViewState or use some other persisting mechanisms. BTW, we have an ASP.NET forum and it is recommended to post questions related to ASP.NET there.

      Navaneeth How to use google | Ask smart questions

      S 1 Reply Last reply
      0
      • N N a v a n e e t h

        When page refreshes each time, the variable rowIndex gets cleared. That's how ASP.NET works. You need to persist the variable's value in ViewState or use some other persisting mechanisms. BTW, we have an ASP.NET forum and it is recommended to post questions related to ASP.NET there.

        Navaneeth How to use google | Ask smart questions

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

        thnks for ur reply any idea how to preserve the value ?? next time i would better move to the ASP.net forums :rolleyes:

        N 1 Reply Last reply
        0
        • S swtlibra

          thnks for ur reply any idea how to preserve the value ?? next time i would better move to the ASP.net forums :rolleyes:

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          As I said keep it in a viewstate.

          Navaneeth How to use google | Ask smart questions

          S 1 Reply Last reply
          0
          • N N a v a n e e t h

            As I said keep it in a viewstate.

            Navaneeth How to use google | Ask smart questions

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

            thank you so much for ur idea i m a new to C#.net it helped me a lot :) :-D :cool::thumbsup:

            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