navigation through records
-
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(); } } } }
-
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(); } } } }
When page refreshes each time, the variable
rowIndex
gets cleared. That's how ASP.NET works. You need to persist the variable's value inViewState
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
-
When page refreshes each time, the variable
rowIndex
gets cleared. That's how ASP.NET works. You need to persist the variable's value inViewState
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
-
thnks for ur reply any idea how to preserve the value ?? next time i would better move to the ASP.net forums :rolleyes:
As I said keep it in a viewstate.
Navaneeth How to use google | Ask smart questions
-
As I said keep it in a viewstate.
Navaneeth How to use google | Ask smart questions