Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:22:06+00:00 2026-06-10T11:22:06+00:00

I have a simple function which returns a single column’s data from a database

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:22:07+00:00Added an answer on June 10, 2026 at 11:22 am

    Your function should probably return a nullable integer, returning Nothing when your SQL server returns nothing or DBNull (if you don’t want to make a difference between No rows returned and DBNull).

    Function GetId(ByVal name As String) As Integer?
    
        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
            Dim result = cmd.ExecuteScalar
            Return If(DBNull.Value.Equals(result), 
                      Nothing, 
                      DirectCast(result, Integer?))
        End Using
    
    End Function
    

    Then, you would call your method like this:

    Dim userId = GetBillingGroupToInvoice(name)
    If userId.HasValue Then
        'Do something with userId.Value'
    End If
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using xlwDotNet, I have created a simple function which returns one number. Sometimes, something
I have written a simple jQuery.ajax function which loads a user control from the
I have a simple function which takes an array of characters as an argument,
I have a simple C function which I declare as: int strLen(const char* str_)
I have a simple function, which I shall call myFunction . It takes two
I have a simple asp page with a script including a function myFunction which
I have a relatively simple class which deletes a post: function delete_post($postid, $reason){ //Stuff
I have a simple function that looks at an incoming mySQL data type and
I have a C++ function which returns a multi-line std::string . In the test-case
I have a bash script which takes a single argument and returns around 8-10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.