Solved

Calendar Control and ASP.net

Posted on 2013-01-18
6
638 Views
Last Modified: 2013-01-19
I'm trying to use the Calendar control within Web Developer Express and output the result to a datetime field in SQL 2005, however I keep getting the same error:

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

I'm new to this so my code is probably well wrong, can someone please help:

strDate = Format(CDate(Calendar1.SelectedDate), "dd-mm-yyyy HH:mm:ss")
0
Comment
Question by:ndr-itsolutions
6 Comments
 
LVL 19

Expert Comment

by:Rikin Shah
Comment Utility
Hi,

You can either use Convert.ToDateTime() or DateTime.TryParseExact() try using them.
0
 
LVL 6

Expert Comment

by:esolve
Comment Utility
You could also try

DateTime.ParseExact(yourdate, "dd/MM/yyyy", CultureInfo.InvariantCulture)

More Info here:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

Just remember that depending on the datetime settings on your machine it expects the date in a specific order. For example if your machine is localized in China it would have the format dd/MM/YYYY. Usually the problem with converting is because the day and month values is swopped around which causes the error.

Also if you're trying to convert by including timeformats (HH:mm:ss) as well you will have to use the ParseExact mentod.

For normal conversions you can use date and time styles. More info here:

http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 

Author Comment

by:ndr-itsolutions
Comment Utility
So, strDate is what I am passing the date selected to , then to be written into the database, how would I change the syntax to do that ?

My code is:
Dim SelDate = Calendar1.SelectedDate

Open in new window



sqlcon.Open()
        Dim strcommand As String
     
        strcommand = "Insert into Release_Tracker(Environment,Current_Version,Component,SubComponent,Resource,Status,DateCreated,Comments,Project_name,Software_Location) values ('" + strEnv + "','" + strBuild + "','" + strComponent + "','" + strSubComponent + "','" + strResource + "','" + strStatus + "','" + SelDate + "','" + strComments + "','" + strProject + "','" + strSoftware + "')"
        Dim sqlcomm As New SqlCommand(strcommand, sqlcon)
        Dim o As String = sqlcomm.ExecuteNonQuery()

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Assisted Solution

by:esolve
esolve earned 250 total points
Comment Utility
You don't

The problem you're having is you're using inline SQL. With this approach you are unable to set values in the database. What I mean by this is if you have a stored proc you can specify the dateformat like this in the procedure:

DATEFORMAT DMY

Also using a procedure is a better approach as what your currently doing because your current code allows for sql injection.

CREATE PROCEDURE p_insert_release_tracker
(
 @Param1 NVARCHAR,
 @Param2 datetime
)
AS
SET DATEFORMAT DMY
...insert statement here

....
GO

Oh another thing. Do not try to convert the date. Just send the datetime value through.

Dim dtDate as datetime()
dtDate = Calendar.Selecteddate

then you create a sqlparameter of type datetime and add it to the parameters collection of the sqlcommand object.

Do you know how to do this?


// Create the connection.
    using (SqlConnection connection = new SqlConnection(@"Data Source=..."))
    {
        // Open the connection.
        connection.Open();

        // Create the command.
        using (SqlCommand command = new SqlCommand("p_insert_release_tracker", connection))
        {
            // Set the command type.
            command.CommandType = System.Data.CommandType.StoredProcedure;

            // Add the parameter.
            SqlParameter parameter = command.Parameters.Add("@dt",
                System.Data.SqlDbType.DateTime);

            // Set the value.
            parameter.Value = DateTime.Now;

            // Make the call.
            command.ExecuteNonQuery();
        }
    }
0
 
LVL 20

Accepted Solution

by:
informaniac earned 250 total points
Comment Utility
Does this help?
strDate = Format(CDate(Calendar1.SelectedDate), "MM-dd-yyyy HH:mm:ss") 

Open in new window

0
 

Author Closing Comment

by:ndr-itsolutions
Comment Utility
As a quick fix to my issue informaniac has provided the solution, but for long term, I want to start using the code provided by esolve. Thanks Guys !
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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