Orion Orion - 3 months ago 24
ASP.NET (C#) Question

Additional information: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

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.

Answer

Your SignUpDate is not being populated, therefore DateTime.MinValue is being used, which is not valid for SqlDbType.DateTime2

You need to populate, something like

    Customer customer = new Customer();
    customer.SignUpDate = DateTime.Now; //notice this line
    customer.CustomerID = customerId;
    customer.CustomerName = name.Text;
    //etc...
Comments