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 7592437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:54:21+00:00 2026-05-30T20:54:21+00:00

This code works. It’s based on some code I found on the internet. Can

  • 0

This code works. It’s based on some code I found on the internet.

Can you tell me if the coding is the best way to get a scalar value and if there is a better way can you show coding samples?

Dim objParentNameFound As Object

TextBoxParentsName.Text = ""

If TextBoxParentID.Text <> "" Then

    ' Display the parent's name using the parent ID. '
    Dim strSqlStatement As String = "Select FatherName " & _
                                      "From Parents " & _
                                     "Where ID = @SearchValue"

    ' Set up the sql command and lookup the parent. '
    Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)

        With objSqlCommand

            ' Add SqlParameters to the SqlCommand. '
            .Parameters.Clear()
            .Parameters.AddWithValue("@SearchValue", TextBoxParentID.Text)

            ' Open the SqlConnection before executing the query. '
            Try
                ObjConnection.Open()

                Try
                    objParentNameFound = .ExecuteScalar()
                    If objParentNameFound <> Nothing Then

                        ' Display the parent name here. '
                        TextBoxParentsName.Text = objParentNameFound
                    End If

                Catch exSqlErrors As SqlException
                    MessageBox.Show("Sorry, I couldn't execute your query because of this error: " & _
                                    vbCrLf & vbCrLf & exSqlErrors.Message, _
                                    "Error")
                End Try
            Catch exErrors As Exception

                MessageBox.Show("Sorry, there was an error. Details follow: " & _
                                vbCrLf & vbCrLf & exErrors.Message, _
                                "Error")
            Finally
                ObjConnection.Close()
            End Try
        End With 
    End Using 
End If 
  • 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-05-30T20:54:23+00:00Added an answer on May 30, 2026 at 8:54 pm

    The Microsoft Access Application block has some nice examples of how to use ADO.Net. In particular what you might find helpful is how they’ve organized tasks such as ExecuteScalar() into a series of overloaded methods making it easy to invoke the process you need.
    The sample you posted would greatly benefit from separating out the concerns. In other words, take the code you use to build up the connection, command and parameters and make that a separate method or methods. This allows the code to be reused without copy& pasting it throughout your codebase. This allows your calling code to simply pass in the parameter(s) and bind the result to your text box or other controls.

    Edit: Example
    Assuming you use the SqlHelper.vb class you can do something like the following:

    Dim searchValue As Integer = 1
    Dim myConnectionString As String = "MyConnectionString"
    Dim sqlStatement As String = "SELECT FatherName FROM Parents WHERE ID = @SearchValue"
    Dim paramList(0) As SqlParameter
    paramList(0) = New SqlParameter() With {.Value = searchValue, .ParameterName = "@SearchValue"}
    
    TextBoxParentsName.Text = SqlHelper.ExecuteScalar(myConnectionString, CommandType.Text, sqlStatement, paramList).ToString()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works: my $href = shift @_; # get reference to hash my
This code works, and I can see the request go out to my php
This code works fine when running in IE. I get a request asking to
This code works, but I wonder if there is any simpler way: def center(self):
This code works, in some cases. If it returns console.log then the call to
This code works: $row = array(5,6,89,97,101); $found = array(); $post = 89; $count_row =
This code works; only, not with IE7. I read that I could get it
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would

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.