winforms1 C# using sql server 2008

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

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

What is problem? Why this error is showing ?

Answers


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.


Need Your Help

Convert String (Json) to Rest Assured Response

java json httpclient httpresponse rest-assured

I have a JSON body as Java String. I would like to convert this String to RestAssured Response. Is this possible?

Calling Soap webservice including WSS from javascript

javascript web-services soap google-chrome-extension ws-security

I was wondering if there would be a technical solution to calling a SOAP webservice that is using client authentication via certificates (xwss ; digital signatures over body, timestamp,