+91-90427 10472
         
Dot net training in Chennai -Maria Academy

Windows Application CRUD Operation with ADO.net

26 Oct 2016

Document by Ganesan – Ganesanva@hotmail.com – +919600370429

Create TestDB Database in Server Explorer
Create table UserDetails in TestDB with the below code snippet,

CREATE TABLE [dbo].[UserDetails] (
[UserId] INT IDENTITY (1, 1) NOT NULL,
[UserName] VARCHAR (100) NULL,
[Password] VARCHAR (100) NULL,
[City] VARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([UserId] ASC)
);


Add the below Connection string in app.config,

<connectionStrings>
<add name=”TestConnection” connectionString=”Data Source=(LocalDb)\v11.0;Initial Catalog=TestDB;Integrated Security=True;Pooling=False” providerName=”System.Data.SqlClient” />
</connectionStrings>


Design Form1 as below,

Textbox Properties
UserName Name – txtUserName
Password Name – txtPassword
City Name – txtCity
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 UserDetails values(‘”+txtUserName.Text+”‘”+”,'”+txtPassword.Text +”‘,'”+txtCity.Text+”‘)”,con);
int results=cmd.ExecuteNonQuery();
con.Close();
if (results > 0)
{
MessageBox.Show(“Inserted Successfully”);
}
}
private void Clear()
{
txtUserName.Text = string.Empty;
txtPassword.Text = string.Empty;
txtCity.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 UserDetails”, 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
UserId textbox Name – txtUserId
UserName Textbox -txtUserName
Password Textbox – txtPassword
City Textbox – txtCity
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 UserDetails Set UserName='” + txtUserName.Text + “‘,Password='” + txtPassword.Text + “‘,City='” + txtCity.Text + “‘ where UserId=” + txtUserId.Text, con);
int results = cmd.ExecuteNonQuery();
con.Close();
if (results > 0)
{
MessageBox.Show(“Updated Successfully”);
}
Clear();
}
private void Clear()
{
txtUserId.Text = string.Empty;
txtUserName.Text = string.Empty;
txtPassword.Text = string.Empty;
txtCity.Text = string.Empty;
btnGet.Enabled = true;
}
private void btnGet_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(“select * from UserDetails where UserId=”+txtUserId.Text, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtUserName.Text = dr[“UserName”].ToString();
txtPassword.Text = dr[“Password”].ToString();
txtCity.Text = dr[“City”].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();
}
}

The Output as below,



Table Data as below,

Click below to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg6khuXmrBnLklntXTA

Social tagging: > > > > > > > > > >