0
con = new SqlConnection(cs);
con.Open();
DateTime current = DateTime.Now;
current = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));
SqlCommand cmd = new SqlCommand(@"INSERT INTO CustomerDetails
(CustomerId, Date, Name, Gender, Address, Phone, Email, MobileNo,Notes)
 VALUES  ('" + txtCustomerID.Text + "','" + current + "','" + 
 txtCustomerName.Text + "','"+Gender+"','"+txtAddress.Text+"','" + txtPhone.Text 
 + "','" + txtEmail.Text+"','" + txtMobileNo.Text + "','" +txtNotes.Text + "')", 
      con);

cmd.ExecuteNonQuery();
con.Close();

I am using this code in database my date has datetime datatype but when I am saving the data through form it is showing an error:

The conversion of a varchar datatype to a datetime data type resulted in an out-of-range value.

What is problem? Why is this error showing up?

2
  • Check your Date field if it really is a Date/Time data type or varchar as suggested by the error. Commented Mar 12, 2014 at 3:30
  • use parameters as Szymon suggested Commented Mar 12, 2014 at 5:15

3 Answers 3

1

You should be using parameters instead of concatenating strings. Not only it will remove the problem with date formats but will also protect you from SQL injection.

SqlCommand cmd = new SqlCommand(@"INSERT INTO CustomerDetails
    (CustomerId, Date, Name, Gender, Address, Phone, Email, MobileNo,Notes)
     VALUES  (@CustomerId, @Date, @Email, @Mobile, @Notes)", con);
cmd.Parameters.Add("@CustomerId", SqlDbType.VarChar, 10).Value = txtCustomerID.Text;
cmd.Parameters.AddWithValue("@Date", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@Email", SqlDbType.VarChar, 10).Value = txtEmail.Text;
cmd.Parameters.Add("@Mobile", SqlDbType.VarChar, 10).Value = txtMobileNo.Text;
cmd.Parameters.Add("@Notes", SqlDbType.VarChar, 10).Value = txtNotes.Text;
Sign up to request clarification or add additional context in comments.

Comments

0

use getdate() or SYSDATETIME() function of sql if you want to store current date.

Try This:

SqlCommand cmd = new SqlCommand(@"INSERT INTO CustomerDetails
(CustomerId, Date, Name, Gender, Address, Phone, Email, MobileNo,Notes)
 VALUES  ('" + txtCustomerID.Text + "',+ getdate() + ,'" + 
 txtCustomerName.Text + "','"+Gender+"','"+txtAddress.Text+"','" + txtPhone.Text 
 + "','" + txtEmail.Text+"','" + txtMobileNo.Text + "','" +txtNotes.Text + "')", 
      con);

Suggestion : your query is open to sql injection attacks so i'd suggest you to use Paramterised Queries to avoid them.

Comments

0

When concatenating your sql string you need to append the date string, not a datetime variable. So unless you have some other reason for the current variable being a date, I would make it a string.

string current = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.