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

  • Home
  • SEARCH
  • 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 8084345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:46:02+00:00 2026-06-05T17:46:02+00:00

Hoping for a quick answer here. OK, since I do a lot of single

  • 0

Hoping for a quick answer here.

OK, since I do a lot of single value lookups from the DB, I created a function to handle the lookup for me. It’s designed to get any type of data type (string, integer, date, …).

It works when I want to retrieve a number, but gives me an error when I want a string (InvalidCastException trying to convert a string to an integer on the line: GetValue = DR(0)).
I can’t do a ctype or directcast because the datatype is unknown and varies.
Haven’t tested any other data types yet.

Code is below. I’d like to find out how to make this function work, or pointed to another function that will serve the same purpose.

Public Shared Function GetValue(Optional ByVal SQL As String = "", Optional ByVal FieldName As String = "", Optional ByVal TableName As String = "", Optional ByVal WhereClause As String = "") As VariantType?
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim strSQL As New SQLStringBuilder
    Dim DR As SqlDataReader

    myConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnAFRAMSSQL").ConnectionString)
    strSQL.Add(SQL)
    If FieldName > "" Then
        strSQL.Add("SELECT " & FieldName)
    End If
    If TableName > "" Then
        strSQL.Add("FROM " & TableName)
    End If
    If WhereClause > "" Then
        strSQL.Add("WHERE " & WhereClause)
    End If
    myConnection.Open()
    myCommand = New SqlCommand(strSQL.ToString, myConnection)
    DR = myCommand.ExecuteReader()
    If DR.HasRows Then
        DR.Read()
        GetValue = DR(0)
    Else
        GetValue = Nothing
    End If
End Function

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-05T17:46:03+00:00Added an answer on June 5, 2026 at 5:46 pm

    You may specify the System.Object (System.Object is the ultimate base class of all types) return type of your method.

    Public Shared Function GetValue(Optional ByVal SQL As String = "", Optional ByVal FieldName As String = "", Optional ByVal TableName As String = "", Optional ByVal WhereClause As String = "") As Object
        Dim myConnection As SqlConnection
        ....
       Dim obj as Object=Nothing
       If DR.Read()
            obj=DR(0)
       End If
       DR.Close()
       myConnection.Close()
       return obj
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was hoping someone could answer my quick question as I am going nuts!
I am hoping that this will have a pretty quick and simple answer. I
I am hoping to get a quick answer to a simple problem. I am
Hoping for a quick answer (which SO seems to be pretty good for)... I
Hoping for a quick peer review here. An associate and I build out a
I am hoping someone can help me out with a quick question I have
Hoping that anybody here knows about a good one: I'm looking for a (free
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin
Hoping someone can shed some light on this: Do lookup tables need their own
I am in way over my head, and am hoping anyone here can help.

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.