Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

Calendar Control and ASP.net

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
ndr-itsolutions
Asked:
ndr-itsolutions
2 Solutions
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

You can either use Convert.ToDateTime() or DateTime.TryParseExact() try using them.
0
 
esolveCommented:
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
 
ndr-itsolutionsAuthor Commented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
esolveCommented:
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
 
informaniacCommented:
Does this help?
strDate = Format(CDate(Calendar1.SelectedDate), "MM-dd-yyyy HH:mm:ss") 

Open in new window

0
 
ndr-itsolutionsAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now