This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Showing posts with label Insert Update. Show all posts
Showing posts with label Insert Update. Show all posts

Monday, 1 July 2013

Insert Update, Delete, Cancel in Gridview




Insert Update, Delete, Cancel in Gridview

// execute this  query  for  in Sql Server 2008  ………..
use test
create table Record (Id int primary key,Name nvarchar(50),Age int)

This source code  for  Default.aspx.cs………………………………..
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;

public partial class _Default : System.Web.UI.Page
{

SqlConnection con;

protected void Page_Load(object sender, EventArgs e)
{

con = new SqlConnection("data source=KUSH-PC\\KUSH; initial catalog=test; integrated security=true");

if (!IsPostBack)
{
BindGridView();
}

}

// When you click on Edit Link Button it shows Update,Cancel links
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();


}
// When you click on Cancel link it shows Edit,Delete links
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}

//Code for Pagging with GridView and set GridView Property Page Size=5
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}

//Code for GridView Binding.................
private void BindGridView()
{

SqlDataAdapter da = new SqlDataAdapter("select * from record",con);
DataSet ds = new DataSet();
da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
//Code for GridView Binding when there in no record ........
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "There is  no record here";
}

}
//Code for Add New Record in GridView .........................
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName =="AddNew")
{
TextBox id =(TextBox)GridView1.FooterRow.FindControl("txtid");
TextBox name = (TextBox)GridView1.FooterRow.FindControl("txtname");
TextBox age = (TextBox)GridView1.FooterRow.FindControl("txtage");
SqlDataAdapter da = new SqlDataAdapter("insert into record values('" + Convert.ToInt32(id.Text) + "','" + name.Text + "','" + Convert.ToInt32(age.Text) + "')", con);
DataSet ds = new DataSet();
da.Fill(ds);
BindGridView();

}
}


//Code for GridView Row Deleting................
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

//GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//Label lbldeleteID = (Label)row.FindControl("lblid");
Label ll = (Label)GridView1.Rows[e.RowIndex].FindControl("lblid");
SqlDataAdapter da = new SqlDataAdapter("delete from record where id='" + Convert.ToInt32(ll.Text) + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
BindGridView();



}
//Code for GridView Row Updating.................
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label ll = (Label)GridView1.Rows[e.RowIndex].FindControl("lblid");
TextBox txtname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
TextBox txtage = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtage");
GridView1.EditIndex = -1;
SqlDataAdapter da = new SqlDataAdapter("update record set name='"+txtname.Text+"',age='"+Convert.ToInt32(txtage.Text)+"' where id='" + Convert.ToInt32(ll.Text) + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
BindGridView();
}


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//getting Id from particular row of GridView
int id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Id"));
//identifying Delete Link Button  control in gridview
LinkButton lnkbtn = (LinkButton)e.Row.FindControl("lbdelete");
// when user clicks on  Delete link button raising javascript Confirmationbox
if (lnkbtn != null)
{
lnkbtn.Attributes.Add("onclick", "javascript:return ConfirmationBox(' Id =  "  + id + "')");
}

}
}
}

This source code  for  Default.aspx………………………………..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Insert Update, Delete, Cancel in Gridview</title>

<style type="text/css">

</style>
<script type="text/javascript">
function ConfirmationBox(id) {

var res = confirm('Are you sure you want to delete ' + id + ' Details ..?');
if (res) {

return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<table width="100%">

<tr><td><center><Font color="red"><b>Insert Update, Delete, Cancel in Gridview</b> </Font></center></td></tr>
<tr><td>
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4"
onrowediting="GridView1_RowEditing" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
ShowFooter="True" onrowcommand="GridView1_RowCommand" DataKeyNames="id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting"
onrowupdating="GridView1_RowUpdating"
onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="true"
PageSize="5" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="lblupdate" runat="server" CommandName="Update" >Update</asp:LinkButton>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="Lblcancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
&nbsp;
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbladdnewitem" runat="server" CommandName="AddNew" >Add New Item</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="lbdelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<EditItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtid" runat="server" Width="75px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="txtage" runat="server" Text='<%# Eval("Age") %>' Width="75px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtage" runat="server" Width="75px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblage" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</center>
</td></tr>
<tr><td></td></tr>

</table>


</div>
</form>
</body>
</html>


Insert Update, Delete, Cancel in Gridview
Insert Update, Delete, Cancel in Gridview
     Update      Cancel   Add New Item Edit     Delete