Friday, 7 April 2017

Crud Operation(Select,Insert,Update,Delete) Without Database Using Asp.Net

Crud Operation (Select,Insert,Update,Delete) Using DataTable 

I.Introduction:

Here i will explain how to perform CRUD  Operation in DataTable with the example 

II.Description:

To implement CRUD in DataTable ,First we need to create one new DataTable and add the columns to the DataTable and store the Datatable in ViewState followed by the below given code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Department :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtDept" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Salary :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblerror" runat="server" Text="" ForeColor="Red" Visible="false"></asp:Label>
<asp:Label ID="lblid" runat="server" Text="" Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" Visible="false" />
</td>
<td>
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
</table>
<br />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns> 
    <asp:TemplateField HeaderText="ID" Visible="false">
    <ItemTemplate>
        <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
    <asp:TemplateField HeaderText="Department">
    <ItemTemplate>
        <asp:Label ID="lblDept" runat="server" Text='<%#Eval("Department") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
    <asp:TemplateField HeaderText="Salary">
    <ItemTemplate>
        <asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
    <asp:TemplateField HeaderText="Action">
    <ItemTemplate >
            <asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="btnEdit_Click" />
        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
        
    </ItemTemplate>
</asp:TemplateField>
</Columns>

<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />

</asp:GridView>
    
           
</form>
</body>
</html>

After completion of aspx page add following namespace in codebehind

C# Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

After completion of adding namespaces you need to write the code like this as shown below

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns["ID"].AutoIncrement = true;
        dt.Columns["ID"].AutoIncrementSeed = 1;
        dt.Columns["ID"].AutoIncrementStep = 1;
        dt.Columns.Add("Name");
        dt.Columns.Add("Department");
        dt.Columns.Add("Salary");
        ViewState["dt"] = dt;
    }
}

protected void btnSave_Click(object sender, EventArgs e)
{
    DataTable dt = ViewState["dt"] as DataTable;
    DataRow drw= dt.NewRow();
    drw["Name"] = txtName.Text;
    drw["Department"] = txtDept.Text;
    drw["Salary"] = txtSalary.Text;
    dt.Rows.Add(drw);
    ViewState["dt"] = dt;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    lblerror.Visible = true;
    lblerror.Text = "Successfully Saved !!!";
           

}
protected void clearControls()
{
    txtName.Text = "";
    txtDept.Text = "";
    txtSalary.Text = "";
    lblerror.Visible = false;
}

protected void btnClear_Click(object sender, EventArgs e)
{
    clearControls();
    lblerror.Visible = false;

}

protected void btnEdit_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow grd = btn.NamingContainer as GridViewRow;
    lblid.Text= (grd.FindControl("lblID") as Label).Text;
    txtName.Text = (grd.FindControl("lblName") as Label).Text;
    txtDept.Text = (grd.FindControl("lblDept") as Label).Text;
    txtSalary.Text = (grd.FindControl("lblSalary") as Label).Text;
    btnSave.Visible = false;
    btnUpdate.Visible = true;
}

protected void btnDelete_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow grd = btn.NamingContainer as GridViewRow;
    lblid.Text = (grd.FindControl("lblID") as Label).Text;
    int row = Convert.ToInt32(lblid.Text) - 1;
    DataTable dt = (DataTable)ViewState["dt"];
    dt.Rows.RemoveAt(row);
    ViewState["dt"] = dt;
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    int row = Convert.ToInt32(lblid.Text) - 1;
    DataTable dt =(DataTable) ViewState["dt"];
    dt.Rows[row]["Name"] = txtName.Text;
    dt.Rows[row]["Department"] = txtDept.Text;
    dt.Rows[row]["Salary"] = txtSalary.Text;
    ViewState["dt"] = dt;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    btnSave.Visible = true;
    btnUpdate.Visible = false;
    lblerror.Visible = true;
    lblerror.Text = "Updated Succesfully !!!!";

}

Demo:


Hope you all understand the creation of CRUD without using Database in Asp.net..  


Do have any query regarding my code you can comment below !!! 

Wednesday, 5 April 2017

Create Dynamically DataTable and bind to GridView in Asp.Net

How To Save DataTable Into ViewState and Bind Gridview Without DataBase Using ASP.Net






I.Introduction

here i will explain how to store data into (programmatically) datatable and bind data into gridview in asp.net using C#.

II.Description:

To implement dynamically datatable we need to create one new datatable after that add columns and bind data to columns after that add those columns to datatable followed by the below shown code.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
    <asp:Label ID="Label1" runat="server" Text="Name :"></asp:Label>
</td>
<td>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    <asp:Label ID="Label2" runat="server" Text="Department :"></asp:Label>
</td>
<td>
    <asp:TextBox ID="txtDept" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    <asp:Label ID="Label3" runat="server" Text="Salary :"></asp:Label>
</td>
<td>
    <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
    <asp:Label ID="lblerror" runat="server" Text="" ForeColor="Red" Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</td>
<td>
    <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
</table>
<br />

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />

</asp:GridView>
</form>
</body>
</html>


After completion of aspx page add following namespaces in codebehind



C# code



using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
            DataTable data = new DataTable();
            data.Columns.Add("Name");
            data.Columns.Add("Department");
            data.Columns.Add("Salary");
            ViewState["dt"] = data;
        }
}

protected void btnSave_Click(object sender, EventArgs e)
{
    DataTable dt = ViewState["dt"] as DataTable;
    DataRow drw= dt.NewRow();
    drw["Name"] = txtName.Text;
    drw["Department"] = txtDept.Text;
    drw["Salary"] = txtSalary.Text;
    dt.Rows.Add(drw);
    ViewState["dt"] = dt;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    lblerror.Visible = true;
    lblerror.Text = "Successfully Saved !!!";
           

}
protected void clearControls()
{
    txtName.Text = "";
    txtDept.Text = "";
    txtSalary.Text = "";
}

protected void btnClear_Click(object sender, EventArgs e)
{
    clearControls();
    lblerror.Visible = false;

}


Demo:

Hope you all understand the creation of  Dynamic Datatable and bind to gridview in Asp.Net   ..  


Do have any query regarding my code you can comment below !!!