How to store datetime into sql database?
I get this exception when I try to save a date into sql database
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.
SQL statement :
SqlCommand cmd = new SqlCommand("insert into tbl_purshaseOrders (po_currency,po_paymentTerms,po_deliverydate,po_projectId,po_supplierId,po_notes,po_expiryDate) values (@po_curr开发者_如何学JAVAency,@po_paymentTerms,@po_deliverydate,@po_deliverydate,@po_projectId,@po_notes,@po_expiryDate)", conn);
cmd.CommandType = CommandType.Text;
MessageBox.Show("" + dp_deliveryDate.Text);
cmd.Parameters.AddWithValue("@po_currency", CB_currency.Text);
cmd.Parameters.AddWithValue("@po_paymentTerms", T_paymentTerms.Text.Trim());
cmd.Parameters.AddWithValue("@po_deliverydate", dp_deliveryDate.Text);
cmd.Parameters.AddWithValue("@po_expiryDate", dp_expiryDate.Text);
cmd.Parameters.AddWithValue("@po_projectId", Int32.Parse(T_project.Text.Trim()));
cmd.Parameters.AddWithValue("@po_supplierId", Int32.Parse(T_supplier.Text.Trim()));
cmd.Parameters.AddWithValue("@po_notes", T_notes.Text.Trim());
int val = cmd.ExecuteNonQuery();
if (val > 0)
{
MessageBox.Show("Added Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
// resetFields();
GetDate();
}
dp_deliveryDate
anddp_expiryDate
are datepicker fields and have a custom dateformatdd/MM/yyyy
**po_deliveryDate
andpo_expiryDate
columns are of datetime data type
You should extract the selected Date from the date pickers, not the text
Specify the parameter type:
cmd.Parameters.Add("@ParameterName", SqlDbType.DateTime).Value = "9/15/2011 11:30 AM";
After modifying your code check this :-
enter code hereSqlCommand cmd = new SqlCommand("insert into tbl_purshaseOrders (po_currency,po_paymentTerms,po_deliverydate,po_projectId,po_supplierId,po_notes,po_expiryDate) values (@po_currency,@po_paymentTerms,@po_deliverydate,@po_deliverydate,@po_projectId,@po_notes,@po_expiryDate)", conn);
cmd.CommandType = CommandType.Text;
MessageBox.Show("" + dp_deliveryDate.Text);
cmd.Parameters.AddWithValue("@po_currency", CB_currency.Text);
cmd.Parameters.AddWithValue("@po_paymentTerms",T_paymentTerms.Text.Trim());
cmd.Parameters.AddWithValue("@po_deliverydate",Convert.ToDateTime(dp_deliveryDate.Text).toLongDateString());
cmd.Parameters.AddWithValue("@po_expiryDate", dp_expiryDate.Text);
cmd.Parameters.AddWithValue("@po_projectId", Int32.Parse(T_project.Text.Trim()));
cmd.Parameters.AddWithValue("@po_supplierId", Int32.Parse(T_supplier.Text.Trim()));
cmd.Parameters.AddWithValue("@po_notes", T_notes.Text.Trim());
int val = cmd.ExecuteNonQuery();
if (val > 0)
{
MessageBox.Show("Added Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
// resetFields();
GetDate();
}
Or
It would be problem of date format also check sending value format same as mentioned in column.
The database doesn't expect that date format. Parse the string in the .NET code:
DateTime deliveryDate = DateTime.ParseExact(dp_deliveryDate.Text, "dd'/'MM'/'yyyy", CultureInfo.InvariantCulture);
Then use the DateTime value in the parameter:
cmd.Parameters.AddWithValue("@po_deliverydate", deliveryDate);
If you are only ever looking at the DateTime in C#, you could store the DateTime as Ticks (see DateTime.Ticks). It's probably better to store the DateTime as a SQL DateTime object, however, so you can read and perform sophisticated queries against it in your database.
精彩评论