Reputation: 160
I am working updating customer details in gridview. Here, I am using 3 tier architecture. I am trying to update certain fields. But, I end up getting above mentioned error. Here is my code.
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();
}
Business Logic Layer code for updating customer.
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);
}
Kindly help me with this. Thanks in advance.
Upvotes: 1
Views: 863
Reputation: 38509
Your SignUpDate
is not being populated, therefore DateTime.MinValue
is being used, which is not valid for SqlDbType.DateTime
However.. the somewhat confusing part to me is that you specify SqlDbType.DateTime2
when adding your parameters.
I suspect when you created your table, you in fact use SqlDbType.DateTime
Anyhow, you'll need to populate the date, something like
Customer customer = new Customer();
customer.SignUpDate = DateTime.Now; //notice this line
customer.CustomerID = customerId;
customer.CustomerName = name.Text;
//etc...
Upvotes: 2