Solved

Populate VB.net variables from SQL Select Query

Posted on 2013-11-27
3
742 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
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

829 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