I am using ASP.NET(web form) with C#.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Simpleweb
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button show;
protected System.Web.UI.WebControls.Button last;
protected System.Web.UI.WebControls.Button next;
protected System.Web.UI.WebControls.Button prev;
protected System.Web.UI.WebControls.Button first;
protected System.Web.UI.WebControls.Button delete;
protected System.Web.UI.WebControls.Button update;
protected System.Web.UI.WebControls.LinkButton clear;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button add;
SqlConnection cn;
SqlDataAdapter daadapter;
SqlCommandBuilder cmdb;
DataTable dt;
int position;
DataRow dr;
private void Page_Load(object sender, System.EventArgs e)
{
cn=new SqlConnection("user id=sa;password=sa;server=raja;database=practice");
cn.Open();
//cmd = new SqlCommand("SELECT uid,uname,upassword,umailid FROM userdetails",cn);
daadapter = new SqlDataAdapter("Select * From userdetails",cn);
cmdb = new SqlCommandBuilder(daadapter);
dt = new DataTable();
//position=0;
daadapter.Fill(dt);
dr = dt.Rows[0];
//SqlCommand cmd = new SqlCommand("SELECT uid,uname,upassword,umailid FROM userdetails",cn);
//SqlDataAdapter daadapter = new SqlDataAdapter("Select * From userdetails",cn);
//SqlCommandBuilder cmdb = new SqlCommandBuilder(daadapter);
//DataTable dt = new DataTable();
//daadapter.Fill(dt);
//DataRow dr = dt.Rows[0];
}
private void ShowCurrentRecord()
{
// The offending code here.
if (dt.Rows.Count==0)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
return;
}
TextBox1.Text = dt.Rows[position]["uid"].ToString();
TextBox2.Text = dt.Rows[position]["uname"].ToString();
TextBox3.Text = dt.Rows[position]["upassword"].ToString();
TextBox4.Text = dt.Rows[position]["umailid"].ToString();
return;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.clear.Click += new System.EventHandler(this.clear_Click);
this.show.Click += new System.EventHandler(this.show_Click);
this.last.Click += new System.EventHandler(this.last_Click);
this.next.Click += new System.EventHandler(this.next_Click);
this.prev.Click += new System.EventHandler(this.prev_Click);
this.first.Click += new System.EventHandler(this.first_Click);
this.delete.Click += new System.EventHandler(this.delete_Click);
this.update.Click += new System.EventHandler(this.update_Click);
this.add.Click += new System.EventHandler(this.add_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void show_Click(object sender, System.EventArgs e)
{
this.ShowCurrentRecord();
}
private void clear_Click(object sender, System.EventArgs e)
{
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
}
private void add_Click(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("user id=sa;password=sa;server=raja;database=practice");
cn.Open();
SqlCommand da=new SqlCommand("INSERT INTO userdetails(uname,upassword,umailid) VALUES ('"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')",cn);
da.ExecuteNonQuery();
cn.Close();
}
private void update_Click(object sender, System.EventArgs e)
{
if (TextBox2.Text=="")
{
Label5.Text="Please enter the name";
}
else if (TextBox3.Text=="")
{
Label5.Text="Please enter the Password";
}
else if (TextBox4.Text=="")
{
Label5.Text="Please enter your EmailId";
}
else
{
Label5.Text="";
SqlConnection cn=new SqlConnection("user id=sa;password=sa;server=raja;database=practice");
cn.Open();
SqlCommand cmd=new SqlCommand("UPDATE userdetails SET uname='"+TextBox2.Text+"',upassword='"+TextBox3.Text+"',umailid='"+TextBox4.Text+"' where uid='"+TextBox1.Text+"'",cn);
cmd.ExecuteNonQuery();
cn.Close();
}
}
private void delete_Click(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("user id=sa;password=sa;server=raja;database=practice");
cn.Open();
SqlCommand cmd=new SqlCommand("DELETE FROM userdetails WHERE uid='"+TextBox1.Text+"'",cn);
cmd.ExecuteNonQuery();
cn.Close();
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
}
private void first_Click(object sender, System.EventArgs e)
{
this.ShowCurrentRecord();
}
private void prev_Click(object sender, System.EventArgs e)
{
Response.Write(position);
if (position > 0)
{
position = position-1;
this.ShowCurrentRecord();
}
}
private void next_Click(object sender, System.EventArgs e)
{
Response.Write(position);
if (position < dt.Rows.Count-1)
{
position = position + 1;
this.ShowCurrentRecord();
}
}
private void last_Click(object sender, System.EventArgs e)
{
Response.Write(position);
if ((dt.Rows.Count) !=0)
{
position = dt.Rows.Count-1;
this.ShowCurrentRecord();
}
}
}
}
In the above code i have the problem of
i can use the first,previous,next,last buttons in the web form.when i am click on first and last buttons then it shows the correct records.but when i click on prev,next buttons then it take position=0,every time the position value is set to '0' only.
Please help me. what is the problem in it.?