Friday, May 13, 2016

CommandBuilder in ADO.Net

The CommandBuilder use to generate update, Delete, insert commands on a single database table for data adapter, in other words it is used to permanently save the record in the database.

The Example of the CommandBuilder is given below: 

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;
 
namespace WindowsFormsApplication20
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void button1_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = textBox1.Text;
            dr[1] = textBox2.Text;
            dr[2] = textBox3.Text;
            ds.Tables[0].Rows.Add(dr);
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            da.Update(ds.Tables[0]);
            MessageBox.Show("save");
        }
    }
}
Output  

Thursday, May 12, 2016

How to create table, Update and Delete rows in Data Grid view

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;
 
 
namespace WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        private void button1_Click(object sender, EventArgs e)
        {
            DataColumn dc1 = new DataColumn("Id", typeof(int));
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            DataColumn dc3 = new DataColumn("Salary", typeof(int));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "Sachin";
            dr[2] = 2000;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            dt.Rows[0].Delete();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            dt.Rows[0][0] = 2;
        }
    }
}

Output : 

click on show button: 
 click on update button :
click on delete button :

Wednesday, May 11, 2016

C# login page using ADO.Net

Firstly create table in the database by name login 

C# code :-

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;
 
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from login", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr[0].ToString().Trim() == textBox1.Text && dr[1].ToString().Trim() == textBox2.Text)
                {
                    Form2 f = new Form2();
                    f.Show();
 
                    break;
 
                }
 
                else
                {
                    MessageBox.Show("Invalid");
                }
 
            }
        }
    }
}

Output : 


Tuesday, May 10, 2016

ACID properties in SQL

ACID stand for
  1. A is stand for Atomicity
  2. C is stand for Consistency
  3. I is stand for Isolation
  4. D is stand for Durability

Atomicity – Atomicity is the features that means all the operations in the transaction must be completed successfully and committed, if any operations become fails then all the transactions must be rolled back.

Consistency – This is the features that means the transaction must be consistent is a state, in other words the transaction must be change the state of the system for a specific operation.

Isolation - Isolation is the property that contains the features that one operation in the transaction cannot see the result of the other operation in the transaction.

Durability – It means the transaction must be durable once it has been successfully completed, it prevent from the loss of information in case of resource damage and system failure.

Find the data of the column in SQL database on the basis of one column

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;
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bool b = false;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i][0].ToString().Trim() == textBox1.Text.Trim())
                {
                    textBox2.Text = ds.Tables[0].Rows[i][1].ToString();
                    textBox3.Text = ds.Tables[0].Rows[i][2].ToString();
                    b = true;
                    break;
                }
            }
            if (!b)
            {
                MessageBox.Show("Id does not exist");
            }
        }
    }
}

Monday, May 9, 2016

Fetch Data from SQL database in Disconnected Environment

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;
 
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = ds.Tables[0].Rows[0][0].ToString();
            textBox2.Text = ds.Tables[0].Rows[0][1].ToString();
            textBox3.Text = ds.Tables[0].Rows[0][2].ToString();
 
        }
    }

}

Output :

Validation controls in ASP.Net

In ASP.Net validation controls are used to check the information that the user enter is valid or not. There are six types of validation control are given below:
  1. RequiredFieldValidation Control
  2. CompareValidator Control
  3. RangeValidator Control
  4. RegularExpressionValidator Control
  5. CustomValidator Control
  6. ValidationSummary
RequiredFieldValidation Control

The RequiredFieldvalidation control is used to see the data is entered for the input control, In other words we use RequiredFieldvalidation control for mandatory field.


<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" Style="top: 100px;

        left: 300px; position: absolute; height: 30px; width: 130px" ErrorMessage="password required"
        ControlToValidate="TextBox2"></asp:RequiredFieldValidator>

CompareValidator Control

CompareValidator Control is used to compare the data is input in one input control with another input control. It is used to compare the password from one textbox to another textbox at a time of registration.


<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" Style="top: 100px;
        left: 300px; position: absolute; height: 30px; width: 130px" ErrorMessage="password required"
        ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
   
RangeValidator Control


RangeValidator Control is used to check the value of the input control that is in valid range, for example minimum and maximum value of the input control.

<asp:RangeValidator ID="RangeValidator1" runat="server" Style="top: 150px; left: 300px;
            position: absolute; height: 20px; width: 90px"
        ErrorMessage="RangeValidator" ControlToValidate="TextBox4" MaximumValue="50"
        MinimumValue="20" Type="Integer"></asp:RangeValidator>
 
 RegularExpressionValidator Control

RegularExpressionValidator Control is used to check user’s input based on a pattern that you define by using a regular expression, it is used to check the valid phone number, email address, zip code etc.
 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Style="top: 200px; left: 300px; position: absolute; height: 20px; width: 160px"        ErrorMessage="RegularExpressionValidator"ControlToValidate="TextBox5"        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
 
CustomValidator Control
  
CustomValidator Control is used to allow write a method to handle the validation of the entered value.

Validation Summery

Validation summery is used to display report of all validations error occurred in the web page.