DataAdapter
-
Hi, I develop a code for data manipulation i.e (first, next, previous and last record). i try the below mentioned code, In this, i click next button -- move the next record. i click one more time it doesnt move same as for previous button. protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); //SqlCommand cmd = new SqlCommand(); con.ConnectionString = "Data source=localhost;initial catalog=northwind;integrated security=true"; String cmd = "select * from employees"; //cmd.CommandType = CommandType.Text; //cmd.Connection = con; //cmd.Connection.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataSet ds = new DataSet(); da.Fill(ds, "employeeinfo"); dt = ds.Tables["employeeinfo"]; tot = dt.Rows.Count; Label5.Text = tot.ToString(); fill(); //SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //Label5.Text= Convert.ToString(dr.FieldCount); //while (dr.Read()) //{ // TextBox1.Text = dr["employeeid"].ToString(); // TextBox2.Text = dr["firstname"].ToString(); // TextBox3.Text = dr["city"].ToString(); // TextBox4.Text = dr["country"].ToString(); //} //Label5.Text=dr.RecordsAffected.ToString(); //GridView1.DataSource = dr; //GridView1.DataBind(); //cmd.Dispose(); //dr.Dispose(); } protected void Button6_Click(object sender, EventArgs e) { //First record cur = 0; fill(); Label5.Text = cur.ToString(); } protected void Prev_Click(object sender, EventArgs e) { //previous record cur--; if (cur < 0) cur = tot - 1; fill(); Label5.Text = cur.ToString(); } private void fill() { TextBox1.Text = dt.Rows[cur]["employeeid"].ToString(); TextBox2.Text = dt.Rows[cur]["firstname"].ToString(); TextBox3.Text = dt.Rows[cur]["city"].ToString(); TextBox4.Text = dt.Rows[cur]["country"].ToString(); Label5.Text = cur.ToString(); } protected void Button8_Click(object sender, EventArgs e) { if (cur < tot - 1) { cur = cur + 1; fill(); } // //Next record // cur = cur + 1;
-
Hi, I develop a code for data manipulation i.e (first, next, previous and last record). i try the below mentioned code, In this, i click next button -- move the next record. i click one more time it doesnt move same as for previous button. protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); //SqlCommand cmd = new SqlCommand(); con.ConnectionString = "Data source=localhost;initial catalog=northwind;integrated security=true"; String cmd = "select * from employees"; //cmd.CommandType = CommandType.Text; //cmd.Connection = con; //cmd.Connection.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataSet ds = new DataSet(); da.Fill(ds, "employeeinfo"); dt = ds.Tables["employeeinfo"]; tot = dt.Rows.Count; Label5.Text = tot.ToString(); fill(); //SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //Label5.Text= Convert.ToString(dr.FieldCount); //while (dr.Read()) //{ // TextBox1.Text = dr["employeeid"].ToString(); // TextBox2.Text = dr["firstname"].ToString(); // TextBox3.Text = dr["city"].ToString(); // TextBox4.Text = dr["country"].ToString(); //} //Label5.Text=dr.RecordsAffected.ToString(); //GridView1.DataSource = dr; //GridView1.DataBind(); //cmd.Dispose(); //dr.Dispose(); } protected void Button6_Click(object sender, EventArgs e) { //First record cur = 0; fill(); Label5.Text = cur.ToString(); } protected void Prev_Click(object sender, EventArgs e) { //previous record cur--; if (cur < 0) cur = tot - 1; fill(); Label5.Text = cur.ToString(); } private void fill() { TextBox1.Text = dt.Rows[cur]["employeeid"].ToString(); TextBox2.Text = dt.Rows[cur]["firstname"].ToString(); TextBox3.Text = dt.Rows[cur]["city"].ToString(); TextBox4.Text = dt.Rows[cur]["country"].ToString(); Label5.Text = cur.ToString(); } protected void Button8_Click(object sender, EventArgs e) { if (cur < tot - 1) { cur = cur + 1; fill(); } // //Next record // cur = cur + 1;
Murugavel Sadagopan wrote:
protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); //SqlCommand cmd = new SqlCommand(); con.ConnectionString = "Data source=localhost;initial catalog=northwind;integrated security=true"; String cmd = "select * from employees"; //cmd.CommandType = CommandType.Text; //cmd.Connection = con; //cmd.Connection.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataSet ds = new DataSet(); da.Fill(ds, "employeeinfo"); dt = ds.Tables["employeeinfo"]; tot = dt.Rows.Count; Label5.Text = tot.ToString(); fill();
Put your code in condition.You page load event everytime fill the data on every events. if(!isPostBack) { // Put your code here }
please don't forget to vote on the post that helped you.
-
Murugavel Sadagopan wrote:
protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); //SqlCommand cmd = new SqlCommand(); con.ConnectionString = "Data source=localhost;initial catalog=northwind;integrated security=true"; String cmd = "select * from employees"; //cmd.CommandType = CommandType.Text; //cmd.Connection = con; //cmd.Connection.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataSet ds = new DataSet(); da.Fill(ds, "employeeinfo"); dt = ds.Tables["employeeinfo"]; tot = dt.Rows.Count; Label5.Text = tot.ToString(); fill();
Put your code in condition.You page load event everytime fill the data on every events. if(!isPostBack) { // Put your code here }
please don't forget to vote on the post that helped you.
hi imran khan, still i have the same problem. thanks, murugavel