473,881 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invalid cast from SQL to other server

Making the following call to a local MSAccess database works fine:

Sub Session_Start(B yVal sender As Object, ByVal e As EventArgs)
Dim intRows As Integer
Dim strSQL As String
Dim ds As New DataSet
' Create connection
Dim cn As New OleDbConnection
With cn
.connectionstri ng =
"provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\PDB\D evelopment\Data base\PDB.mdb"
End With
' Open connection
cn.Open()
' -----------------------------------------------------------
' Get System Name
' -----------------------------------------------------------
' Create command
Dim cmd1 As New OleDbCommand
With cmd1
.Connection = cn
.CommandText = "SELECT System FROM Reference"
End With
' Execute the SQL
Dim strSystemName As Integer = cmd1.ExecuteSca lar
However, changing the connectionstrin g to

.ConnectionStri ng = "Provider=M S Remote;" & _
"Remote Server=http://scfmzcp1;" & _
"Remote Provider=Micros oft.Jet.OLEDB.4 .0;" & _
"Data Source=W:\PDB\D evelopment\PDB. mdb;" & _
"Connection Timeout=30"

causes a "Specified cast is not valid" message on the ExecuteScalar
line.

Thanks for your help

John
Nov 18 '05 #1
3 2296
To create a generic connection that can connection to virtually any database
(as it seems you are trying to do) you should utilize the generic
IDBConnection interface.
Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"John Howard" <me@somewhere > wrote in message
news:oq******** *************** *********@4ax.c om...
Making the following call to a local MSAccess database works fine:

Sub Session_Start(B yVal sender As Object, ByVal e As EventArgs)
Dim intRows As Integer
Dim strSQL As String
Dim ds As New DataSet
' Create connection
Dim cn As New OleDbConnection
With cn
.connectionstri ng =
"provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=C:\PDB\D evelopment\Data base\PDB.mdb"
End With
' Open connection
cn.Open()
' -----------------------------------------------------------
' Get System Name
' -----------------------------------------------------------
' Create command
Dim cmd1 As New OleDbCommand
With cmd1
.Connection = cn
.CommandText = "SELECT System FROM Reference"
End With
' Execute the SQL
Dim strSystemName As Integer = cmd1.ExecuteSca lar
However, changing the connectionstrin g to

.ConnectionStri ng = "Provider=M S Remote;" & _
"Remote Server=http://scfmzcp1;" & _
"Remote Provider=Micros oft.Jet.OLEDB.4 .0;" & _
"Data Source=W:\PDB\D evelopment\PDB. mdb;" & _
"Connection Timeout=30"

causes a "Specified cast is not valid" message on the ExecuteScalar
line.

Thanks for your help

John

Nov 18 '05 #2
"John Howard" <me@somewhere > wrote in message
news:oq******** *************** *********@4ax.c om...
Making the following call to a local MSAccess database works fine:

Sub Session_Start(B yVal sender As Object, ByVal e As EventArgs)
...
Dim strSystemName As Integer = cmd1.ExecuteSca lar


If you get a "specified cast is invalid" exception, then you should ask
yourself which cast it's referring to.

Now, there's no cast visible on that line because VB.NET is allowing you to
get away without one (try turning Options Strict On). There's an implicit
cast to Integer:

Dim strSystemName as Integer = CType(cmd1.Exec uteScalar, Integer)

So, if the "specified cast is invalid", then it suggests that the
ExecuteScalar call is returning something which cannot be cast to an Integer
(in other words, it's not returning an integer). You might try the
following:

Dim o As Object = cmd1.ExecuteSca lar
Response.Write (o.GetType().To String())

Another clue is the fact that you named the variable "strSystemName" , but
you say it is of type "Integer". Maybe the ExecuteScalar is returning a
string?
--
John Saunders
johnwsaundersii i at hotmail

Nov 18 '05 #3
John:

This is the response:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidC astException: Specified cast is not
valid.

Source Error:
Line 75: Dim strSystemName As Integer
Line 76: 'Dim strSystemName As Integer = cmd1.ExecuteSca lar
Line 77: Dim o As Object = cmd1.ExecuteSca lar
Line 78: Response.Write( o.GetType().ToS tring())
Line 79: '
-----------------------------------------------------------
Source File: C:\Inetpub\wwwr oot\WebPDB2\Glo bal.asax.vb Line: 77

Stack Trace:
[InvalidCastExce ption: Specified cast is not valid.]
System.Data.Ole Db.OleDbExcepti on..ctor(IError Info errorInfo, Int32
errorCode, Exception inner)
System.Data.Ole Db.OleDbConnect ion.ProcessResu lts(Int32 hResult,
OleDbConnection connection, Object src)

System.Data.Ole Db.OleDbCommand .ExecuteCommand TextErrorHandli ng(Int32
hr)

System.Data.Ole Db.OleDbCommand .ExecuteCommand TextForSingleRe sult(tagDBPARAM S
dbParams, Object& executeResult)
System.Data.Ole Db.OleDbCommand .ExecuteCommand Text(Object&
executeResult)
System.Data.Ole Db.OleDbCommand .ExecuteCommand (CommandBehavio r
behavior, Object& executeResult)

System.Data.Ole Db.OleDbCommand .ExecuteReaderI nternal(Command Behavior
behavior, String method)
System.Data.Ole Db.OleDbCommand .ExecuteScalar( )
WebPDB2.Global. Session_Start(O bject sender, EventArgs e) in
C:\Inetpub\wwwr oot\WebPDB2\Glo bal.asax.vb:77
System.Web.Sess ionState.Sessio nStateModule.Ra iseOnStart(Even tArgs
e)
System.Web.Sess ionState.Sessio nStateModule.On Start(EventArgs e)
System.Web.Sess ionState.Sessio nStateModule.Co mpleteAcquireSt ate()
System.Web.Sess ionState.Sessio nStateModule.Be ginAcquireState (Object
source, EventArgs e, AsyncCallback cb, Object extraData)

System.Web.Asyn cEventExecution Step.System.Web .HttpApplicatio n+IExecutionSte p.Execute()
System.Web.Http Application.Exe cuteStep(IExecu tionStep step,
Boolean& completedSynchr onously) +173


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.432 2.573;
ASP.NET Version:1.1.432 2.573

On Tue, 8 Jun 2004 17:08:09 -0400, "John Saunders"
<jo************ **@notcoldmail. com> wrote:
"John Howard" <me@somewhere > wrote in message
news:oq******* *************** **********@4ax. com...
Making the following call to a local MSAccess database works fine:

Sub Session_Start(B yVal sender As Object, ByVal e As EventArgs)
...
Dim strSystemName As Integer = cmd1.ExecuteSca lar


If you get a "specified cast is invalid" exception, then you should ask
yourself which cast it's referring to.

Now, there's no cast visible on that line because VB.NET is allowing you to
get away without one (try turning Options Strict On). There's an implicit
cast to Integer:

Dim strSystemName as Integer = CType(cmd1.Exec uteScalar, Integer)

So, if the "specified cast is invalid", then it suggests that the
ExecuteScala r call is returning something which cannot be cast to an Integer
(in other words, it's not returning an integer). You might try the
following:

Dim o As Object = cmd1.ExecuteSca lar
Response.Wri te (o.GetType().To String())

Another clue is the fact that you named the variable "strSystemName" , but
you say it is of type "Integer". Maybe the ExecuteScalar is returning a
string?


Nov 18 '05 #4

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

Similar topics

3
31035
by: Krung Saengpole | last post by:
Hi, I used SQL Server 2000 Personal Edition. I created a stored procedure having input parameters as smallint,tinyint,char,varchar and smalldatetime. When I executed it by Query Analyzer, it's ok. But when I executed it by ASP code that used ADODB.command, it showed error: Invalid character value for cast specification . Though SQL Server debugger, it showed: Invalid character value for cast
3
13897
by: Giulio Santorini | last post by:
Hi, I've got a little problem with my C# WinForms test application. I would like to have a ComboBox showing a a visual value and some hidden values. So I build a class able to store thoose values. This class called ListItem have got two private variable, one is a string containing the visual value, the other one is a UDT variable. It is a struct called
0
1690
by: Alan Z. Scharf | last post by:
Win Server 2003 VS.Net 2003 --------------- 1. I'm having the same problem below on all six of my pages with a datagrid item. 2. These pages all worked fine for months until problem started. 3. Same problem on two different computers running Win 2003.
5
3447
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
2
2245
by: adams114 | last post by:
I am having a strange problem with invalid type casts. I am trying to update a MS SQL Database with a stored procedure. When I setup the parameters collection for the command object I get a invalid cast exception error: Compiler Error Message: BC30311: Value of type 'Date' cannot be converted to 'Integer'. The real problem here is that the type in the database and the stored prcedure aren't integers at all rather they are datetime...
1
4922
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm specifiying my code: The Code used in Visual Studio: Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As String, ByVal iDate As Date) As String 'Dim Ret As Integer
15
2270
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed spectacularly. I was so embarrassed and felt like killing the person that made it fail. However, when it goes live, IT MUST NOT FAIL. The system has a backoffice system that takes an excel spreadsheet from the
21
1935
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring = "provider=Microsoft.Jet.OLEDB.4.0;" & _
7
3634
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i get to item 250 i get a "system.invalid cast exception" and "specified cast is not valid". Once i get this error and try to go to the next record.. every record after gets the same error... It's like the com connection to outlook has been lost....
0
9776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10716
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9552
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7109
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.