Windows Application CRUD Operation with ADO.net
- Get link
- X
- Other Apps
Posted by Ganesan - +919042710472
Create STUDENTDB Database in Server Explorer.
Create table StudentDetails in STUDENTDB with the below code snippet,
Textbox Properties
StudentName Name - txtStudentName
Age Name - txtAge
Save -btnSave
Show Data Link Name -linkLabel1
Events
btnSave OnClick =btnSave_Click
Show Data Link onClick= linkLabel1_LinkClicked
Add below Namespace in Form1.cs
Using System.Data.SqlClient;
Using System.Configuration;
Add Reference System.Configuration.DLL in the Project.
In Form1.cs (Replace) put the below Snippet,
public partial class Form1 : Form { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString); public Form1() { InitializeComponent(); } private void btnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("insert into StudentDetails values('"+txtStudentName.Tex+"'"+",'"+txtAge.Text+"')",con); int results=cmd.ExecuteNonQuery(); con.Close(); if (results > 0) { MessageBox.Show("Inserted Successfully"); } } private void Clear() { txtStudentName.Text = string.Empty; txtAge.Text = string.Empty; } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { ShowData objShowData = new ShowData(); objShowData.Show(); this.Hide(); } } |
Add ShowData.cs form as below design,
Properties
DataGrid Name - dgvGridData
Update Data Name - lnkUpdate
Events
lnkUpdate Link onClick Event - lnkUpdate_LinkClicked
ShowData Form OnLoad Event - ShowData_Load
Add below Namespace in ShowData.cs
Using System.Data.SqlClient;
Using System.Configuration;
In ShowData.cs (Replace) put the below Snippet,
public partial class ShowData : Form { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString); public ShowData() { InitializeComponent(); } private void ShowData_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand("select * from StudentDetails", con); SqlDataAdapter ada = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); ada.Fill(ds); dgvGridData.DataSource = ds.Tables[0]; } private void lnkUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { UpdateUserData objUpdateUserData = new UpdateUserData(); objUpdateUserData.Show(); this.Hide(); } } |
Add UpdateUserData.cs Form as below
Design,
Properties
StudentId textbox Name -
txtStudentId
StudentName Textbox -txtStudentName
Age Textbox - txtAge
Update button name -btnUpdate
Get Data button Name - btnGet
Clear button Name - btnClear
Show Data Link Name -linkLabel1
Event :
btnUpdate onClick =btnUpdate_Click
btnGet onClick= btnGet_Click
btnClear onClick =btnClear_Click
linkLabel1 onClick -
linkLabel1_LinkClicked
Add below Namespace in UpdateUserData.cs
Using System.Data.SqlClient; Using System.Configuration; |
Put (Replace) the below snippet in UpdateUserData.cs ,
public partial class UpdateUserData : Form
{ SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString); public UpdateUserData() { InitializeComponent(); } private void btnUpdate_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("Update StudentDetails Set StudentName='" + txtStudentName.Text + "',Age='" + txtAge.Text + "' where StudentId=" + txtStudentId.Text, con); int results = cmd.ExecuteNonQuery(); con.Close(); if (results > 0) { MessageBox.Show("Updated Successfully"); } Clear(); } private void Clear() { txtStudentId.Text = string.Empty; txtStudentName.Text = string.Empty; txtAge.Text = string.Empty; btnGet.Enabled = true; } private void btnGet_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("select * from StudentDetails where StudentId="+txtStudentId.Text, con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { txtStudentName.Text = dr["StudentName"].ToString(); txtAge.Text = dr["Age"].ToString(); } con.Close(); btnGet.Enabled = false; } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { ShowData objShowData = new ShowData(); objShowData.Show(); this.Hide(); } private void btnClear_Click(object sender, EventArgs e) { Clear(); } } |
- Get link
- X
- Other Apps
Comments
Post a Comment