Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETC#3.5
Dear experts ,
plz solve my problem..i have tried so lot..but not geting..plz...help me..
this is my code.
sorce code.
 
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false"
            AllowPaging="true" PageSize="5"
             OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" OnRowUpdating="gvdetails_RowUpdating" DataKeyNames="UserId,UserName"
              OnRowCancelingEdit="gvdetails_RowCancelingEdit"  OnPageIndexChanging="gvdetails_PageIndexChanging">
 
        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
        <Columns>
        <asp:TemplateField>
        <EditItemTemplate>
        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Button cancel Icon.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
        </EditItemTemplate>
        <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit Icon.jpg" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
        </ItemTemplate>
        </asp:TemplateField>
 
        <%--<asp:TemplateField HeaderText="UserId">
        <ItemTemplate>
        <asp:Label ID="lblitmid" runat="server" Text='<%#Eval("UserId") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>--%>
 
       <asp:TemplateField HeaderText="UserName">
       <ItemTemplate>
       <asp:Label ID="lblitmusr" runat="server" Text='<%#Eval("UserName")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <%--<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>--%>
       <asp:TextBox ID="txteditusr" runat="server" Text='<%#Eval("UserName") %>'></asp:TextBox>
       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="City">
       <ItemTemplate>
       <asp:Label ID="lblitmcity" runat="server" Text='<%#Eval("City") %>' ></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <%--<asp:Label ID="lbleditcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>--%>
       <asp:TextBox ID="txteditcity" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Designation">
       <ItemTemplate>
       <asp:Label ID="lblitmdesignation" runat="server" Text='<%#Eval("Designation")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <%--<asp:Label ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:Label>--%>
       <asp:TextBox ID="txteditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:TextBox>
       </EditItemTemplate>
       </asp:TemplateField>
 
        </Columns>
 
        </asp:GridView>
        <asp:Label ID="lblresult" runat="server"></asp:Label>
 

aspx.cs code
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
 

 
    }
    protected void BindGridView()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from sample", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
            int columncount = gvdetails.Rows[0].Cells.Count;
            gvdetails.Rows[0].Cells.Clear();
            gvdetails.Rows[0].Cells.Add(new TableCell());
            gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdetails.Rows[0].Cells[0].Text = "No Records Found";
 
        }
 

    }
 
    protected void gvdetails_RowUpdating(object sender,GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)gvdetails.Rows[e.RowIndex];
        int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Value.ToString());
        string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        TextBox txtcity = (TextBox)row.FindControl("lbleditcity");
        //TextBox txtcity=(TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtcity");
        TextBox txtDesignation = (TextBox)row.FindControl("lbleditdesignation");
        //TextBox txtDesignation=(TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtDesignation");
        con.Open();
        SqlCommand cmd = new SqlCommand("update sample set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where Userid=" + userid, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.Text=username + "Details Updated Successfully";
        gvdetails.EditIndex = -1;
        BindGridView();
       
        
 

 
    }
   
    protected void gvdetails_RowEditing(object sender,GridViewEditEventArgs e )
    {
        gvdetails.EditIndex = e.NewEditIndex;
        BindGridView();
    }
 
    protected void gvdetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvdetails.EditIndex = -1;
        BindGridView();
    }
    protected void gvdetails_RowDeleting(object sender,GridViewDeleteEventArgs e)
    {
 

        int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from sample where UserId=" + userid, con);
        //SqlCommand cmd = new SqlCommand("delete from Grid where UserId=" + userid, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
       
        if (result == 1)
        {
            BindGridView();
            lblresult.Text = username + "Details deleted successfully";
 
        }
    }
    protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvdetails.PageIndex = e.NewPageIndex;
        BindGridView();
 
    }
And database table
 
UserId int Unchecked
UserName varchar(50) Checked
City varchar(50) Checked
Designation varchar(50) Checked
Unchecked
Posted 7-Sep-12 23:55pm
Edited 7-Sep-12 23:58pm
__TR__28.8K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Replace these two lines
 TextBox txtcity = (TextBox)row.FindControl("lbleditcity");
 TextBox txtDesignation = (TextBox)row.FindControl("lbleditdesignation");
with
TextBox txtcity = (TextBox)row.FindControl("txteditcity");
TextBox txtDesignation = (TextBox)row.FindControl("txteditdesignation");
in gvdetails_RowUpdating().
 
That should fix the problem.
  Permalink  
Comments
Member 9376025 at 8-Sep-12 6:18am
   
Thanks alot TR..
__TR__ at 8-Sep-12 6:30am
   
You are welcome :) On a different note i would like to point out that your code is prone to SQL Injection[^]. So i would suggest you to use parameterized queries. Take a look at the below links. You will get an idea of what i am talking about. using-parameters-parameterized-queries-database-interactions-csharp[^] Using a Parameterized Query[^] Adding Parameters to Commands[^] C# SqlParameter[^]
Member 9376025 at 8-Sep-12 6:55am
   
thanks..

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 895
1 OriginalGriff 523
2 thatraja 245
3 Abhinav S 243
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid