Scott Chang

Hi all,

I just saw a quick flash of result of my Console Application project, when I executed the following code of Module1.vb in my VB 2005 Express program to get the output of DataReader associated with SqlConnection and SqlCommand to a database in my SQL Server Management Studio Express:

//////////////--Module1.vb--///////////////

Imports System.Data

Imports System.Data.SqlClient

Module Module1

Sub Main()

Dim mySqlConnection As SqlConnection

Dim mySqlCommand As SqlCommand

Dim myDataReader As SqlDataReader

mySqlConnection = New SqlConnection("server=.\SQLEXPRESS;Trusted_Connection=yes;database=ssmsExpressDB")

mySqlCommand = New SqlCommand("SELECT AnalyteID, AnalyteName, Result, Unit, LabQualifier FROM LabTests", mySqlConnection)

mySqlConnection.Open()

myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)

Do While (myDataReader.Read())

Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))

Console.Write(myDataReader.GetString(1).ToString + Chr(9))

Console.Write(myDataReader.GetDecimal(2).ToString + Chr(9))

Console.Write(myDataReader.GetString(3).ToString + Chr(9))

Console.Write(myDataReader.GetString(4).ToString + Chr(9))

If (myDataReader.IsDBNull(2)) Then

Console.Write("N/A" + Chr(10))

Else

Console.Write(myDataReader.GetDecimal(2).ToString() + Chr(10))

End If

Loop

myDataReader.Close()

mySqlConnection.Close()

End Sub

End Module

////////////////////////////////////////////////////////////////////////////////////

I can not read the result of my Console Application at all!! How can I make the Console screen stable and lasting Please help and give me some detailed instructions for obtaining the stable Console screen.

Thanks in advance,

Scott Chang




Re: .NET Framework Data Access and Storage Console Application in VB 2005 Express: How to get the Stable Output of DataReader on the screen?

Matt Neerincx

This will work ->

End Sub

Console.WriteLine("Press any key to exit...')

Console.Readline()

End Module






Re: .NET Framework Data Access and Storage Console Application in VB 2005 Express: How to get the Stable Output of DataReader on the screen?

Scott Chang

Hi Matt, Thanks for your valuable response.

I inserted the 2 code statements (you instructed) into my Module1.vb. I executed my revised project and I got the following error message:

Invalid Cast Exception was unhandled.

Specified cast is not valid - This statement was point to the following statement of my code:

Console.Write (myDataReader.GetInt32(0).ToString() + Chr(9)).

I do not know how to fix it!!! Please help and advise.

Thanks,

Scott Chang

P. S. In the Table.dbo.LabTests of my SQL Server Express Database, the first colum "ID" and the second column "AnalyteID" are "Int". Do you think that causes the problem If so, how can I delete the first column "ID" that was in my table by mistake.






Re: .NET Framework Data Access and Storage Console Application in VB 2005 Express: How to get the Stable Output of DataReader on the screen?

Matt Neerincx

Most likely column zero is not int, it is something other than int.

Console.Write (myDataReader.GetInt32(0).ToString() + Chr(9)).

So try this:

Console.Write ("type of my field zero is " + myDataReader.GetValue(0).GetType().Name + Chr(9)).






Re: .NET Framework Data Access and Storage Console Application in VB 2005 Express: How to get the Stable Output of DataReader on the screen?

Scott Chang

Hi Matt, Thanks for your new response.

I tried the new code statement as you instructed. I got the following output on my console:

  type of my field zero is Double   Acetone  67-64-1

and a new error message is "Invalid Cast Exception was unhandled.

                                                   Specified cast is not valid " which is pointing to the following

   statement of my code "Console.Write(myDataReader.GetDecimal(2).ToString + Chr(9))".

What does the "type of my field zero is Double" output tell you What does the new error message tell you   Please help and advise again.

Thanks again,

Scott Chang