未使用GridView在数据库中更新行

时间:2018-11-06 19:18:15

标签: c# gridview webforms

我正在尝试通过网格视图图像按钮更新数据库。但是,更改未更新。所有其他操作均有效,即删除,添加新内容,取消。下面是我后面的GridView和C#代码的代码。我哪里出错了?

GridView声明

 <asp:GridView ID="gvFarmer" runat="server"
        BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="5" style="margin-right: 58px" DataKeyNames="Farmer_Id"
        CellSpacing="1" GridLines="None" AutoGenerateColumns="false" Height="166px" Width="692px" ShowFooter="true" ShowHeaderWhenEmpty="true" 

        OnRowCommand="gvFarmer_RowCommand" OnRowEditing="gvFarmer_RowEditing" OnRowCancelingEdit="gvFarmer_RowCancelingEdit" OnRowUpdating="gvFarmer_RowUpdating" 
        OnRowDeleting="gvFarmer_RowDeleting">

按钮列      

                <ItemTemplate>
                    <asp:ImageButton  ImageUrl="~/Images/Edit.jpg" runat="server" CommandName="Edit" ToolTip="Edit Row" Height="20px" Width="20px"/>
                     <asp:ImageButton  ImageUrl="~/Images/Delete.jpg" runat="server" CommandName="Delete" ToolTip="Delete" Height="20px" Width="20px"/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:ImageButton  ImageUrl="~/Images/Save.jpg" runat="server" CommandName="Save" ToolTip="Save" Height="20px" Width="20px"/>
                     <asp:ImageButton  ImageUrl="~/Images/Cancel.jpg" runat="server" CommandName="Cancel" ToolTip="Cancel" Height="20px" Width="20px"/>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:ImageButton  ImageUrl="~/Images/Add New.jpg" runat="server" CommandName="AddNew" ToolTip="Add New" Height="20px" Width="20px"/>

                </FooterTemplate>

            </asp:TemplateField>

下面的更新功能

 protected void gvFarmer_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                con.Open();

                string InsertQuery = "UPDATE Farmer SET FirstName = @FirstName, LastName = @LastName, DOB = @DOB, Address = @Address, Gender = @Gender WHERE Farmer_Id = @Farmer_Id";
                //parametrized variables are used to prevent sql injection

                SqlCommand insert = new SqlCommand(InsertQuery, con);

                insert.Parameters.AddWithValue("@Farmer_Id", Convert.ToInt32(gvFarmer.DataKeys[e.RowIndex].Value.ToString().Trim()));
                //get the info from textbox, trim spaces and store it in appropirate fields in the database.

                insert.Parameters.AddWithValue("@FirstName", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerFirstName") as TextBox).Text.Trim());

                insert.Parameters.AddWithValue("@LastName", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerLastName") as TextBox).Text.Trim());

                insert.Parameters.AddWithValue("@DOB", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerDOB") as TextBox).Text.Trim());

                insert.Parameters.AddWithValue("@Address", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerAddress") as TextBox).Text.Trim());

                insert.Parameters.AddWithValue("@Gender", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerGender") as TextBox).Text.Trim());

                insert.ExecuteNonQuery(); //function executes the insert query

                gvFarmer.EditIndex =1;
                PopulateGridView(); //function is called to show updated view.

                lblSuccess.Text = "Record Updated!";
                lb1Error.Text = "";
            }//using block ends here

        }
        catch (Exception ex)
        {

            lblSuccess.Text = "";
            lb1Error.Text = ex.Message;
        }//end of try catch
    }//end of row updating function

1 个答案:

答案 0 :(得分:0)

您应在保存按钮上使用 Update 作为命令名称

<asp:ImageButton  ImageUrl="~/Images/Save.jpg" runat="server" CommandName="Update" ToolTip="Save" Height="20px" Width="20px"/>