Aktham Mahmoud

Aktham Mahmoud

  • 1.4k
  • 571
  • 5.1k

what is a first a priority?

Jul 8 2020 11:55 PM
Hi
 
I've GrideView has (CRUD) operation using SQLDATASOURCE tool to connect with table, I'm trying to update some columns by rowcommand , but still updating get the value from controls in gridview.
 
See that code below
 
SQLDATACOURCE in code:
  1. <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Products] WHERE [Id] =@Id"  
  2. SelectCommand="SELECT [Id], [P_Name] WHERE ([Id] = @Id)"  
  3. UpdateCommand="UPDATE [Products] SET [P_Name] = @P_Name WHERE [Id] = @Id">  
  4. <DeleteParameters>  
  5. <asp:Parameter Name="Id" Type="Int32" />  
  6. </DeleteParameters>  
  7. <UpdateParameters>  
  8. <asp:Parameter Name="P_Name" Type="String" />  
  9. <asp:Parameter Name="Id" Type="Int32" />  
  10. </UpdateParameters>  
  11. </asp:SqlDataSource>  
GRIDVIEW Code
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2" EmptyDataText="No Products in Selected Category" OnRowCommand="GridView1_RowCommand">  
  2. <Columns>  
  3. <asp:TemplateField HeaderText="Name" SortExpression="P_Name">  
  4. <EditItemTemplate>  
  5. <asp:TextBox ID="TB_NameE" runat="server" Text='<%# Bind("P_Name") %>'></asp:TextBox>  
  6. </EditItemTemplate>  
  7. <ItemTemplate>  
  8. <asp:Label ID="Label4" runat="server" Text='<%# Eval("P_Name") %>'></asp:Label>  
  9. </ItemTemplate>  
  10. </asp:TemplateField>  
  11. <asp:TemplateField>  
  12. <ItemTemplate>  
  13. <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" class="btn btn-outline-success" CommandName="Edit" Text="Edit"></asp:LinkButton>  
  14. <asp:LinkButton ID="btndelete" runat="server" CausesValidation="False" class="btn btn-outline-danger" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?')" Text="Delete"></asp:LinkButton>  
  15. </ItemTemplate>  
  16. <EditItemTemplate>  
  17. <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" class="btn btn-outline-primary" CommandName="Update" Text="Update"></asp:LinkButton>  
  18. <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" class="btn btn-outline-warning" CommandName="Cancel" Text="Cancel"></asp:LinkButton>  
  19. </EditItemTemplate>  
  20. </asp:TemplateField>  
  21. </Columns>  
  22. </asp:GridView>  
Code behind code :
  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3. using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  4. if (e.CommandName == "Update")  
  5. {  
  6. try  
  7. {  
  8. GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);  
  9. int rowid = gvr.RowIndex;  
  10. //Open Connection with D.B  
  11. sqlcon.Open();  
  12. string query = "UPDATE Products SET P_Name = @P_Name WHERE (Id = @Id)";  
  13. {  
  14. SqlCommand sqlcmd = new SqlCommand(query, sqlcon);  
  15. sqlcmd.Parameters.AddWithValue("@P_Name""A1");  
  16. //=============================================  
  17. //I Disabled that row just to check if update table is working by add last value "A1", but still get it from textbox  
  18. //sqlcmd.Parameters.AddWithValue("@P_Name", (GridView1.Rows[rowid].FindControl("TB_NameE") as TextBox).Text);  
  19. sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(GridView1.DataKeys[rowid].Value.ToString()));  
  20. sqlcmd.ExecuteNonQuery();  
  21. GridView1.EditIndex = -1;  
  22. GridView1.DataBind();  
  23. }  
  24. }//End Try  
  25. catch (Exception ex)  
  26. {  
  27. LBgvFi.Text = ex.Message;  
  28. }  
  29. }  
  30. }  
my question is: what is a first a priority?
 
1) textbox has DataBind("Name");
 
Or
 
2) Codebehind in c#
 
but why is still update a column by Textbox value. Until if I implement it by rowcommand event.
 
Thanks

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)