I am using sqlserver, three tier architecture and store procedure for this...
My aspx Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<columns>
<asp:TemplateField HeaderText="ProductId">
<itemtemplate>
<asp:Label ID="lblProductId" runat="server" Text='<%#Eval("ProductId") %>'>'>
</itemtemplate>
<asp:TemplateField HeaderText="ProductCategory">
<itemtemplate>
<asp:Label ID="lblProductCategory" runat="server" Text='<%#Eval("ProductCategory") %>'>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtProductCategory" runat="server" Text='<%#Eval("ProductCategory") %>'>' >
</edititemtemplate>
<asp:TemplateField HeaderText="GirthFrom">
<itemtemplate>
<asp:Label ID="lblGirthFrom" runat="server" Text='<%#Eval("GirthFrom") %>'>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtGirthFrom" runat="server" Text='<%#Eval("GirthFrom") %>'>' >
</edititemtemplate>
<asp:TemplateField HeaderText="GirthTo">
<itemtemplate>
<asp:Label ID="lblGirthTo" runat="server" Text='<%#Eval("GirthTo") %>'>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtGirthTo" runat="server" Text='<%#Eval("GirthTo") %>'>' >
</edititemtemplate>
<asp:TemplateField HeaderText="Action">
<itemtemplate>
<asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit">
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete">
</itemtemplate>
<edititemtemplate>
<asp:LinkButton ID="Update" runat="server" CommandName="Update" Text="Update">
<asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel" Text="cancel">
</edititemtemplate>
</columns>
My 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;
public partial class _Default : System.Web.UI.Page
{
Creation obj=new Creation();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridviewdatabind();
}
}
private void Gridviewdatabind()
{
DataTable dt = obj.display1();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string lblProductId = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProductId")).Text;
obj.ProductId = Convert.ToInt32(lblProductId);
int res = obj.delete();
Gridviewdatabind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Gridviewdatabind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string lblProductId = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProductId")).Text;
string txtProductCategory = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtProductCategory")).Text;
string txtGirthFrom = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtGirthFrom")).Text;
string txtGirthTo = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtGirthTo")).Text;
obj.ProductCategory = txtProductCategory;
obj.GirthFrom = Convert.ToInt32(txtGirthFrom);
obj.GirthTo = Convert.ToInt32(txtGirthTo);
obj.ProductId = Convert.ToInt32(lblProductId);
int res = obj.update();
GridView1.EditIndex = -1;
Gridviewdatabind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Gridviewdatabind();
}
}
My Datalayer(databaseoperatio class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class DBoperations
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Micro;Integrated Security=True");
SqlCommand cmd = null;
public DBoperations()
{
}
public int UPDATECATEGORY(Categorycreation obj)
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_Updatecategory";
cmd.Parameters.AddWithValue("@pdtctg", obj.ProductCategory);
cmd.Parameters.AddWithValue("@girfrom", obj.GirthFrom);
cmd.Parameters.AddWithValue("@girto", obj.GirthTo);
cmd.Parameters.AddWithValue("@pdtid", obj.ProductId);
con.Open();
int res = cmd.ExecuteNonQuery();
con.Close();
return res;
}
}
My property layer(class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public class Categorycreation
{
DBoperations dbo = new DBoperations();
public int _Pdtid;
public string _Pdtctg;
public int _girfrom;
public int _girto;
public int ProductId
{
get
{
return _Pdtid;
}
set
{
_Pdtid = value;
}
}
public string ProductCategory
{
get
{
return _Pdtctg;
}
set
{
_Pdtctg = value;
}
}
public int GirthFrom
{
get
{
return _girfrom;
}
set
{
_girfrom = value;
}
}
public int GirthTo
{
get
{
return _girto;
}
set
{
_girto = value;
}
}
public Categorycreation()
{
}
public int update()
{
int res = dbo.UPDATECATEGORY(this);
return res;
}
}
My stored Procedure
ALTER PROCEDURE SP_Updatecategory(@pdtctg nvarchar(50),@girfrom int,@girto int,@pdtid int)
AS
BEGIN
update Category set ProductCategory=@pdtctg,GirthFrom=@girfrom,GirthTo=@girto where ProductId=@pdtid
END
The Problem is While clicking the update linkbutton the text from the textbox of the gridview is no reading..... that is in aspx.cs pag the following code is not workinge
string txtProductCategory = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtProductCategory")).Text;
string txtGirthFrom = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtGirthFrom")).Text;
string txtGirthTo = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtGirthTo")).Text;
please help me...