Mike Jonson

Mike Jonson

  • NA
  • 239
  • 190.6k

Error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated

Jun 24 2011 2:47 AM


I add new date in table, but every time error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

I create next method:

public bool AddCredit(Guid id, Guid debitorId, int amount, int balance, DateTime date )
{
string query = string.Format("insert into Credits" + "(ID, DebitorID, Amount, Balance, OpenDate)"
+ "values('{0}', '{1}', '{2}', '{3}', '{4}')", id, debitorId, amount, balance, date);
bool flagResult = false;
using (SqlConnection con = new SqlConnection(conectionString))
{
SqlCommand com = new SqlCommand(query, con);
// try
{
con.Open();
if (com.ExecuteNonQuery() == 1)
flagResult = true;
}
//catch
{ }
}
return flagResult;
}


and add parametr in this method



/// <summary>
/// button new credit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_addNewCredit_Click(object sender, EventArgs e)
{
if (dall.AddCredit(new Guid(tb_creditID.Text), new Guid(lb_debitorID.SelectedValue.ToString()),
Int32.Parse(tb_creditAmount.Text), Int32.Parse(tb_balance.Text), dtp_date.Value))
this.DialogResult = DialogResult.OK;
else
this.DialogResult = DialogResult.Cancel;
}


Answers (7)