D
dgleeson3
Hello All
I have VB code (.Net 2005) reading from an SQL server 2005 database.
Im getting InvalidCastException when doing reader.GetInt32(0)
Im simply reading an int from a simple database. It has two columns,
User and Tel number. User is filled with 1,2,3 and tel number has 3
telephone numbers.
The User data column is specified as (PK, int, not null) in MS SQL
Server management Studio.
My code works fine if I target the Northwind database, CategoryID from
the Categories Table. Its also specified as (PK, int, not null) in MS
SQL Server management Studio.
I thought it might be an access issue as Im working on Vista. SO I
copied everything across to a Windows 2000 machine. I got the same
error.
It must be something to do with my database if my code is OK with
Northwind. I just cant figure it.
Relevant Code below.
----------------------------------------------------------------------------------------------------
Public Sub New()
' Open database.
conDatabase = _
New SqlConnection("Data Source=LAP2;Database='Access1';" &
_
"Integrated Security=SSPI")
Try
' Try to open the database but catch errors if Server not
running or
' database not present.
conDatabase.Open()
Catch ex As Exception
' if we didnt sucessfuly open the database then put up a
msgbox.
If (conDatabase.State = ConnectionState.Closed) Then
' MessageBox.Show("DataBase NOT Open - Status of
DataBase Connection " + "ServerVersion: " +
conDatabase.ServerVersion _
' + ControlChars.Cr + "State: " +
conDatabase.State.ToString())
MessageBox.Show("DataBase Server NOT Running or
Database not present")
End If
End Try
End Sub
--------------------------------------------------------------------------------------------------------
Public Function Load_User_Details(ByVal ListView_X As ListView) As
Boolean
'1. set up query to get required data from database
' Assumes that conDatabase is a valid SqlConnection object.
Dim command As SqlCommand = New SqlCommand( _
"SELECT User, Telephone FROM Tel_Number", conDatabase)
'2. Set up something to hold data from database
Dim reader As SqlDataReader = command.ExecuteReader()
Do While reader.Read()
If reader.IsDBNull(0) Then
Console.Write("<NULL>")
Else
Try
Console.WriteLine("User Number " &
reader.GetInt32(0))
Catch ex As InvalidCastException
Console.Write("Invalid data type.")
End Try
End If
Console.WriteLine()
Loop
reader.Close()
' No problems
Return (True)
End Function
Many thanks for any input.
Regards
Denis
___________________
Denis Gleeson
http://www.CentronSolutions.com
I have VB code (.Net 2005) reading from an SQL server 2005 database.
Im getting InvalidCastException when doing reader.GetInt32(0)
Im simply reading an int from a simple database. It has two columns,
User and Tel number. User is filled with 1,2,3 and tel number has 3
telephone numbers.
The User data column is specified as (PK, int, not null) in MS SQL
Server management Studio.
My code works fine if I target the Northwind database, CategoryID from
the Categories Table. Its also specified as (PK, int, not null) in MS
SQL Server management Studio.
I thought it might be an access issue as Im working on Vista. SO I
copied everything across to a Windows 2000 machine. I got the same
error.
It must be something to do with my database if my code is OK with
Northwind. I just cant figure it.
Relevant Code below.
----------------------------------------------------------------------------------------------------
Public Sub New()
' Open database.
conDatabase = _
New SqlConnection("Data Source=LAP2;Database='Access1';" &
_
"Integrated Security=SSPI")
Try
' Try to open the database but catch errors if Server not
running or
' database not present.
conDatabase.Open()
Catch ex As Exception
' if we didnt sucessfuly open the database then put up a
msgbox.
If (conDatabase.State = ConnectionState.Closed) Then
' MessageBox.Show("DataBase NOT Open - Status of
DataBase Connection " + "ServerVersion: " +
conDatabase.ServerVersion _
' + ControlChars.Cr + "State: " +
conDatabase.State.ToString())
MessageBox.Show("DataBase Server NOT Running or
Database not present")
End If
End Try
End Sub
--------------------------------------------------------------------------------------------------------
Public Function Load_User_Details(ByVal ListView_X As ListView) As
Boolean
'1. set up query to get required data from database
' Assumes that conDatabase is a valid SqlConnection object.
Dim command As SqlCommand = New SqlCommand( _
"SELECT User, Telephone FROM Tel_Number", conDatabase)
'2. Set up something to hold data from database
Dim reader As SqlDataReader = command.ExecuteReader()
Do While reader.Read()
If reader.IsDBNull(0) Then
Console.Write("<NULL>")
Else
Try
Console.WriteLine("User Number " &
reader.GetInt32(0))
Catch ex As InvalidCastException
Console.Write("Invalid data type.")
End Try
End If
Console.WriteLine()
Loop
reader.Close()
' No problems
Return (True)
End Function
Many thanks for any input.
Regards
Denis
___________________
Denis Gleeson
http://www.CentronSolutions.com