C# Windows form Insert, update, delete, edit operation in Gridview using the SQL Server database
Introduction :
In this artical you can lern how to perform insert update delete edit operation with windows form in desk top applicaton with use of Sql Server Database.
Step 1 : Create the Database
Step 2 : Create Class DAL.CS :
public class DAL
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public string gender { get; set; }
}
Step 3: Create Class BAL.CS :
public class BAL
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\program\WindowsCrud\WindowsCrud\WFCrud.mdf;Integrated Security=True;User Instance=True");
public void insert_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into user_master values('" + da.name + "','" + da.address + "','" + da.gender + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable select(DAL da)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select * from user_master", con);
sda.Fill(dt);
return dt;
}
public void delete_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from user_master where name='" + da.name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
}
public void update_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand("update user_master set name='" + da.name + "',address='" + da.address + "',gender='" + da.gender + "' where name='"+da.name+"'",con);
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable Edit_data(DAL da)
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("select * from user_master where name='"+da.name+"'",con);
adp.Fill(dt);
return dt;
}
}
Step 4: Add Windows Form And Add data Gridview in form:
Step 5: Code in form1.cs :
public partial class Form1 : Form
{
DAL da = new DAL();
BAL ba = new BAL();
DataTable dt=new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
grid();
}
public void grid()
{
da.name = txt_name.Text;
da.address = txt_address.Text;
da.gender = radioButton1.Text;
da.gender = radioButton2.Text;
dt = ba.select(da);
dataGridView1.DataSource = dt;
}
private void btn_insert_Click(object sender, EventArgs e)
{
da.name = txt_name.Text;
da.address = txt_address.Text;
if (radioButton1.Checked == true)
{
da.gender = radioButton1.Text;
ba.insert_data(da);
MessageBox.Show("Data Insrted.....");
}
else
if (radioButton2.Checked == true)
{
da.gender = radioButton2.Text;
ba.insert_data(da);
MessageBox.Show("Data Insrted.....");
}
else
{
MessageBox.Show("Enter Your Gender ????");
}
grid();
}
private void btn_delete_Click(object sender, EventArgs e)
{
da.name = txt_name.Text;
da.address = txt_address.Text;
da.gender = radioButton1.Text;
da.gender = radioButton2.Text;
ba.delete_data(da);
MessageBox.Show("data Deleted...........");
grid();
}
private void btn_update_Click(object sender, EventArgs e)
{
da.name = txt_name.Text;
da.address = txt_address.Text;
if (radioButton1.Checked == true)
{
da.gender = radioButton1.Text;
ba.update_data(da);
MessageBox.Show("Data updated.....");
}
else
if (radioButton2.Checked == true)
{
da.gender = radioButton2.Text;
ba.update_data(da);
MessageBox.Show("Data updated.....");
}
else
{
MessageBox.Show("Enter Your Gender ????");
}
grid();
}
private void btn_edit_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
da.name = txt_name.Text;
dt = ba.Edit_data(da);
txt_address.Text=dt.Rows[0]["name"].ToString();
radioButton1.Text =dt.Rows[0]["gender"].ToString();
radioButton1.Text=dt.Rows[0]["gender"].ToString();
grid();
}
}
Run Program :
See Other Tutorial :
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
1 comments:
Write commentschetu patel को मेरी ओर से धन्यवाद आप का ये ब्लॉग मौजे बहुत पसंद आया ओर जो आप ने पोस्ट एड़ की वो अछि है॰
Replyfrom
http://computerhindiknowledge.blogspot.in/
ConversionConversion EmoticonEmoticon