Friday, 3 August 2012

Connectivity with SqlServer with DisConnected Mode



this code for Connectivity with DataBase (Ado.NET with Sql Server ) in Dis Connected Mode.....

1.  Create Database test
     Create database test

2.  Create Table p_customer in test Database

     a.  id  int ,primary key 
     b.  fname nvarchar(50)
      c.  lname nvarchar(50)
     d.  dob nvarchar(50)


   use test
   create table p_customer ( id  int primary key, fname nvarchar(50), lname nvarchar(50), dob      nvarchar(50))












using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
//Frist we are using this  name  sapce for SqlDataAdapter, DataSet 
 using System.Data;

namespace DisConnectedMode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
    private void Bfill_Click(object sender, EventArgs e)
{
        string ConStr = "Data Source=KUSH-PC;Initial Catalog=test;Integrated      Security=True";
       string cmd = "select * from p_customer";
            da = new SqlDataAdapter(cmd, ConStr);
  or  
         da = new SqlDataAdapter("select * from p_customer", "Data Source=KUSH-PC;Initial       Catalog=test;Integrated Security=True");
            ds = new DataSet();
            da.Fill(ds);
            MessageBox.Show("Dataset is filled");
        }

       we are using this  button  for showing data  in gridview
         private void BShow_Click(object sender, EventArgs e)
         {
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Visible = true;
          }
     we are using this  button  for updating data  in database without this button data  is  save  is    tempory database which is  callad DataAdapter................
        private void Bupdatedb_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder cbd = new SqlCommandBuilder(da);
            da.Update(ds);
            MessageBox.Show("Database is  updated");
        }

       we are using this  button  for saving data  
        private void BAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables[0];
            DataRow dr = dt.NewRow();
            dr[0] = txtsrno.Text;
            dr[1] = txtfname.Text;
            dr[2] = txtlname.Text;
            dr[3] = txtdob.Text;
            dt.Rows.Add(dr);
            MessageBox.Show("New row Added");
        }

      we are using this  button  for searching  data with srno
        private void BSearch_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                if(dr.RowState.ToString ()!="Deleted")

                if (dr[0].ToString() == txtsrno.Text)
                {
                    txtfname.Text = dr[1].ToString();
                    txtlname.Text = dr[2].ToString();
                    txtdob.Text = dr[3].ToString();
                    break;
                }
            }
        }

      we are using this  button  for first record in datatable 
        private void BFirst_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                txtsrno.Text = dr[0].ToString();
                txtfname.Text = dr[1].ToString();
                txtlname.Text = dr[2].ToString();
                txtdob.Text = dr[3].ToString();
                break;
            }
        }

       we are using this  button  for last record in datatable 
        private void BLast_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                txtsrno.Text = dr[0].ToString();
                txtfname.Text = dr[1].ToString();
                txtlname.Text = dr[2].ToString();
                txtdob.Text = dr[3].ToString();

            }

        }

        we are using this  button  for next record in datatable 
        private void bNext_Click(object sender, EventArgs e)
        {
            bool ck = false;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                if (ck == true)
                {
                    txtsrno.Text = dr[0].ToString();
                    txtfname.Text = dr[1].ToString();
                    txtlname.Text = dr[2].ToString();
                    txtdob.Text = dr[3].ToString();
                    break;
                }
                if (dr[0].ToString() == txtsrno.Text)
                {
                    ck = true;
                }
                 }
        }

       we are using this  button  for previous record in datatable 
        private void Bpre_Click(object sender, EventArgs e)
        {
            int ck = Convert.ToInt32(txtsrno.Text);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                if (dr[0].ToString() == ck.ToString())
                {

                    break;
                }
                txtsrno.Text = dr[0].ToString();
                txtfname.Text = dr[1].ToString();
                txtlname.Text = dr[2].ToString();
                txtdob.Text = dr[3].ToString();
            }
        }

        we are using this  button  for delete record in data table 
        private void bDelete_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                 if(dr.RowState.ToString ()!="Deleted")
                if (txtsrno.Text == dr[0].ToString())
                {
                    dr.Delete();
                    MessageBox.Show("data deleted");
                    break;
                }

            }
        }

         we are using this  button  for modifie  record in datatable 
        private void bModifiy_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (txtsrno.Text == dr[0].ToString())
                {
                    dr[1] = txtfname.Text;
                    dr[2] = txtlname.Text;
                    dr[3] = txtdob.Text;
                    MessageBox.Show("data modified");


                    break;
                }
            }
        }
  


       }
       }



4 comments:

  1. thank you for this source code.. really it is very helpful for me. please do post some more code also.

    ReplyDelete
  2. nice post really it is very helpful for me

    ReplyDelete
  3. thank u kush sir for next & prevs code in disconetd mode.its helpful for me.

    ReplyDelete