1

I want to update only selected row from my grid view when i press linked button but it is not working on my end .

Here is my Design Page

<asp:GridView ID="grdCompanyUsers" runat="server"
   DataKeyNames="id_company_user,nm_company_username"
   AutoGenerateColumns="false" GridLines="None" CssClass="grid" AlternatingRowStyle-
   BackColor="#DDE0EF" OnRowDataBound="grdCompanyUsers_DataBound">
  <Columns>
  <asp:TemplateField>
  <EditItemTemplate>
  <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images
  /Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="CompUserID" runat="server" Width="15"  
   Text='<%#Eval("id_company_user")%>'> </asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField>
  <ItemTemplate>
  <asp:Label ID="companyusername" runat="server" Width="51" 
   Text='<%#Eval("nm_company_username")%>'></asp:Label>
   </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField>
   <ItemTemplate>
   <asp:Label ID="compName" runat="server" Width="56" Text='<%#Eval("nm_company_name")%>'>
   </asp:Label>
   </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField>
   <ItemTemplate>
   <asp:Label ID="compDesc" runat="server" Width="129" Text='<%#Eval("nm_company_desc")%>'>
   </asp:Label>
   </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField>
   <ItemTemplate>
   <asp:Label ID="compEmail" runat="server" Width="103px" 
    Text='<%#Eval("nm_company_email_address")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="compAddress" runat="server" Width="153px" 
     Text='<%#Eval("nm_company_address")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="chkBoxStatus" runat="server" Width="15px" Enabled="false" 
     Text='<%#Eval("ind_active")%>'>
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="btnEdit" runat="server" Font-Underline="false" 
     CommandArgument='<%#Eval ("id_company_user")%>'
     OnClick="btnEdit_Click">Edit</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton runat="server" ID="lnkDeny" Font-Underline="false" CommandName="Deny" 
     CommandArgument='<%# Eval("id_company_user") %>'
     OnClick="btnDeny_Click">Deny</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

Here is my code behind the aspx page

protected void btnEdit_Click(object sender, EventArgs e)
  { 
       LinkButton btn = (LinkButton)sender;
       GridViewRow row = (GridViewRow)btn.NamingContainer;
        int i = Convert.ToInt32(row.RowIndex);
       _connString = ConfigurationManager.AppSettings["connString"];
       using (SqlConnection conn = new SqlConnection(_connString))
             {
                conn.Open(); 
                SqlCommand cmd = new SqlCommand("update ref_registration_company_user 
                set ind_active=1 where id_company_user=id_company_user", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }      

   }

Here is Design View :

enter image description here

I just want that only selected row should be affected in database. I will be thank full for help.

senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
Ammar Asjad
  • 2,920
  • 6
  • 29
  • 42

3 Answers3

2

you need to set the id_company_user value in your sql statement. use parameters as below.

SqlCommand cmd = new SqlCommand("update ref_registration_company_user set ind_active=1 where id_company_user=@id_company_user", conn);
cmd.Parameters.AddWithValue("@id_company_user", id);

you need to get current row id_company_user value check below SO question and answer, you can use OnRowCommand of GridView and CommandArgument property

GridView: Get datakey of the row on button click

<asp:GridView ID="grdCompanyUsers" runat="server" DataKeyNames="id_company_user,nm_company_username" AutoGenerateColumns="false" GridLines="None" CssClass="grid" AlternatingRowStyle-BackColor="#DDE0EF" OnRowDataBound="grdCompanyUsers_DataBound" 
       OnRowCommand="myGridView_RowCommand">
     <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="btnEdit" runat="server" Font-Underline="false" CommandArgument='<%#Eval ("id_company_user")%>' CommandName="Edit">Edit</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
     </Columns>
</asp:GridView>

code-behind:

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    var id= int.Parse(e.CommandArgument);
    _connString = ConfigurationManager.AppSettings["connString"];
    using (SqlConnection conn = new SqlConnection(_connString))
    {
        conn.Open(); 
        using(SqlCommand cmd = new SqlCommand("update ref_registration_company_user set ind_active=1 where id_company_user=id_company_user", conn))
        {
           cmd.Parameters.AddWithValue("@id_company_user", id);
           cmd.ExecuteNonQuery();
        }
    }  
}
Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Yeah ! It's working now :) just add string CompanyUserId = btn.CommandArgument; SqlCommand cmd = new SqlCommand("update ref_registration_company_user set ind_active=1 where id_company_user=@id_company_user", conn); cmd.Parameters.AddWithValue("@id_company_user", CompanyUserId); – Ammar Asjad Oct 12 '13 at 05:56
1

Can you please check once your update query where clause,

Gurunadh
  • 453
  • 2
  • 10
  • 24
  • 1
    where id_company_user=id_company_user, here you are comparing data key name with data key name only, where you need to pass parameter to compare. – Gurunadh Oct 12 '13 at 05:46
0

Check the sqlDataSource control for this problem, you can add the select and update command for populate and update rows your GridView on there.

Maybe this example could be you useful http://asp-net-example.blogspot.mx/2008/12/aspnet-gridview-and-sqldatasource.html

JuanCrg90
  • 1,006
  • 13
  • 24