Friday 21 September 2012

Database Operation in Visual C#

Controls
  • Three TextBox
  • Three Button
  • One Data GridView


Setting DataGridView..
  • Click Datagridview task An Arrow right top side of Gridview
  • Choose Data Source
  • Add Project Data Source
  • Select Database from Configuration Wizard Click Next
  • Click New Connection Click change from add connection wizard,select Microsoft Access Database file from  change data source wizard click ok
  • Browse your database mdb file from your computer, Test Connection > Ok >click next from configuration wizard
  • Visual studio prompt you to add your database in current project if you want to add click yes otherwise on
  • Select table from configuratiob wizard select your table and check fields which you want show in Datagridview and finish




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.OleDb;
namespace AccessDemo
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=D:\Raj\c#\Database\bsw.mdb");
        OleDbCommand com = new OleDbCommand();
        public Form1()
        {
            try
            {
                con.Open();
                com.Connection = con;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtRoll.Text == "" || txtName.Text == "" || txtCity.Text == "")
            {
                MessageBox.Show("Please fill all field properly");
            }
            else
            {
                             
                try
                {
               
                    com.CommandText = "insert into stu values(" + txtRoll.Text + ",'" + txtName.Text + "','" + txtCity.Text + "')";
                    com.ExecuteNonQuery();
                    MessageBox.Show("saved");
                    this.stuTableAdapter.Fill(this.bswDataSet.stu);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
         

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bswDataSet.stu' table. You can move, or remove it, as needed.
            this.stuTableAdapter.Fill(this.bswDataSet.stu);

        }



        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtRoll.Text == "" && txtName.Text == "" && txtCity.Text == "")
            {
                MessageBox.Show("Please provide All Value");
            }
            else
            {
                com.CommandText = "update stu set name='" + txtName.Text + "',city='" + txtCity.Text + "' where roll=" + txtRoll.Text;
                try
                {
                    com.ExecuteNonQuery();
                    MessageBox.Show("Record Updated");
                    this.stuTableAdapter.Fill(this.bswDataSet.stu);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtRoll.Text == "")
            {
                MessageBox.Show("Please enter roll number");
                txtRoll.Focus();
            }
            else
            {
                com.CommandText = "delete from stu where roll=" + txtRoll.Text;
                try
                {
                    if(MessageBox.Show("Are you sure to delete this record","Girfa:Student Help",MessageBoxButtons.YesNo)==DialogResult.Yes)
                    {
                        com.ExecuteNonQuery();
                        MessageBox.Show("Record Deleted");
                        txtRoll.Text = "";
                        this.stuTableAdapter.Fill(this.bswDataSet.stu);
                    }
                 
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

No comments:

Post a Comment