I have a simple function which returns a single column’s data from a database based on an inline SQL statement like this (simplified version):
Function GetId(ByVal name As String) As Object
Dim sql As String = "Select Id From table where username = '" & name & "'"
Using cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
GetId = cmd.ExecuteScalar
End Using
End Function
If the SQL returns a row it can be NULL or integer, or alternatively SQL does not return anything.
In another part of the code I have:
Dim userId As Object = New Object
userId = GetBillingGroupToInvoice(name)
Is it OK to use Object as a return type here. If not what should I specify as a return type of this function? This is in VB but answers in C# is also OK.
Thanks
Your function should probably return a nullable integer, returning
Nothingwhen your SQL server returns nothing orDBNull(if you don’t want to make a difference between No rows returned and DBNull).Then, you would call your method like this: