我正在使用gridview更新客户详细信息。在这里,我使用3层架构。我正在尝试更新某些字段。但是,我最终得到了上面提到的错误。 这是我的代码。
protected void MyProfileGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int customerId = Convert.ToInt32(Session["CustomerID"]);
TextBox name = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_Name");
TextBox shopName = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_ShopName");
TextBox address = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_Address");
TextBox mobile1 = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_Mobile1");
TextBox mobile2 = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_Mobile2");
TextBox password = (TextBox)MyProfileGridView.Rows[e.RowIndex].FindControl("txt_Password");
Customer customer = new Customer();
customer.CustomerID = customerId;
customer.CustomerName = name.Text;
customer.ShopName = shopName.Text;
customer.Address = address.Text;
customer.Mobile1 = mobile1.Text;
customer.Mobile2 = mobile2.Text;
customer.Password = password.Text;
CustomerBL.UpdateCustomer(customer);
MyProfileGridView.EditIndex = -1;
MyProfileGridView.DataSource = CategoryBL.GetCategories();
MyProfileGridView.DataBind();
}
用于更新客户的业务逻辑层代码。
public static void UpdateCustomer(Customer customer)
{
string query = "UPDATE [Customers] SET [LoginID] = @LoginID, [Password] = @Password, [CustomerName] = @CustomerName, [ShopName] = @ShopName, [Address] = @Address, [Mobile1] = @Mobile1, [Mobile2] = @Mobile2, [ReferenceNumber] = @ReferenceNumber, [SignUpDate] = @SignUpDate, [Enabled] = @Enabled WHERE [CustomerID] = @CustomerID";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@LoginID", SqlDbType.Text).Value = customer.LoginID;
cmd.Parameters.AddWithValue("@Password", SqlDbType.Text).Value = customer.Password;
cmd.Parameters.AddWithValue("@CustomerName", SqlDbType.Text).Value = customer.CustomerName;
cmd.Parameters.AddWithValue("@ShopName", SqlDbType.Text).Value = customer.ShopName;
cmd.Parameters.AddWithValue("@Address", SqlDbType.Text).Value = customer.Address;
cmd.Parameters.AddWithValue("@Mobile1", SqlDbType.Text).Value = customer.Mobile1;
cmd.Parameters.AddWithValue("@Mobile2", SqlDbType.Text).Value = customer.Mobile2;
cmd.Parameters.AddWithValue("@ReferenceNumber", SqlDbType.Text).Value = customer.ReferenceNumber;
cmd.Parameters.AddWithValue("@SignUpDate", SqlDbType.DateTime2).Value = customer.SignUpDate;
cmd.Parameters.AddWithValue("@Enabled", SqlDbType.Bit).Value = customer.Enabled;
cmd.Parameters.AddWithValue("@CustomerID", SqlDbType.Text).Value = customer.CustomerID;
DbUtility.UpdateDb(cmd);
}
请帮助我。提前致谢。
答案 0 :(得分:1)
您的SignUpDate
未填充,因此正在使用DateTime.MinValue
,这对SqlDbType.DateTime
无效
但是..对我来说有点令人困惑的部分是你在添加参数时指定SqlDbType.DateTime2
我怀疑你创建表时,实际上是使用SqlDbType.DateTime
无论如何,您需要填写日期,例如
Customer customer = new Customer();
customer.SignUpDate = DateTime.Now; //notice this line
customer.CustomerID = customerId;
customer.CustomerName = name.Text;
//etc...