Friday, February 24, 2012

Help retreving Value form sql reader

hello,

I have tried myReader.GetSqlString, GetSqlValue, GetSqlInt16, etc...etc...

But I keep getting an error (System.InvalidCastException was caught
Message="Conversion from type 'SqlInt32' to type 'String' is not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value)
at ImportDelimitedFile.SumCurrentAmount() in C:\Documents and Settings\emg3703\My Documents\Visual Studio 2005\EscuelasComunidad\ImportDelimitedFile.aspx.vb:line 556)

Here is my code:

Public Function SumCurrentAmount()As String

Dim sqlconnAs New SqlConnection(ConfigurationManager.ConnectionStrings("GDBRemitanceConnectionString1").ConnectionString)
Dim sqlcmdAs New SqlCommand("SELECT SUM(CONVERT (Int, Field_6)) AS TotalAmount, Record_Type FROM tblTempWorkingStorage_NACHA GROUP BY Record_Type HAVING (Record_Type ='6')", sqlconn)

Try

sqlcmd.Connection.Open()
Dim myReaderAs SqlDataReader
myReader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
If myReader.Read()Then

SumCurrentAmount =CType(myReader.GetSqlValue(0),String)
Return SumCurrentAmount

Else

End If
myReader.Close()

Catch

End Try
sqlcmd.Connection.Close()

End Function

I need to know which "GetSql...(type) should I used to extract field numbre one from myReader.

Thanks a lot,

Try to change this line:

SumCurrentAmount =CType(myReader.GetSqlValue(0),String)

to:

SumCurrentAmount = (myReader.GetDecimal(0)).ToString()

For more information about mapping SqlDbTypes to CLR data types, you can refer to:

http://msdn2.microsoft.com/en-us/library/system.data.sqldbtype.aspx

No comments:

Post a Comment