Thursday, 18 April 2013

Add a new form in the project and design it as following






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
namespace WindowsFormsApp
{
    public partial class Form7 : Form
    {
        SqlConnection con;
        SqlCommandBuilder cb;
        DataSet ds;
        int rno = 0;
        SqlDataAdapter da;

        public Form7()
        {
            InitializeComponent();
        }

        private void Form7_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true");
            da = new SqlDataAdapter("Select Eno,Ename,Job,Salary from Employee Order By Eno", con);
            ds = new DataSet();
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            da.Fill(ds, "Employee");
            ShowData();
        }
        public void ShowData()
        {
            txteno.Text = ds.Tables[0].Rows[rno][0].ToString();
            txtname.Text = ds.Tables[0].Rows[rno][1].ToString();
            txtjob.Text = ds.Tables[0].Rows[rno][2].ToString();
            txtsalary.Text = ds.Tables[0].Rows[rno][3].ToString();
        }

        private void btnfirst_Click(object sender, EventArgs e)
        {
            rno = 0;
            ShowData();
        }

        private void btnprev_Click(object sender, EventArgs e)
        {
            if (rno > 0)
            {
                rno -= 1; ;
                if (ds.Tables[0].Rows[rno].RowState == DataRowState.Deleted)
                {
                    MessageBox.Show("Deleted row cannot be accessed.");
                    return;
                }
                ShowData();
            }
            else MessageBox.Show("First record of table");
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            if (rno 0)
            {
                int Eno = int.Parse(value);
                DataRow dr = ds.Tables[0].Rows.Find(Eno);
                if (dr != null)
                {

                    txteno.Text = dr[0].ToString();
                    txtname.Text = dr[1].ToString();
                    txtjob.Text = dr[2].ToString();
                    txtsalary.Text = dr[3].ToString();
                }
                else
                {
                    MessageBox.Show("Employee doesnot exist for a given employee no.");
                }
            }

        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();

        }
    }
}

No comments:

Post a Comment