Results 1 to 7 of 7

Thread: VB.Net SqlDbType.DateTime Question

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Unhappy VB.Net SqlDbType.DateTime Question

    I have a SQL Server stored procedure which accepts a datetime parameter to filter it's resulting data by. This has the following statement declared at the start:
    Code:
    SET DATEFORMAT dmy
    Through the SQL Query Analyzer, I can run this stored procedure, using either of the following, and receive results:
    Code:
    SET @FromDate = '30-12-2004 00:01:01:001'
    SET @FromDate = '30/12/2004'
    Placing exactly the same strings in the following code within vb.net, produces a "String was not recognized as a valid DateTime." exception message! Can someone please explain how I can pass a date value into the SQLClient.SQLCommand object successfully???
    VB Code:
    1. cmdSQL = New SqlClient.SqlCommand
    2.  
    3. cmdSQL.Parameters.Add("@FromDate", SqlDbType.DateTime).Value= ?????
    4. cmdSQL.Parameters.Item(0).Direction = ParameterDirection.Input

    Thanks!!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: VB.Net SqlDbType.DateTime Question

    Try to convert the string to a datetime with System.Convert.ToDateTime(strValue) before passing it as a parameter to the SQLCommand.

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: VB.Net SqlDbType.DateTime Question

    Hi kaffenils - thanks for the reply!

    I've tried this one and it resulted in an "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value." exception - however I format the date I pass to the parameter.

    Thanks again!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB.Net SqlDbType.DateTime Question

    I suspect the problem lies in the fact that VB doesn't know about the DATEFORMAT setting (which doesn't get set until the SP runs). With that said, there's two things that come to mind, both of which are a little shady.

    1) Just set the date parameter formated in the "default" mdy. Not sure how it will interpret it. I'd suggest trying it in a "safe" area.

    2) We used to this with SQLServer7 and Crystal. Change your parameter type to a string, then pass in the parameter in as a string, formatted the way you want. Inside the SP, after SETting DATEFORMAT, DECLARE a datetime variable, and CONVERT the string parameter into the date variable.

    Like I said, both are a little hoaky, but one of them should work.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: VB.Net SqlDbType.DateTime Question

    Hey thanks for those!

    I was thinking of number 2 myself, passing this into the stored procedure as a string, then performing a cast etc on it, but surely there must be a professional / proper way to do this?

    It just seemed odd that have MS put in a datetime option on the SQLDBType enumeration if this doesn’t work.

    Thanks for the suggestions

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB.Net SqlDbType.DateTime Question

    But the problem is that the client code doesn't know about a server side setting -- which doesn't even get set until the SP actualy runs. That's why it errors out on the VB end.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: VB.Net SqlDbType.DateTime Question

    Oh sorry - I see what you mean now, because I have the dateformat setting. Sorry I'm with it now! Ok thanks for that!!!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width