Specified cast is not valid in vb.net - vb.net

Getting this error when assigning an integer variable to a value coming back from a SQL stored procedure that may at times contain a NULL.
System.InvalidCastException: Specified cast is not valid.
Code snippet:
Dim iUserId As Nullable(Of Integer)
' Get the UserId associated to the server.
.CommandType = Data.CommandType.StoredProcedure
.CommandText = "SelectUserIdByServerId"
.Parameters.Clear()
.Parameters.AddWithValue("#ServerId", Request("serverid"))
' Returns back 1 column.
iUserId = .ExecuteScalar()
I though that if I have: Dim iUserId As Nullable(Of Integer), that it should not have a problem with it.

ExecuteScalar() returns Object which is an integer or DbNull value.
So you need to write it like this:
Dim tmp As Object = .ExecuteScalar()
iUserId = if(Convert.IsDbNull(tmp), new integer?(), directcast(tmp, integer))

Related

OverFlowException on Executescalar on vb.net

I'm working in asp.net with vb.net and in the backend I'm trying to select something from the database.
But whenever I ask to execute the query it gives an error which says 'OverflowException Ocuured'. The query that is made works perfectly in my SQL Manager tool. Any ideas what can be the problem.
(it gives the problem on the line under 'try' so the 'returnedId = com.ExecuteScalar' line)
Function selectEIDCardnumber(ByVal name As String) As Integer
Dim con As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("testdatabase").ConnectionString)
Dim selecter As String = "SELECT EIDCardNumber FROM [dbo].[_User] where DisplayName = #name"
Dim com As New SqlCommand(selecter, con)
com.Parameters.AddWithValue("#name", name)
con.Open()
Dim returnedId As Integer = 0
Try
returnedId = com.ExecuteScalar
Catch ex As Exception
Response.Redirect("oops.aspx")
End Try
con.Close()
Return returnedId
End Function
The result of com.ExecuteScalar is larger than the max int value.
Int32.TryParse(com.ExecuteScalar, returnedId)
By defining your returnedId and Function As type Integer you are telling VB that the value will be a whole number between -2,147,483,648 and 2,147,483,647. Assuming the data in your database is correct, the solution is to change the returnedId and Function types to Int64, which holds numbers up to 9,223,372,036,854,775,807.
If you're sure that the EIDCardNumber column is an integer type column then try changing Dim returnedId As Integer = 0 into Dim returnedId As Long = 0 and if the problem persists try changing the value of selecter to "SELECT top 1 EIDCardNumber FROM [dbo].[_User] where DisplayName = #name"

Empty row doing insert

I have tried to enter a record with a new table and the below message shows:
"Input string was not in a correct format."
Here is my code:
Dim max_ As String = "select MAX(referance_number) from table"
Dim obj_ As New DATAAccess
Dim ds_ As New Data.DataSet
obj_.populate_dataset_Access_accdb_test(ds_, max_)
Dim ref As Integer = Convert.ToInt32(ds_.Tables(0).Rows(0).Item(0).ToString)
ref = ref + 1
Table columns are int with not null.
If the table is empty then the MAX will return NULL. That means your DataRow will contain DBNull.Value, whose ToString method returns String.Empty, which obviously can't be converted to an Integer. Test for NULL first using the DataRow's own IsNull method.
Of course, you could always do as many thousands of developers the world over do and let the database generated sequential IDs for you.

SQL Data Reader - How to handle Null column values elegantly

I'm using an SQLDataReader to retrieve values from a database which may be null. I've worked out how to handle Null string values but can't get the same trick to work with integers or booleans:
Using cmd As DbCommand = store.GetStoredProcCommand("RetrievePOCO")
store.AddInParameter(cmd, "ID", DbType.Int32, ID)
Using reader As IDataReader = store.ExecuteReader(cmd)
If reader.Read() = True Then
Dim newPOCO As New POCO()
With newPOCO
'If the source column is null TryCast will return nothing without throwing an error
.StatusXML = TryCast(reader.GetString(reader.GetOrdinal("StatusXML")), String)
'How can a null integer or boolean be set elegantly?
.AppType = TryCast(reader.GetInt32(reader.GetOrdinal("AppType")), System.Nullable(Of Integer))
.Archived = TryCast(reader.GetBoolean(reader.GetOrdinal("Archived")), Boolean)
So how can a null integer or boolean be set elegantly? I've seen suggestions in C# but they don't translate correctly to VB giving a 'TryCast operand must be reference type, but integer? is a value type' compiler errors.
I use the following function in this scenario:
Public Shared Function NoNull(ByVal checkValue As Object, ByVal returnIfNull As Object) As Object
If checkValue Is DBNull.Value Then
Return returnIfNull
Else
Return checkValue
End If
End Function
Your code would look something like this:
With newPOCO
.StatusXML = NoNull(reader("StatusXML"), "")
.AppType = NoNull(reader("AppType"), -1)
.Archived = NoNull(reader("Archived"), False)
End With
Note that this function requires you to pass the value which should be used if the value is DbNUll as the second Parameter.
You can take advantage of the IsDBNull method of the SqlDataReader and use the VB.NET ternary operator to assign a default value to your poco object
.StatusXML = If(reader.IsDBNull(reader.GetOrdinal("StatusXML")), _
"",reader.GetString(reader.GetOrdinal("StatusXML")))
It is just one line, not very elegant because you need to call two times the GetOrdinal method.
Public Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T
If Value Is Nothing OrElse IsDBNull(Value) Then
Return DefaultValue
Else
Return Value
End If
End Function

finding min and maxin list using LINQ and DBNulls

i have the following code which will get the max and min values from a list of type double
Dim sprices As List(Of Double) = grp.ProductGroups.AsEnumerable().[Select](Function(sm) sm.Field(Of Double)("sprice")).Distinct().ToList()
Dim min As Integer = sprices.Min()
Dim max As Integer = sprices.Max()
how ever in the first line i get the exception "Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type."
how can i avoid this or not add the DBNulls to the list?
One way is to use a nullable Double, which can be expressed as Double?:
Dim sprices As List(Of Double?) = grp.ProductGroups.AsEnumerable().[Select](Function(sm) sm.Field(Of Double?)("sprice")).Distinct().ToList()

Make An Integer Null

I have an update function that updates an sql server db table through a dataset. One of the fields in the table is an integer and accepts null values. So when I am populating the update function I need a way to enter a null in when the function wants an integer.
I tried to do it this way but _intDLocation = "" throws an exception
Dim _dLocation As String = udDefaultLocationTextEdit.Text
Dim _intDLocation As Integer
If _dLocation <> "" Then
_intDLocation = Integer.Parse(udDefaultLocationTextEdit.Text)
Else
'NEED HELP HERE
_intDLocation = ""
End If
Integers cannot be set to Null. You have to make the integer "nullable" by adding a question mark after the word Integer. Now _intDLocation is no longer a normal integer. It is an instance of Nullable(Of Integer).
Dim _dLocation As String = udDefaultLocationTextEdit.Text
Dim _intDLocation As Integer?
If _dLocation <> "" Then
_intDLocation = Integer.Parse(udDefaultLocationTextEdit.Text)
Else
_intDLocation = Nothing
End If
Later on, if you want to check for null you can use this handy, readable syntax:
If _intDLocation.HasValue Then
DoSomething()
End If
In some cases you will need to access the value as an actual integer, not a nullable integer. For those cases, you simply access
_intDLocation.Value
Read all about Nullable here.
Try this:
Dim _dLocation As String = udDefaultLocationTextEdit.Text
Dim _intDLocation As Nullable(Of Integer)
If Not String.IsNullOrEmpty(_dLocation) Then
_intDLocation = Integer.Parse(_dLocation)
End If
My application uses a lot of labels that start out blank (Text property), but need to be incremented as integers, so I made this handy function:
Public Shared Function Nullinator(ByVal CheckVal As String) As Integer
' Receives a string and returns an integer (zero if Null or Empty or original value)
If String.IsNullOrEmpty(CheckVal) Then
Return 0
Else
Return CheckVal
End If
End Function
This is typical example of how it would be used:
Dim Match_Innings As Integer = Nullinator(Me.TotalInnings.Text)
Enjoy!
_intDLocation = Nothing