473,386 Members | 1,830 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,386 software developers and data experts.

System.InvalidCastException: Specified cast is not valid.

The below statement is causing System.InvalidCastException: Specified
cast is not valid. Can anyone see what is going wrong with this and
help me out.

string statement =
Convert.ToString("Select ConfNum from Call" +
" where AcctNo = '" + r["AcctNo"] + "'" +
" AND BegDate = '" + DateOfCall + "'" +
" AND (BegTime between '" + ((DateTime)r["BegDate"]).TimeOfDay + "'"
+
" AND '" + ((DateTime)r["BegDate"]).AddMinutes(30).TimeOfDay +
"')" +
" OR (BegTime between '" + ((DateTime)r["BegTime"]) + "'" +
" AND '" + ((DateTime)r["BegTime"]).AddMinutes(30) + "'");

Thanks,
IndyChris

Apr 25 '06 #1
2 4580
<ch***********@yahoo.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
The below statement is causing System.InvalidCastException: Specified
cast is not valid. Can anyone see what is going wrong with this and
help me out.

string statement =
Convert.ToString("Select ConfNum from Call" +
" where AcctNo = '" + r["AcctNo"] + "'" +
" AND BegDate = '" + DateOfCall + "'" +
" AND (BegTime between '" + ((DateTime)r["BegDate"]).TimeOfDay + "'"
+
" AND '" + ((DateTime)r["BegDate"]).AddMinutes(30).TimeOfDay +
"')" +
" OR (BegTime between '" + ((DateTime)r["BegTime"]) + "'" +
" AND '" + ((DateTime)r["BegTime"]).AddMinutes(30) + "'");

Thanks,
IndyChris


Something returned from the datareader (presumably r is a datareader) cannot
be converted to DateTime, possibly a value that is DBNull.Value. You should
definately break this up into more lines and remove the Convert.ToString.

Michael
Apr 25 '06 #2
<ch***********@yahoo.com> wrote...
The below statement is causing System.InvalidCastException:
Specified cast is not valid. Can anyone see what is going
wrong with this and help me out.


There is one superfluous "conversion", and two possible sources to the
error.

As you concatenate everything into a string anyway, there's no need to
convert that string into a string... (unnecessary Convert.ToString).

The two possible sources for the error you're getting:

r["AcctNo"]
DateOfCall

What are the types of those?

Do they have an implicit conversion to string, e.g. by a ToString()-method?

I would guess that the first one doesn't...

I would suggest you instead have this format of the statement:

string statement =
"Select ConfNum from Call " +
" where AcctNo = ? " +
" AND BegDate = ? " +
" AND (BegTime between ? AND ? )" +
" OR (BegTime between ? AND ? )";

....and use Parameters to insert the values instead of concatenation.

// Bjorn A
Apr 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: karunakar | last post by:
Hi All Here iam getting this Error "Specified cast is not valid." My Project has Onc solution diffrent class libarary In that solution DALfactory solution iam getting this error I was calling...
2
by: Michael Moreno | last post by:
Hello, I am failing to understand why I get an invalid cast exception with this very simple code. I have derived the EventArgs class as follow: public class StringArgs : System.EventArgs {...
1
by: Arjen | last post by:
Hello, Does somebody haves a solution for this? Thanks! Line 74: if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
1
by: Miguel Angel Dinelli | last post by:
I´m trying to execute a project and I get this error System.InvalidCastException: Specified cast is not valid. I found in microsoft some Q in 327287, but I not sure how make to solution it. ...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
0
by: Miriam | last post by:
Hello, I am trying to edit an xml file. I am using Visual Studio .NET 2003. When I click on the "Data" tab (instead of the XML tab) to enter in data like in excel, the following occurs: If I...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.