GridView行更新事件不会更新数据库,但也不会引发错误,它什么都不做
我使用的是Visual Studio 2015和SQL Server 2014,我编写了代码以在项目的另一种表单上执行所需的代码,因此我决定将现有代码复制并粘贴到新页面上,但是我开始遇到错误,已经令人惊讶,因为它的副本和原始文件工作正常,但我修复了这些问题,但更新按钮仍然没有更改数据库
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Finding the controls from Gridview for the row which is going to update
Label id =(Label)GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
TextBox Plate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Plate") as TextBox;
TextBox Make = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Make") as TextBox;
TextBox Model = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Model") as TextBox;
TextBox Description = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Description") as TextBox;
TextBox Route = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Route") as TextBox;
string ID = id.Text;
string plate = Plate.Text;
string make = Make.Text;
string model = Model.Text;
string Desc = Description.Text;
string route = Route.Text;
SqlConnection con = new SqlConnection("Data Source = SHIREDOCKS\\SQLEXPRESS; Initial Catalog = JerichoCarPark; Integrated Security = True");
con.Open();
// updating the record
SqlCommand cmd = new SqlCommand("VehicleUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("VehicleID", ID);
cmd.Parameters.AddWithValue("VehiclePlateNo", plate);
cmd.Parameters.AddWithValue("VehicleMake", make);
cmd.Parameters.AddWithValue("VehicleModel", model);
cmd.Parameters.AddWithValue("VehicleDescription", Desc);
cmd.Parameters.AddWithValue("JTARoute",route);
cmd.ExecuteNonQuery();
con.Close();
// Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
// Call ShowData method for displaying updated data
ShowData();
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="Yellow" BorderStyle="Solid" BorderWidth="5px" CellPadding="6" Font-Names="Kozuka Gothic Pro B" style="z-index: 3; left: 322px; top: 112px; position: absolute; height: 470px; width: 1167px" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating">
<AlternatingRowStyle BackColor="#99CCFF" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="Edit" BackColor="#3366FF" Font-Names="Century Gothic" ForeColor="White" Font-Size="Medium" BorderStyle="Solid" CausesValidation="True" UseSubmitBehavior="False" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" CommandName="Update" Text="Update" BackColor="#3366FF" Font-Names="Century Gothic" ForeColor="White" BorderStyle="Solid" UseSubmitBehavior="False"/>
<br></br>
<asp:Button ID="btn_Cancel" runat="server" CommandName="Cancel" Text="Cancel" BackColor="#3366FF" Font-Names="Century Gothic" ForeColor="White" BorderStyle="Solid" UseSubmitBehavior="False" />
<br></br>
<asp:Button ID="btn_Delete" runat="server" CommandName="Delete" Text="Delete" BackColor="#3366FF" Font-Names="Century Gothic" ForeColor="White" BorderStyle="Solid" UseSubmitBehavior="False" />
</EditItemTemplate>
</asp:TemplateField>
存储过程:
ALTER PROCEDURE [dbo].[VehicleUpdate]
@VehicleID VARCHAR(50) = '',
@VehiclePlateNo VARCHAR(6) = '',
@VehicleMake VARCHAR(20) = '',
@VehicleModel VARCHAR(50) = '',
@VehicleDescription VARCHAR(225) = '',
@JTARoute VARCHAR(20) = ''
AS
BEGIN
UPDATE Vehicles
SET [VehiclePlateNo] = (@VehiclePlateNo),
[VehicleMake] = (@VehicleMake),
[VehicleModel] = (@VehicleModel),
[VehicleDescription] = (@VehicleDescription),
[JTARoute] = (@JTARoute)
WHERE [VehicleID] = @VehicleID
END
我希望它像原始页面一样更新数据库,但是它只是重新加载并显示未更新的表