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:
- <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Products] WHERE [Id] =@Id"
- SelectCommand="SELECT [Id], [P_Name] WHERE ([Id] = @Id)"
- UpdateCommand="UPDATE [Products] SET [P_Name] = @P_Name WHERE [Id] = @Id">
- <DeleteParameters>
- <asp:Parameter Name="Id" Type="Int32" />
- </DeleteParameters>
- <UpdateParameters>
- <asp:Parameter Name="P_Name" Type="String" />
- <asp:Parameter Name="Id" Type="Int32" />
- </UpdateParameters>
- </asp:SqlDataSource>
GRIDVIEW Code
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2" EmptyDataText="No Products in Selected Category" OnRowCommand="GridView1_RowCommand">
- <Columns>
- <asp:TemplateField HeaderText="Name" SortExpression="P_Name">
- <EditItemTemplate>
- <asp:TextBox ID="TB_NameE" runat="server" Text='<%# Bind("P_Name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label4" runat="server" Text='<%# Eval("P_Name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" class="btn btn-outline-success" CommandName="Edit" Text="Edit"></asp:LinkButton>
- <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>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" class="btn btn-outline-primary" CommandName="Update" Text="Update"></asp:LinkButton>
- <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" class="btn btn-outline-warning" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
- </EditItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
Code behind code :
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- using (SqlConnection sqlcon = new SqlConnection(connectionString))
- if (e.CommandName == "Update")
- {
- try
- {
- GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
- int rowid = gvr.RowIndex;
-
- sqlcon.Open();
- string query = "UPDATE Products SET P_Name = @P_Name WHERE (Id = @Id)";
- {
- SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
- sqlcmd.Parameters.AddWithValue("@P_Name", "A1");
-
-
-
- sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(GridView1.DataKeys[rowid].Value.ToString()));
- sqlcmd.ExecuteNonQuery();
- GridView1.EditIndex = -1;
- GridView1.DataBind();
- }
- }
- catch (Exception ex)
- {
- LBgvFi.Text = ex.Message;
- }
- }
- }
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