Sunday, 1 May 2011

First-Next-Previous-Last Records in a Textboxes using Windows Application


First-Next-Previous-Last in a Textboxes using Windows Application:-



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int i;
        SqlConnection cn;
        SqlDataAdapter da;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Last Button
            i = ds.Tables[0].Rows.Count - 1;
            textBox1.Text = ds.Tables[0].Rows[i]["emp_id"].ToString();
            textBox2.Text = ds.Tables[0].Rows[i]["emp_name"].ToString();
            textBox3.Text = ds.Tables[0].Rows[i]["sub"].ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Clear Button
            textBox1.Clear();
            textBox2.Text = "";
            textBox3.Text = string.Empty;
        }

        public void bind_data_grid()
        {
            try
            {
                cn = new SqlConnection("Password=sa123;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=COMP-2");
                if (cn.State == ConnectionState.Closed)
                {
                    cn.Open();
                    da = new SqlDataAdapter("select * from r11", cn);
                    ds = new DataSet();
                    da.Fill(ds, "r11");
                    dataGrid1.DataSource = ds.Tables["r11"];
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {          
       
          
       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //First Button
            if (ds.Tables[0].Rows.Count > 0)
            {
                i = 0;
                textBox1.Text = ds.Tables[0].Rows[i]["emp_id"].ToString();
                textBox2.Text = ds.Tables[0].Rows[i]["emp_name"].ToString();
                textBox3.Text = ds.Tables[0].Rows[i]["sub"].ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Next Button
            if (i < ds.Tables[0].Rows.Count - 1)
            {
                i++;
                textBox1.Text = ds.Tables[0].Rows[i]["emp_id"].ToString();
                textBox2.Text = ds.Tables[0].Rows[i]["emp_name"].ToString();
                textBox3.Text = ds.Tables[0].Rows[i]["sub"].ToString();
            }
            else
            {
                MessageBox.Show("NO Next Records");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //Previous Button
            if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
            {
                i--;
                textBox1.Text = ds.Tables[0].Rows[i]["emp_id"].ToString();
                textBox2.Text = ds.Tables[0].Rows[i]["emp_name"].ToString();
                textBox3.Text = ds.Tables[0].Rows[i]["sub"].ToString();
            }
            else
            {
                MessageBox.Show("NO Previous Records");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Show Button
            bind_data_grid();
        }
    }
}

1 comment:

  1. you have assigned i only in a event than how can you access that variable into another event. Your code will not work until you assign "i" globally.
    Now after assigning the "i" globally you will face so many issues like if you have pressed Next button than the value of the "i" will remain same in this case your previous button will generate exception because of the row number is not exist in the database.

    ReplyDelete