Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > InvalidCastException


Reply
 
Thread Tools Display Modes
  #1  
Old 03-18-2005, 11:59 AM
DTFan's Avatar
DTFan DTFan is offline
Junior Contributor
 
Join Date: Jul 2003
Location: Atlanta, Georgia
Posts: 230
Question InvalidCastException


Well, I've made (somewhat) the move to .NET (2003) and am slowly starting to work with it (during my free time). Anyway, I've run into something that I haven't been able to find too much information on. I've found several threads addressing the error but nothing really clear (at least to me) in terms of how to address it. I've seen suggestions ranging from making sure I'm importing the proper stuff to re-installing visual studio and the .NET framework (please don't let that be the answer ).

The program I'm working on does use a database (msde) but that isn't what's causing the error so I'm hoping it's OK that I posted here instead of in the Database Forum (if it's not, I'll more than understand if you have to move this thread over to there).

Anyway, here's the code that's causing the problem.


Code:
' use Try/Catch to make sure executenonquery works properly Try sqlCmd.ExecuteNonQuery() Catch sqlInsertE As Exception MessageBox.Show(sqlInsertE, "SQL Insert Error") End Try

First off, I'm doing an SQLClient.SQLCommand to run an Insert into the database. And that is working fine. For "testing" purposes, I made it cause an error so I could see how the Try/Catch would work. But now, when I run the code, I get:


Quote:
An unhandled exception of type 'System.InvalidCastException' occured ...

Additional Information: specified cast is not valid
Am I leaving something out? Or is there a problem with the Try/Catch? Any and all help is greatly appreciated.

Thanks in advance.
__________________

DTFan
Ever-hopeful programmer-in-training
:)

Last edited by DTFan; 03-18-2005 at 12:06 PM.
Reply With Quote
  #2  
Old 03-18-2005, 12:23 PM
candyman candyman is offline
Newcomer
 
Join Date: Feb 2004
Posts: 11
Default

Quote:
Originally Posted by DTFan
Well, I've made (somewhat) the move to .NET (2003) and am slowly starting to work with it (during my free time). Anyway, I've run into something that I haven't been able to find too much information on. I've found several threads addressing the error but nothing really clear (at least to me) in terms of how to address it. I've seen suggestions ranging from making sure I'm importing the proper stuff to re-installing visual studio and the .NET framework (please don't let that be the answer ).

The program I'm working on does use a database (msde) but that isn't what's causing the error so I'm hoping it's OK that I posted here instead of in the Database Forum (if it's not, I'll more than understand if you have to move this thread over to there).

Anyway, here's the code that's causing the problem.


Code:
' use Try/Catch to make sure executenonquery works properly Try sqlCmd.ExecuteNonQuery() Catch sqlInsertE As Exception MessageBox.Show(sqlInsertE, "SQL Insert Error") End Try

First off, I'm doing an SQLClient.SQLCommand to run an Insert into the database. And that is working fine. For "testing" purposes, I made it cause an error so I could see how the Try/Catch would work. But now, when I run the code, I get:



Am I leaving something out? Or is there a problem with the Try/Catch? Any and all help is greatly appreciated.

Thanks in advance.
Try This:

Code:
' use Try/Catch to make sure executenonquery works properly Try sqlCmd.ExecuteNonQuery() Catch sqlInsertE As Exception MessageBox.Show(sqlInsertE.message, "SQL Insert Error") End Try
Reply With Quote
  #3  
Old 03-18-2005, 12:57 PM
DTFan's Avatar
DTFan DTFan is offline
Junior Contributor
 
Join Date: Jul 2003
Location: Atlanta, Georgia
Posts: 230
Default

Quote:
Originally Posted by candyman
Try This:

Code:
' use Try/Catch to make sure executenonquery works properly Try sqlCmd.ExecuteNonQuery() Catch sqlInsertE As Exception MessageBox.Show(sqlInsertE.message, "SQL Insert Error") End Try
candyman - THANK YOU. That was it. I had a feeling it was something small and easy ... a **** .message ....

Thank you for that. Now I can move on to other confusing (but still fun) things to learn in .NET.
__________________

DTFan
Ever-hopeful programmer-in-training
:)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->