C# Asp.Net Web Form CRUD : Insert, Update, Delete and View With Sql Server Database | CodAffection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace CRUD_Asp.Net
{
public partial class Contacts : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnDelete.Enabled = false;
FillGridView();
btnSave.Text = "Save";
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
Clear();
}
public void Clear()
{
hfContactID.Value = "";
txtName.Text = txtMobile.Text = txtAddress.Text = "";
lblSuccessMessage.Text = lblErrorMessage.Text = "";
btnSave.Text = "";
btnDelete.Enabled = false;
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
using(SqlConnection sqlCon = new SqlConnection(cs))
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("ContactCreateOrUpdate", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@ContactID", (hfContactID.Value == "" ? 0 : Convert.ToInt32(hfContactID.Value)));
sqlCmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
string contactID = hfContactID.Value;
Clear();
if (contactID == "")
{
lblSuccessMessage.Text = "Saved Successfully";
}
else
{
lblSuccessMessage.Text = "Updated Successfully";
}
FillGridView();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
void FillGridView()
{
try
{
using (SqlConnection sqlCon = new SqlConnection(cs))
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ContactViewAll",sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
gvContact.DataSource = dtbl;
gvContact.DataBind();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
protected void lnk_OnClick(object sender, EventArgs e)
{
int ContactID = Convert.ToInt32((sender as LinkButton).CommandArgument);
try
{
using (SqlConnection sqlCon = new SqlConnection(cs))
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ContactViewByID", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@ContactID",ContactID);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
hfContactID.Value = ContactID.ToString();
txtName.Text = dtbl.Rows[0]["Name"].ToString();
txtMobile.Text = dtbl.Rows[0]["Mobile"].ToString();
txtAddress.Text = dtbl.Rows[0]["Address"].ToString();
btnSave.Text = "Update";
btnDelete.Enabled = true;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
using(SqlConnection sqlCon = new SqlConnection(cs))
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("ContactDeleteByID", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@ContactID", Convert.ToInt32(hfContactID.Value));
sqlCmd.ExecuteNonQuery();
Clear();
FillGridView();
lblSuccessMessage.Text = "Deleted Successfully";
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
}
}
Above File is CCRUD_Asp.Net\Contacts.aspx.cs
<%@ Page EnableViewState="true" Language="C#" AutoEventWireup="true" CodeBehind="Contacts.aspx.cs" Inherits="CRUD_Asp.Net.Contacts" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hfContactID" runat="server"/>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Mobile"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Address"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td colspan="2">
<asp:Button ID="btnSave" runat="server" Text="Save" EnableViewState="true" OnClick="btnSave_Click"/>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click"/>
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click"/>
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<asp:Label ID="lblSuccessMessage" runat="server" Text="" ForeColor="Green"></asp:Label>
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<asp:Label ID="lblErrorMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
<br/>
<asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField="Mobile" HeaderText="Mobile"/>
<asp:BoundField DataField="Address" HeaderText="Address"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%# Eval("ContactID") %>' OnClick="lnk_OnClick">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment