Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using sqlserver, three tier architecture and store procedure for this...

My aspx Page


ASP.NET
<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;

/// <summary>
/// Summary description for DBoperations
/// </summary>
public class DBoperations
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Micro;Integrated Security=True");
    SqlCommand cmd = null;
	public DBoperations()
	{
		//
		// TODO: Add constructor logic here
		//
	}

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)

CSS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for Categorycreation
/// </summary>
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()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public int update()
    {
        int res = dbo.UPDATECATEGORY(this);
        return res;
    }
}


My stored Procedure

SQL
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
C#
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...
Posted
Updated 19-May-14 1:59am
v5
Comments
Murugesan22 14-May-14 7:11am    
Can you post aspx Page full source
Ajesh1986 21-May-14 2:52am    
i have posted the full page can u check that for me
Ajesh1986 14-May-14 7:57am    
posted...
ZurdoDev 14-May-14 8:19am    
1. You need to reply to the comment so that the user is notified.
2. Please do not post everything. Just post the relevant code.
3. It sounds like your only issue is .FindControl is not finding what you need? Instead of using FindControl use the debugger and exam the .Controls collection. FindControl is not recursive.
Ajesh1986 14-May-14 10:02am    
ok Thank you

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblProductId = (Label)row.FindControl("lblProductId");
obj.ProductId = Convert.ToInt32(lblProductId);
int res = obj.delete();
Gridviewdatabind();


try this.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 19-May-14 4:08am    
I think that you should explain why that!
The first version (from OP) utilizing the Control.FindControl() method, that can not be used to find controls from template.
The second version will call the GridViewRow.FindControl() method, that was created specifically for this scenario.
Ajesh1986 19-May-14 8:45am    
I also agree with Kornfeld You should explain why GridViewRow.FindControl()......my code is working fine for Label control....but Textbox control is not returning value...
Ajesh1986 19-May-14 9:56am    
could you please check if there is any problem with my HTML page
one way to find the control is naming container in the event.. may this help..

GridViewRow row = ((linkbutton)sender ).NamingContainer as GridViewRow;
and then Find the Controls using
TextBox name = row.FindControl("--yourHTMLID--") as TextBox ;

and then name.Text and so on... may this Help
 
Share this answer
 
v3
Comments
Ajesh1986 19-May-14 8:46am    
No, Thats not working brother...
A.R Farooqui 20-May-14 0:38am    
Whats is the problem then??
A.R Farooqui 20-May-14 0:48am    
your labels and textboxes should contains their closing tags ... and your EVAL method contain an extra '> .... may this be the problem ... <br>
 <br>
or <br>
TextBox txtProductCategory = GridView1.Rows[e.RowIndex]<br>
.FindControl("txtProductCategory") as TextBox;<br>
and debug and then check that if your textbox control is null?<br>
if it is null then there is some thing wrong with the declaration of your textboxes..


or Use Bind instead of Eval..
Ajesh1986 20-May-14 10:28am    
My labels and textboxes has closing tags....that extra '> is the editing problem of codeproject...my code does not have that extra.........that txtProductCtegory is not getting the updaed value only the old value is reching there.....i have also tried with Bind<br>
.....Nothing working<br>
Ajesh1986 20-May-14 10:29am    
could you please check my HTML page

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900