Insert query with datetime

Posted on 2007-07-25
Last Modified: 2012-06-21
Hi, I am working on MSSQL, C#, ASP.Net 1.1

I have DataTime in variable
DateTime dtContactDate = txtContactDate.SelectedDate;
having the value  = 7/25/2007

I create a insert query string at runtime so I get the insert query in the form

INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])Values (430,'25/07/2007 10:33:18','Test');

Now, I changed above DateTime to string as below

string strContactDate = dtContactDate.ToShortDateString();

I get the value "25/07/2007"

and then I get my inserts statement generated as below

INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])Values (430,'25/07/2007','Test');

In both the cases when I execute the insert query, I get the error below:

Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

In C#, I create the query as below

strQuery = strQuery+"INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])"+
"Values ("+strCustomerId+",'"+strContactDate+"','"+strRemarks+"');";

How to solve the problem

Please help
Question by:tia_kamakshi
    1 Comment
    LVL 75

    Accepted Solution

    INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])Values (430,'2007-07-25','Test');

    INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])Values (430,'25-July-2007','Test');


    INSERT INTO SleepingCustomerContactDates([CustomerId],[ContactedDate],[Remarks])Values (430,'25/07/2007','Test');


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now