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

Invalid cast from SQL to other server

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;" & _
"Data Source=C:\PDB\Development\Database\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.ExecuteScalar
However, changing the connectionstring to

.ConnectionString = "Provider=MS Remote;" & _
"Remote Server=http://scfmzcp1;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=W:\PDB\Development\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 2251
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.com...
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;" & _
"Data Source=C:\PDB\Development\Database\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.ExecuteScalar
However, changing the connectionstring to

.ConnectionString = "Provider=MS Remote;" & _
"Remote Server=http://scfmzcp1;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=W:\PDB\Development\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.com...
Making the following call to a local MSAccess database works fine:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
...
Dim strSystemName As Integer = cmd1.ExecuteScalar


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.ExecuteScalar, 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.ExecuteScalar
Response.Write (o.GetType().ToString())

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
johnwsaundersiii 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.InvalidCastException: Specified cast is not
valid.

Source Error:
Line 75: Dim strSystemName As Integer
Line 76: 'Dim strSystemName As Integer = cmd1.ExecuteScalar
Line 77: Dim o As Object = cmd1.ExecuteScalar
Line 78: Response.Write(o.GetType().ToString())
Line 79: '
-----------------------------------------------------------
Source File: C:\Inetpub\wwwroot\WebPDB2\Global.asax.vb Line: 77

Stack Trace:
[InvalidCastException: Specified cast is not valid.]
System.Data.OleDb.OleDbException..ctor(IErrorInfo errorInfo, Int32
errorCode, Exception inner)
System.Data.OleDb.OleDbConnection.ProcessResults(I nt32 hResult,
OleDbConnection connection, Object src)

System.Data.OleDb.OleDbCommand.ExecuteCommandTextE rrorHandling(Int32
hr)

System.Data.OleDb.OleDbCommand.ExecuteCommandTextF orSingleResult(tagDBPARAMS
dbParams, Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommandText( Object&
executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommand(Comm andBehavior
behavior, Object& executeResult)

System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior
behavior, String method)
System.Data.OleDb.OleDbCommand.ExecuteScalar()
WebPDB2.Global.Session_Start(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\WebPDB2\Global.asax.vb:77
System.Web.SessionState.SessionStateModule.RaiseOn Start(EventArgs
e)
System.Web.SessionState.SessionStateModule.OnStart (EventArgs e)
System.Web.SessionState.SessionStateModule.Complet eAcquireState()
System.Web.SessionState.SessionStateModule.BeginAc quireState(Object
source, EventArgs e, AsyncCallback cb, Object extraData)

System.Web.AsyncEventExecutionStep.System.Web.Http Application+IExecutionStep.Execute()
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step,
Boolean& completedSynchronously) +173


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573;
ASP.NET Version:1.1.4322.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(ByVal sender As Object, ByVal e As EventArgs)
...
Dim strSystemName As Integer = cmd1.ExecuteScalar


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.ExecuteScalar, 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.ExecuteScalar
Response.Write (o.GetType().ToString())

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
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...
3
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...
0
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....
5
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...
2
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...
1
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...
15
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...
21
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...
7
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.