Solved

Populate VB.net variables from SQL Select Query

Posted on 2013-11-27
3
768 Views
Last Modified: 2013-11-27
I am calling this sql command from vb.net:

cmd.CommandText = "SELECT * FROM SNUGZ_EXT00101_SOP10200_2 WHERE PT_UD_KEY = '" & strKey & "'"
       
The sql query will return 1 row with several columns labeled PT_UD_1, PT_UD_2, etc.

I want to put the values in the columns into variables in my vb.net code.  For example:

strImprint = PT_UD_1
strImprintColor = PT_UD_2


How do I code my vb.net to do this?
0
Comment
Question by:thoecherl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:thoecherl
ID: 39682181
Additional information:

I only want to run the SQL query once.  I don't want to run it each time for each of the 12 variables I need to populate.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39682186
Use the ExecuteReader method of your command object.

e.g.

Dim reader As SqlDataReader = cmd.ExecuteReader()

If reader.Read() Then  ' If there is at least one row
    strImprint = reader("PT_UD_1").ToString()
    strImprintColor = reader("PT_UD_2").ToString()
End If

Open in new window


Also be aware that your query is susceptible to SQL Injection. Building SQL queries using string concatenation is a bad practice. I suggest reading up on parameterized queries.
0
 

Author Closing Comment

by:thoecherl
ID: 39682366
Did the trick.  Thanks.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question