[英]asp.net update doesn't work
I try to do a update in a formview but it doesn't work. 我尝试在formview中进行更新,但不起作用。 I add a breakpoint at the begin of the update methode, and he goes perfectly trough the whole methode. 我在update methode的开头添加了一个断点,而他在整个methode上都处于完美状态。 But a classmate told me that at the end of the methode (after "return i") he has to go to the BO.orders.cs, and he doesn't do that. 但是一位同学告诉我,在方法结束时(“返回i”之后),他必须去BO.orders.cs,但他没有这样做。 I don't get any errors. 我没有任何错误。 Is there somebody who can help me? 有没有人可以帮助我? (Sorry for my bad english) (对不起,我的英语不好)
This is my update methode and GetConnectionString 这是我的更新方法和GetConnectionString
public static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings
["BookstoreConnectionString"].ConnectionString;
}
public static int UpdateOrder(Order order)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "UPDATE tblOrders " +
"SET Name = @name, Address = @address, " +
"City = @city, Pc = @pc, Date = @date, Book_id = @book_id, Count = @count, Amount = @amount, " +
"Delivered = @delivered, Ddate = @ddate, Paid = @paid, Pdate = @pdate " +
" WHERE Id = @Id";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", order.OrderId);
cmd.Parameters.AddWithValue("@name", order.Name);
cmd.Parameters.AddWithValue("@address", order.Address);
cmd.Parameters.AddWithValue("@city", order.City);
cmd.Parameters.AddWithValue("@pc", order.Pc);
cmd.Parameters.AddWithValue("@date", order.Date);
cmd.Parameters.AddWithValue("@book_id", order.Bookid);
cmd.Parameters.AddWithValue("@count", order.Count);
cmd.Parameters.AddWithValue("@amount", order.Amount);
cmd.Parameters.AddWithValue("@delivered", order.Delivered);
cmd.Parameters.AddWithValue("@paid", order.Paid);
cmd.Parameters.AddWithValue("@ddate", order.Ddate);
cmd.Parameters.AddWithValue("@pdate", order.Pdate);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
And this is my objectdatasource of my formview 这是我的formview的objectdatasource
<asp:ObjectDataSource ID="dtsDetail" runat="server" DataObjectTypeName="BO.Order" DeleteMethod="DeleteOrder" InsertMethod="InsertOrder" OldValuesParameterFormatString="original_{0}" SelectMethod="GetOrdersByOrderId" TypeName="DAL.OrdersDAL" UpdateMethod="UpdateOrder">
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Amount" Type="Decimal" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="gv" Name="ID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Thanks for helping 感谢您的帮助
If the value of "i" is 0 (as mentioned in the comment) after you've called ExecuteNonQuery(), that means 0 rows were updated after executing your sql query. 如果在调用ExecuteNonQuery()之后“ i”的值为0(如注释中所述),则意味着执行SQL查询后更新了0行。
Basically, your update statement ( string sql = "UPDATE tblOrders "
...) is looking for a row that has an id of whatever the value of order.OrderID
is (... " WHERE Id = @Id";
cmd.Parameters.AddWithValue("@id", order.OrderId);
) and it cannot find a matching row, so it doesn't update anything. 基本上,您的update语句( string sql = "UPDATE tblOrders "
...)正在查找具有与order.OrderID
值order.OrderID
的id的行order.OrderID
为(... " WHERE Id = @Id";
cmd.Parameters.AddWithValue("@id", order.OrderId);
),它找不到匹配的行,因此不会更新任何内容。 So, to get the update working, you will need to provide an id that exists. 因此,要使更新生效,您将需要提供一个存在的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.