Create Database Name as Inventory.
Create Table Name as Books Columns are Id (int),Title(nvarchar),ISBN(nvarchar),Price(Number).
Paste the Code on aspx Page:-
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<h2>
<asp:Label ID="lblMsg" runat="server" > </asp:Label>
</h2>
<table>
<tr>
<td width="120px">
<asp:Label ID="Label3" runat="server" Text="ID"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtId"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="Label1" runat="server" Text="Name"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="LblISBN" runat="server" Text="ISBN"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtISBN"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="Label2" runat="server" Text="Price"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtPrice"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="Server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="grdDetails" runat="server" Width="250px" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcancelingedit="grdDetails_RowCancelingEdit"
onrowdeleting="grdDetails_RowDeleting" onrowediting="grdDetails_RowEditing"
onrowupdating="grdDetails_RowUpdating" onrowcanceling="grdDetails_RowCancelingEdit" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:Content>
=================================
Paste the Code ob aspx.cs Page:-
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;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
// Connection String
// public const String ConnectionString = "server=.;database=Inventory;User Id=sa;password=sa123";
SqlConnection Con = new SqlConnection(Settings.ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grdDetails.DataSource = GetData();
grdDetails.DataBind();
}
}
DataTable GetData()
{
DataTable dt = new DataTable();
{
SqlConnection con = new SqlConnection(Settings.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * From Books ", con);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
}
return dt;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection Con = new SqlConnection(Settings.ConnectionString);
Con.Open();
string query = "insert into Books(ID,Title,ISBN,Price)Values('" + txtId.Text + "','" + txtName.Text + "','" + txtISBN.Text + "','" + txtPrice.Text + "' )";
SqlCommand cmd = new SqlCommand(query, Con);
cmd.ExecuteNonQuery();
Con.Close();
lblMsg.Text = "Saving is Done";
txtId.Text = "";
txtName.Text = "";
txtISBN.Text = "";
txtPrice.Text = "";
Response.Redirect("default.aspx");
}
protected void grdDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDetails.EditIndex = e.NewEditIndex;
}
protected void grdDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdDetails.EditIndex = -1;
GetData();
}
protected void grdDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = grdDetails.Rows[grdDetails.EditIndex];
int Id = int.Parse(((TextBox)row.Cells[1].Controls[0]).Text);
// string Title = ((TextBox)row.Cells[2].Controls[0]).Text;
TextBox txtTitle = (TextBox)row.Cells[2].Controls[0];
TextBox txtISBN = (TextBox)row.Cells[3].Controls[0];
TextBox txtPrice = (TextBox)row.Cells[4].Controls[0];
Con.Open();
SqlCommand cmd = new SqlCommand("update Books set Title='" + txtTitle.Text + "', ISBN='" + txtISBN.Text + "',Price='" + txtPrice.Text + "' where Id=" + Id, Con);
cmd.ExecuteNonQuery();
Con.Close();
grdDetails.EditIndex = -1;
GetData();
Response.Redirect("Default.aspx");
Response.Write("<Script>alert('Updated Successfully') </script>");
}
protected void grdDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = grdDetails.Rows[e.RowIndex];
int Id = int.Parse(row.Cells[1].Text);
Con.Open();
SqlCommand cmd = new SqlCommand("delete from Books where Id=" + Id, Con);
int result = cmd.ExecuteNonQuery();
Con.Close();
Response.Redirect("Default.aspx");
Response.Write("<Script>alert('DeletedSuccessfully') </script>");
}
}
}
Create Table Name as Books Columns are Id (int),Title(nvarchar),ISBN(nvarchar),Price(Number).
Paste the Code on aspx Page:-
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<h2>
<asp:Label ID="lblMsg" runat="server" > </asp:Label>
</h2>
<table>
<tr>
<td width="120px">
<asp:Label ID="Label3" runat="server" Text="ID"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtId"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="Label1" runat="server" Text="Name"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="LblISBN" runat="server" Text="ISBN"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtISBN"></asp:TextBox>
</td>
</tr>
<tr>
<td width="120px">
<asp:Label ID="Label2" runat="server" Text="Price"> </asp:Label>
</td>
<td width="180px">
<asp:TextBox runat="server" ID="txtPrice"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="Server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="grdDetails" runat="server" Width="250px" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcancelingedit="grdDetails_RowCancelingEdit"
onrowdeleting="grdDetails_RowDeleting" onrowediting="grdDetails_RowEditing"
onrowupdating="grdDetails_RowUpdating" onrowcanceling="grdDetails_RowCancelingEdit" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:Content>
=================================
Paste the Code ob aspx.cs Page:-
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;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
// Connection String
// public const String ConnectionString = "server=.;database=Inventory;User Id=sa;password=sa123";
SqlConnection Con = new SqlConnection(Settings.ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grdDetails.DataSource = GetData();
grdDetails.DataBind();
}
}
DataTable GetData()
{
DataTable dt = new DataTable();
{
SqlConnection con = new SqlConnection(Settings.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * From Books ", con);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
}
return dt;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection Con = new SqlConnection(Settings.ConnectionString);
Con.Open();
string query = "insert into Books(ID,Title,ISBN,Price)Values('" + txtId.Text + "','" + txtName.Text + "','" + txtISBN.Text + "','" + txtPrice.Text + "' )";
SqlCommand cmd = new SqlCommand(query, Con);
cmd.ExecuteNonQuery();
Con.Close();
lblMsg.Text = "Saving is Done";
txtId.Text = "";
txtName.Text = "";
txtISBN.Text = "";
txtPrice.Text = "";
Response.Redirect("default.aspx");
}
protected void grdDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDetails.EditIndex = e.NewEditIndex;
}
protected void grdDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdDetails.EditIndex = -1;
GetData();
}
protected void grdDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = grdDetails.Rows[grdDetails.EditIndex];
int Id = int.Parse(((TextBox)row.Cells[1].Controls[0]).Text);
// string Title = ((TextBox)row.Cells[2].Controls[0]).Text;
TextBox txtTitle = (TextBox)row.Cells[2].Controls[0];
TextBox txtISBN = (TextBox)row.Cells[3].Controls[0];
TextBox txtPrice = (TextBox)row.Cells[4].Controls[0];
Con.Open();
SqlCommand cmd = new SqlCommand("update Books set Title='" + txtTitle.Text + "', ISBN='" + txtISBN.Text + "',Price='" + txtPrice.Text + "' where Id=" + Id, Con);
cmd.ExecuteNonQuery();
Con.Close();
grdDetails.EditIndex = -1;
GetData();
Response.Redirect("Default.aspx");
Response.Write("<Script>alert('Updated Successfully') </script>");
}
protected void grdDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = grdDetails.Rows[e.RowIndex];
int Id = int.Parse(row.Cells[1].Text);
Con.Open();
SqlCommand cmd = new SqlCommand("delete from Books where Id=" + Id, Con);
int result = cmd.ExecuteNonQuery();
Con.Close();
Response.Redirect("Default.aspx");
Response.Write("<Script>alert('DeletedSuccessfully') </script>");
}
}
}
For More details Follow the link working with Grid View
No comments:
Post a Comment