Hi,

i am trying to extract values based on range of dates.
Why i am getting this error

Invalidcastexception was unhandeled
Conversion from string "SELECT * FROM TBLRequest where T" to type 'Long' is not valid.

for below string
FlexString = "SELECT * FROM TBLRequest where TBLRequest.PrDate >='" & Date.Parse(TxtDateFrom.Text) And "TBLRequest.PrDate<='" & Date.Parse(TxtDateTo.Text) & "'"
Thanks

The error is related to your SQL query itself. It seems to be implying that the variable you are storing the string in is set to be a long variable.
What is the actual code around the problem?

** Public FlexString As String**

there is no issue for below code
** FlexString = "Select * from TBL_PR order by PRNO"**

errar occures only for between for e.g
FlexString = "SELECT * FROM TBLRequest where TBLRequest.PrDate between'" & Date.Parse(TxtDateFrom.Text) And Date.Parse(TxtDateTo.Text) & "'"

The error says you are trying to put a string into a long. That should be enough to track down the cause.
Although your SQL query doesn't look right. Don't you want quotes around the 'AND'? Otherwise you're using it as the boolean operator (I think, my VB>net is rusty)

FlexString = "SELECT * FROM TBLRequest where TBLRequest.PrDate between '" & Date.Parse(TxtDateFrom.Text) & "' And '" & Date.Parse(TxtDateTo.Text) & "'"

Although if the fields are dates in the database you could probably use (depending on the database engine):

FlexString = "SELECT * FROM TBLRequest where TBLRequest.PrDate between " & Date.Parse(TxtDateFrom.Text) & " And " & Date.Parse(TxtDateTo.Text)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.