Example to work with Data Reader.


Create a new windows forms Application 

Take fout Lablels,four TextBoxes,one Button on it and set the following Properties
To them

lable1:
Name         :   lblEmpId
Text           :  Enter EmpId lable2:
Name         :   lblEName
Text         :  Enter EName

lable3:
Name         :   lblDoj
Text :   Enter Doj

lable1:
Name         :   lblSalary
Text  :   Enter Salary
textBox1:
Name         :  txtEmpId
textBox2:
Name         :  txtUName
textBox3:
Name         :  txtDoj
textBox1:
Name         :  txtSalary
button1:
Name         :   btnNext
Text :   Next



Design :-



Generate the Event Handlers for btnNext button Click Events
Goto Source & write the following code

using System.Data.SqlClient;
namespace FormNPQInsert
{
    public partial class FormDataReader : Form
    {
        SqlConnection Con = new SqlConnection("Server=.;User Id=sa;Password=Admin123;DataBase=Employee");
        SqlCommand Cmd;
        SqlDataReader DR;
        public FormDataReader()
        {
            InitializeComponent();
        }

        private void FormDataReader_Load(object sender, EventArgs e)
        {
            Cmd = new SqlCommand("Select * from EmpDetails",Con);
            Con.Open();
            DR = Cmd.ExecuteReader();
            MessageBox.Show("Data is Available in Network Buffer");
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (DR.Read())
            {
                txtEmpId.Text = DR[0].ToString();
                txtEName.Text = DR[1].ToString();
                txtDoj.Text = DR[2].ToString();
                txtSalary.Text = DR[3].ToString();
            }
            else
            {
                MessageBox.Show("There are No Record(s)");
                DR.Close();
                btnNext.Enabled = false;
            }

        }

      
    }
}




No comments:

Post a Comment