Kann ich einen Code zum Konvertieren von ASP.NET (DateTimeFormat) in das SqlDateTime-Format schreiben?Konvertieren von Datetime-Format in SQL?
ich diesen Fehler:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Mein Code:
if (Session["user"] == null)
{
//redirect to the loginform.aspx
Response.Redirect("LoginForm.aspx");
}
//session of cart is not null
if (Session["cart"] != null)
{
ShoppingCart sc = (ShoppingCart)Session["cart"];
tbxId.Text = sc.Seminar.Id;
tbxCategoryName.Text = sc.Seminar.CategoryName;
tbxTitle.Text = sc.Seminar.Name;
tbxDesc.Text = sc.Seminar.Description;
tbxDuration.Text = sc.Seminar.Duration.ToString();
tbxDate.Text = sc.Seminar.Schedule.Date.ToShortDateString();
lblAmount.Text = sc.Seminar.Price.ToString("C");
// How can I convert this date time format to become sql?
lblDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
else
{
lblOutput0.Text = "No seminar selected";
}
Das ist mein Warenkorb Datenbank-Manager, wo es von hier ausgeführt werden können.
public static int purchaseSeminar(ShoppingCart sc)
{
int id = -1;
SqlConnection con = new SqlConnection(conStr);
try
{
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "insert into Payment (payment_id, payment_name, payment_ccNo, payment_ccType, payment_ccCode, payment_expDate, payment_price, payment_optionPay, payment_date ,reg_id) values (@payment_id, @payment_name, @payment_ccNo, @payment_ccType, @payment_ccCode, @payment_expDate, @payment_price, @payment_optionPay, @payment_date ,@reg_id)";
command.Parameters.AddWithValue("@payment_id", sc.Id);
command.Parameters.AddWithValue("@payment_name", sc.Member.Name);
command.Parameters.AddWithValue("@payment_ccNo", sc.CreditCard);
command.Parameters.AddWithValue("@payment_ccType", sc.CreditCardType);
command.Parameters.AddWithValue("@payment_ccCode", sc.SecurityCode);
command.Parameters.AddWithValue("@payment_expDate", sc.CCExpiryDate);
command.Parameters.AddWithValue("@payment_price", sc.TotalAmount);
command.Parameters.AddWithValue("@payment_optionPay", sc.OptionPay);
command.Parameters.AddWithValue("@payment_date", sc.Date);
con.Open();
if (command.ExecuteNonQuery() > 0)
{
command.CommandText = "Select @@identity"; //it can't execute because the date time format is overflow
id = Convert.ToInt32(command.ExecuteScalar());
}
return id;
}
finally
{
con.Close();
}
}
was ist die Ausnahmemeldung? –
Ich bin mir nicht sicher, was hat das Setzen des 'label'-Textes mit der Konvertierung in SQL zu tun? Der Fehler tritt an anderer Stelle auf, entweder "sc.CCExpiryDate" oder "sc.Date". Konvertieren Sie das einfach in "DateTime", wenn sie "string" sind. – 12seconds
Was ist der tatsächliche Wert Ihres DateTime-Werts? Die Ausnahme ist sehr klar, was falsch ist, es sei denn, Ihr Wert liegt im Bereich, aber trotzdem wird die Ausnahme ausgelöst. –