Hi Firuz,
I have made a sample that fill-fill your requirement.
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("PINFO", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SURNAME", txtSurname.Text.Trim());
cmd.Parameters.AddWithValue("@NAM", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@LASTNAME", txtLastName.Text.Trim());
cmd.Parameters.AddWithValue("@BIRTHDAY", txtBirthday.Text);
cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text.Trim());
try
{
cmd.ExecuteNonQuery();
this.GetData();
con.Close();
}
catch
{
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["emp"];
if (dt.Rows.Count > 0)
{
int firstIndex = 0;
txtSurname.Text = dt.Rows[firstIndex]["SURNAME"].ToString();
txtName.Text = dt.Rows[firstIndex]["NAM"].ToString();
txtLastName.Text = dt.Rows[firstIndex]["LASTNAME"].ToString();
txtBirthday.Text = dt.Rows[firstIndex]["BIRTHDAY"].ToString();
txtAddress.Text = dt.Rows[firstIndex]["ADDRESS"].ToString();
Session["CurrentIndex"] = firstIndex;
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["emp"];
int currentIndex = Convert.ToInt32(Session["CurrentIndex"]);
if (currentIndex == dt.Rows.Count - 1)
{
currentIndex = 0;
txtSurname.Text = dt.Rows[currentIndex]["SURNAME"].ToString();
txtName.Text = dt.Rows[currentIndex]["NAM"].ToString();
txtLastName.Text = dt.Rows[currentIndex]["LASTNAME"].ToString();
txtBirthday.Text = dt.Rows[currentIndex]["BIRTHDAY"].ToString();
txtAddress.Text = dt.Rows[currentIndex]["ADDRESS"].ToString();
Session["CurrentIndex"] = currentIndex;
}
else if (dt.Rows.Count > 0)
{
txtSurname.Text = dt.Rows[currentIndex + 1]["SURNAME"].ToString();
txtName.Text = dt.Rows[currentIndex + 1]["NAM"].ToString();
txtLastName.Text = dt.Rows[currentIndex + 1]["LASTNAME"].ToString();
txtBirthday.Text = dt.Rows[currentIndex + 1]["BIRTHDAY"].ToString();
txtAddress.Text = dt.Rows[currentIndex + 1]["ADDRESS"].ToString();
Session["CurrentIndex"] = currentIndex + 1;
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["emp"];
int currentIndex = Convert.ToInt32(Session["CurrentIndex"]);
if (currentIndex == 0)
{
currentIndex = dt.Rows.Count;
}
if (dt.Rows.Count > 0)
{
txtSurname.Text = dt.Rows[currentIndex - 1]["SURNAME"].ToString();
txtName.Text = dt.Rows[currentIndex - 1]["NAM"].ToString();
txtLastName.Text = dt.Rows[currentIndex - 1]["LASTNAME"].ToString();
txtBirthday.Text = dt.Rows[currentIndex - 1]["BIRTHDAY"].ToString();
txtAddress.Text = dt.Rows[currentIndex - 1]["ADDRESS"].ToString();
Session["CurrentIndex"] = currentIndex - 1;
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["emp"];
int lastIndex = dt.Rows.Count - 1;
txtSurname.Text = dt.Rows[lastIndex]["SURNAME"].ToString();
txtName.Text = dt.Rows[lastIndex]["NAM"].ToString();
txtLastName.Text = dt.Rows[lastIndex]["LASTNAME"].ToString();
txtBirthday.Text = dt.Rows[lastIndex]["BIRTHDAY"].ToString();
txtAddress.Text = dt.Rows[lastIndex]["ADDRESS"].ToString();
Session["CurrentIndex"] = lastIndex;
}
public void GetData()
{
string str = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("SELECT SURNAME,NAM,LASTNAME,BIRTHDAY,ADDRESS FROM INFO", con);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
//gvEmp.DataSource = ds.Tables["emp"];
//gvEmp.DataBind();
ViewState["emp"] = ds.Tables["emp"];
Session["CurrentIndex"] = 0;
}
Screenshot
Hope this will help you.