473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Date Problem in Visual Basic 2005 and Sql Server 2005

Hi!

I am using SQL Server 2005 express edition as backend and Visual Basic 2005 express edition as frontend. This question is related to date comparison.

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN CONVERT(DATETIME, '2006-05-01 00:00:00', 102) AND CONVERT(DATETIME, '2006-05-31 00:00:00', 102)) AND (AgencyCode = 1595)
ORDER BY IssueDate


Above query is generated by query designer and it retrieves 16 records.

However, when I use this code in my vb 2005 application and instead of '2006-05-01 00:00:00', I use dtpicker1.value and instead of '2006-05-31 00:00:00' I use dtpicker2.value, it gives me the following error :

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

What is the problem and how to solve it?

My regional date setting id dd/mm/yyyy.
May 18 '07 #1
5 6955
Killer42
8,435 Expert 8TB
I'd suggest that you try plugging "mm/dd/yyyy" format into the SQL.

See this link for more information.

By the way, thanks for providing so much detail about your circumstances. It makes things a lot easier.
May 18 '07 #2
I'd suggest that you try plugging "mm/dd/yyyy" format into the SQL.

See this link for more information.

By the way, thanks for providing so much detail about your circumstances. It makes things a lot easier.

Thanks! But please tell me how to plug "mm/dd/yyyy" format into the SQL. Because in VB 2005 code, I am not giving any format. I am just using Convert function with dtpicker1.value and dtpicker2.value.

One more observation is that in VB 2005 code, if I use 103 style instead of 102, then it doesn't give the error, but it gives wrong results.(I think style 103 corresponds to my regional date settings).

To be precise, style 102 gives correct results when executed from SQL Server 2005 Management Studion Express, but gives error when executed from VB 2005 Code. On other hand, style 103 gives error when executed from SQL Server 2005 Management Studion Express, and gives wrong results when executed from VB 2005 code.

Please help.
May 21 '07 #3
Killer42
8,435 Expert 8TB
Thanks! But please tell me how to plug "mm/dd/yyyy" format into the SQL. Because in VB 2005 code, I am not giving any format. I am just using Convert function with dtpicker1.value and dtpicker2.value.

One more observation is that in VB 2005 code, if I use 103 style instead of 102, then it doesn't give the error, but it gives wrong results.(I think style 103 corresponds to my regional date settings).

To be precise, style 102 gives correct results when executed from SQL Server 2005 Management Studion Express, but gives error when executed from VB 2005 Code. On other hand, style 103 gives error when executed from SQL Server 2005 Management Studion Express, and gives wrong results when executed from VB 2005 code.

Please help.
Ok, I'm really a bit out of my depth here, as you're getting into the innards of VB.Net and I'm only a VB6 user (I really should update, but can't find the time).

In VB6, I would just use Format() function to format the date/time value. I don't think I've ever even heard of the CONVERT function, so can't advise you on that. But from what you said about 102 & 103, perhaps there's another code which refers to the U.S. format. I hope you referred to the link I gave you, as it provides details about the # delimiters and so forth.

Keep in mind that from here on I'm dealing in pure guesswork, so take it with a grain of salt...

Another possible source of error is that you have included the actual control name in the SQL string. For example, let's assume your SQL string is as follows...

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN CONVERT(DATETIME, DatePicker1.Value, 102) AND CONVERT(DATETIME, DatePicker1.Value, 102)) AND (AgencyCode = 1595)
ORDER BY IssueDate


In that case, I would say it's very probably wrong. Because the SQL interpreter (in the database, not VB) probably has no idea what "DatePicker1.Value" means. It's just a string, and doesn't mean anything as a date. In this case you would need to concatenate the actual value from the DatePicker control into the string, not the property name. This is the age-old problem of differentiating between a variable and its value. You may think you had that one figured out ages ago, but SQL tends to complicate it further.

The code is likely to be something along these lines...

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT IssueDate, Client, AgencyCode " & _
  2.     "FROM SPACEBOOKING " & _
  3.     "WHERE (IssueDate BETWEEN #" & _
  4.     Format(DatePicker1.Value, "mm/dd/yyyy") & _
  5.     "# AND #" & _
  6.     Format(DatePicker2.Value, "mm/dd/yyyy") & _
  7.     "#) AND (AgencyCode = 1595) " & _
  8.     "ORDER BY IssueDate"[/b]
Which should result in a string something like... (without the line-wrapping)

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN #05/01/2006# AND #05/31/2006#)
AND (AgencyCode = 1595)
ORDER BY IssueDate
May 21 '07 #4
Ok, I'm really a bit out of my depth here, as you're getting into the innards of VB.Net and I'm only a VB6 user (I really should update, but can't find the time).

In VB6, I would just use Format() function to format the date/time value. I don't think I've ever even heard of the CONVERT function, so can't advise you on that. But from what you said about 102 & 103, perhaps there's another code which refers to the U.S. format. I hope you referred to the link I gave you, as it provides details about the # delimiters and so forth.

Keep in mind that from here on I'm dealing in pure guesswork, so take it with a grain of salt...

Another possible source of error is that you have included the actual control name in the SQL string. For example, let's assume your SQL string is as follows...

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN CONVERT(DATETIME, DatePicker1.Value, 102) AND CONVERT(DATETIME, DatePicker1.Value, 102)) AND (AgencyCode = 1595)
ORDER BY IssueDate


In that case, I would say it's very probably wrong. Because the SQL interpreter (in the database, not VB) probably has no idea what "DatePicker1.Value" means. It's just a string, and doesn't mean anything as a date. In this case you would need to concatenate the actual value from the DatePicker control into the string, not the property name. This is the age-old problem of differentiating between a variable and its value. You may think you had that one figured out ages ago, but SQL tends to complicate it further.

The code is likely to be something along these lines...

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT IssueDate, Client, AgencyCode " & _
  2.     "FROM SPACEBOOKING " & _
  3.     "WHERE (IssueDate BETWEEN #" & _
  4.     Format(DatePicker1.Value, "mm/dd/yyyy") & _
  5.     "# AND #" & _
  6.     Format(DatePicker2.Value, "mm/dd/yyyy") & _
  7.     "#) AND (AgencyCode = 1595) " & _
  8.     "ORDER BY IssueDate"[/b]
Which should result in a string something like... (without the line-wrapping)

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN #05/01/2006# AND #05/31/2006#)
AND (AgencyCode = 1595)
ORDER BY IssueDate

Thanks! But it is being executed correctly from Sql Server using actual values.
Problem lies in executing it from VB 2005 code(using either actual values or variable names)

Also, I think lines of code you have given are about vb6. But I am talking about VB 2005.
May 22 '07 #5
Killer42
8,435 Expert 8TB
Thanks! But it is being executed correctly from Sql Server using actual values.
Problem lies in executing it from VB 2005 code(using either actual values or variable names)

Also, I think lines of code you have given are about vb6. But I am talking about VB 2005.
Guilty! (In fact, I told you I was writing VB6).

Anyway, let's get back to basics. What happens if you use this string...

SELECT IssueDate, Client, AgencyCode
FROM SPACEBOOKING
WHERE (IssueDate BETWEEN #05/01/2006# AND #05/31/2006#) AND (AgencyCode = 1595)
ORDER BY IssueDate


Also, can you show me exactly how it is being used? There are so many different ways to do database access these days it can be hard to pin things down.

One more note - you may get a quicker answer in either the Access or .Net forum (possibly both).
May 22 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bob Achgill | last post by:
I would like to use the timestamp on files to manage the currency of support files for my VB windows application. In this case I would only put the timestamp of the file in the management database...
6
by: Brian Henry | last post by:
Visual Basic 2005 Express: http://download.microsoft.com/download/f/c/7/fc7debaf-4513-4300-9e6a-8fe27be88cd1/vbsetup.exe Visual C# 2005 Express:...
5
by: Jeff | last post by:
Hi All, I am fairly new to VB .Net 2003. I have been reading a book and doing some of the exercises. I was practicing with labels and it was explaing how to have info show in the label, so I...
0
by: Jerry Spivey | last post by:
Hi, I'm a SQL Server DBA and I'm trying to get a listing of the SQL Servers on the network. I installed Visual Basic 2005 Express edition and used the SQL Server 2005 ServerInfo sample which...
5
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. ...
8
by: WT | last post by:
Is it normal that Visual Studio sets the PreInit handler for a Page from the OnInit code ? No chance to fire it as OnPreInit is run befor OnInit. ??? CS
1
by: FlyingBuckner | last post by:
All right be kind, this is my first question on a forum. I need help on selecting the right software to purchase. I have a database set up using Access, 6 users. It is split into Tables and...
1
by: umeshj99 | last post by:
Hi! I am using SQL Server 2005 express edition as backend and Visual Basic 2005 express edition as frontend. This question is related to date comparison. SELECT IssueDate, Client, ...
1
by: =?Utf-8?B?Rmxhbm1hbg==?= | last post by:
I have a tutorial I was working on with Visual Web Developer 2005 express. Very basic stuff intro to asp.net. All was working fine a few days ago. I took my pc home and installed the visual studion...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.