1. ShowFooter="true" // This Property will set for Footer Reason
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Gridview_insertupdateDelete : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["Satya"].ToString());
con.Open();
if (!IsPostBack)
{
GetData();
}
}
public void GetData()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da = new SqlDataAdapter("Select * from Demo",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GetData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int a = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string b = GridView1.DataKeys[e.RowIndex].Values["Name"].ToString();
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox tc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Update demo set Name='"+tb.Text+"',Address='"+tc.Text+"' where roll="+a,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
GetData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int a = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
//string b = GridView1.DataKeys[e.RowIndex].Value.ToString();
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox tc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Delete from Demo where Roll='"+a.ToString()+"'",con);
cmd.ExecuteNonQuery();
con.Close();
GetData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GetData();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox txtCity = (TextBox)GridView1.FooterRow.FindControl("TextBox5");
TextBox txtDesgnation = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd =
new SqlCommand(
"insert into Demo(Roll,Name,Address) values('" + txtUsrname.Text + "','" +
txtCity.Text + "','" + txtDesgnation.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
Response.Write("inserted succefully");
GetData();
// lblresult.ForeColor = Color.Green;
// lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
Response.Write(" data not inserted succefully");
GetData();
//lblresult.ForeColor = Color.Red;
//lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}
}
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridview insertupdateDelete.aspx.cs" Inherits="Gridview_insertupdateDelete" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server"
style="position:absolute; top: 75px; left: 256px;"
AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" ShowFooter="true"
onrowupdating="GridView1_RowUpdating" DataKeyNames="Roll,Name"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" Text="AddNew" CommandName="AddNew" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll">
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Roll")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" >
<FooterTemplate>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Address")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Page Design
Database Design
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Gridview_insertupdateDelete : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["Satya"].ToString());
con.Open();
if (!IsPostBack)
{
GetData();
}
}
public void GetData()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da = new SqlDataAdapter("Select * from Demo",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GetData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int a = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string b = GridView1.DataKeys[e.RowIndex].Values["Name"].ToString();
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox tc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Update demo set Name='"+tb.Text+"',Address='"+tc.Text+"' where roll="+a,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
GetData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int a = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
//string b = GridView1.DataKeys[e.RowIndex].Value.ToString();
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox tc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Delete from Demo where Roll='"+a.ToString()+"'",con);
cmd.ExecuteNonQuery();
con.Close();
GetData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GetData();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox txtCity = (TextBox)GridView1.FooterRow.FindControl("TextBox5");
TextBox txtDesgnation = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd =
new SqlCommand(
"insert into Demo(Roll,Name,Address) values('" + txtUsrname.Text + "','" +
txtCity.Text + "','" + txtDesgnation.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
Response.Write("inserted succefully");
GetData();
// lblresult.ForeColor = Color.Green;
// lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
Response.Write(" data not inserted succefully");
GetData();
//lblresult.ForeColor = Color.Red;
//lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}
}
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridview insertupdateDelete.aspx.cs" Inherits="Gridview_insertupdateDelete" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server"
style="position:absolute; top: 75px; left: 256px;"
AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" ShowFooter="true"
onrowupdating="GridView1_RowUpdating" DataKeyNames="Roll,Name"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" Text="AddNew" CommandName="AddNew" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll">
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Roll")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" >
<FooterTemplate>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("Address")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Page Design
No comments:
Post a Comment