Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 11-21-2012, 11:48 AM   #1
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default VB.net loop coding question

I have the following loop:


For COUNT As Integer = 1 To 5
If (COUNT = 6) Then
Exit For
End If

SQL = "INSERT INTO CALL_AUDIT_SCORES (AUDIT_NO, SECTION, TOPIC_NO, TOPIC_TEXT)" & _
"VALUES ('" & (FILENO.Text + "-" + COLL_NO.Text + "-" + DateTime.Now.ToString("yyyyMMdd")) & "','OPENING','" & COUNT & "','" & OPENING_1_TOPIC.Text & "')"
'Dim Adapter As SqlDataAdapter = New SqlDataAdapter(SQL, cn)
Adapter = New SqlDataAdapter(SQL, cn)
rs = New DataSet
Adapter.Fill(rs, "master")
cn.Close()
Next

But what I want to do is make the “1” in OPENING_1_TOPIC equal to “COUNT”
So as it loops through, I get OPENING_1_TOPIC, OPENING_2_TOPIC, OPENING_3_TOPIC, OPENING_4_TOPIC, and OPENING_5_TOPIC

Ideas?
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 11:59 AM   #2
PhatoseAlpha
Platinum Member
 
Join Date: Apr 2005
Posts: 2,059
Default

The obvious solution would be to change the variable OPENING_1_TOPIC, OPENING_2_TOPIC, etc into an array, OPENING_TOPICS(1), ect.

Why are you doing it with separate variables in the first place?
__________________
"He is behind me. You are in front of me. If you value your life, be someplace else."
PhatoseAlpha is offline   Reply With Quote
Old 11-21-2012, 12:02 PM   #3
WannaFly
Platinum Member
 
WannaFly's Avatar
 
Join Date: Jan 2003
Posts: 2,811
Default

There really isnt an easy way to use a variable as part of a variable name. You could using reflection, but I'd guess it's just not worth it.

Your best bet would be to add the values or controls into an array and then just reference their values using COUNT as the index of the array.

Also, You don't need the if statement, COUNT will never go above 5 in this case.
WannaFly is offline   Reply With Quote
Old 11-21-2012, 12:35 PM   #4
KLin
Lifer
 
KLin's Avatar
 
Join Date: Feb 2000
Location: Phreaznaux
Posts: 28,711
Default

http://www.dotnetperls.com/string-array-vbnet

use a string array, populate string array with opening topic field values

reference string array in sql building code using COUNT to ensure you get the correct value.
__________________
'L_'
KLin is offline   Reply With Quote
Old 11-21-2012, 12:39 PM   #5
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

Hmmm Ok Let me give a little more info here.

I have a VB Form.
In that form there are 5 "Topics" that the user fills in, then hits "submit" and those 5 entires are then INSERTed in SQL. The values of those 5 topics are OPENING_1_TOPIC.Text, OPENING_2_TOPIC.Text etc
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 12:54 PM   #6
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

AH-HA!!!


Dim array() As String = {OPENING_1_TOPIC.Text, OPENING_2_TOPIC.Text}
For Each value As String In array
SQL = "INSERT INTO CALL_AUDIT_SCORES (AUDIT_NO, SECTION, TOPIC_TEXT)" & _
"VALUES ('" & (FILENO.Text + "-" + COLL_NO.Text + "-" + DateTime.Now.ToString("yyyyMMdd")) & "','OPENING','" & value & "')"
'Dim Adapter As SqlDataAdapter = New SqlDataAdapter(SQL, cn)
Adapter = New SqlDataAdapter(SQL, cn)
rs = New DataSet
Adapter.Fill(rs, "master")
cn.Close()
Next
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:00 PM   #7
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

Ok... now as I'm learning here... this is the next step.

For each "OPENING_1_TOPIC.Text" there is a corresponding "OPENING_1_ANSWER" how do I make it so that as it loops through the Array, that it is inserting the correct, corresponding ANSWER? into the table?
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:05 PM   #8
Tweak155
Diamond Member
 
Tweak155's Avatar
 
Join Date: Sep 2003
Location: Michigan
Posts: 8,962
Default

Holy cow this is confusing me. I'm fairly "fluent" in VB and what you're describing to us makes no sense to me.

Rather than JUST show your code, perhaps a brief explanation of your goal would help us help you.

Arrays SOUNDS like the easiest way to do this...

Dim ANSWER_ARRAY() As String = {answer1, answer2, answer3}
Dim QUESTION_ARRAY() As String = {question1, question2, question3}

Now QUESTION_ARRAY(1) will correspond to ANSWER_ARRAY(1)....

It really depends on your end goal here.

EDIT:

And to loop through both:

For COUNT = 1 to MAX_QUESTIONS
someString = "SQL HERE " & QUESTION_ARRAY(COUNT)
someString = "SQL HERE " & ANSWER_ARRAY(COUNT)
Next
__________________
Organo Gold Coffee

Heatware 92-0-0

Last edited by Tweak155; 11-21-2012 at 01:08 PM.
Tweak155 is offline   Reply With Quote
Old 11-21-2012, 01:16 PM   #9
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

Quote:
Originally Posted by Tweak155 View Post
Holy cow this is confusing me. I'm fairly "fluent" in VB and what you're describing to us makes no sense to me.

Rather than JUST show your code, perhaps a brief explanation of your goal would help us help you.

Arrays SOUNDS like the easiest way to do this...

Dim ANSWER_ARRAY() As String = {answer1, answer2, answer3}
Dim QUESTION_ARRAY() As String = {question1, question2, question3}


Now QUESTION_ARRAY(1) will correspond to ANSWER_ARRAY(1)....

It really depends on your end goal here.
LOL yeah. I apologize for the confusion... I admit, I'm just vomiting up thoughts here without much explanation/direction.

My end goal is to have a form. Users will have fields to fill in. There will be 5 rows, and 4 columns in each (so 5 "topics", with 4 fields to fill in for each one) resulting in 20 total values.

I need each one of those rows, and all 4 of its values, to be inserted into a corresponding row within a SQL table.

I think I can do this with a two-dimensional array?


Dim Array(5, 4) As String

Array(0, 0) = "Row 1, Column 1"
Array(0, 1) = "Row 1, Column 2"
...
Array(1, 0) = "Row 2, Column 1"
etc
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused

Last edited by Homerboy; 11-21-2012 at 01:18 PM.
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:19 PM   #10
Tweak155
Diamond Member
 
Tweak155's Avatar
 
Join Date: Sep 2003
Location: Michigan
Posts: 8,962
Default

If it is 5 x 4 then you'd want Dim Array(5, 4).

Then look at it as Array(ROW, COLUMN)
__________________
Organo Gold Coffee

Heatware 92-0-0
Tweak155 is offline   Reply With Quote
Old 11-21-2012, 01:22 PM   #11
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

Quote:
Originally Posted by Tweak155 View Post
If it is 5 x 4 then you'd want Dim Array(5, 4).

Then look at it as Array(ROW, COLUMN)
Yeah I edited that (5,1) was just a typo.
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:25 PM   #12
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

But what don't still get is how those string values of "Array(0, 0)" and "Array(0, 1)" etc are put into the SQL INSERT statement.


SQL = "INSERT INTO CALL_AUDIT_SCORES (AUDIT_NO, SECTION, TOPIC_TEXT, ANSWER)" & _
"VALUES ('" & (FILENO.Text + "-" + COLL_NO.Text + "-" + DateTime.Now.ToString("yyyyMMdd")) & "','OPENING','" & value & ..............."')"

The "value" is just of that first Array(0, 0) correct?
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:34 PM   #13
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

I could just cheat and use a multi-row INSERT statement in the SQL...
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 01:46 PM   #14
beginner99
Platinum Member
 
Join Date: Jun 2009
Posts: 2,180
Default

Our old friend bobby tables would like this very much...

If OP does nto get the Joke:

google for SQL Injection. Threads like this explain soooo much...
beginner99 is offline   Reply With Quote
Old 11-21-2012, 01:58 PM   #15
Homerboy
Lifer
 
Homerboy's Avatar
 
Join Date: Mar 2000
Location: MKE, WI
Posts: 19,613
Default

Quote:
Originally Posted by beginner99 View Post
Our old friend bobby tables would like this very much...

If OP does nto get the Joke:

google for SQL Injection. Threads like this explain soooo much...
I'm familiar with SQL injection.
I'm not overly concerned with that in this case as this is an internal VB.net application used by about 2 people.
__________________
"Ah... In a time of such ugliness, the only true protest is to be beautiful." - Refused
Homerboy is offline   Reply With Quote
Old 11-21-2012, 02:38 PM   #16
KLin
Lifer
 
KLin's Avatar
 
Join Date: Feb 2000
Location: Phreaznaux
Posts: 28,711
Default

http://stackoverflow.com/questions/5...stract-factory

You should implement that for executing commands instead of using that adapter.fill stuff.
__________________
'L_'
KLin is offline   Reply With Quote
Old 11-21-2012, 02:41 PM   #17
nickbits
Diamond Member
 
nickbits's Avatar
 
Join Date: Mar 2008
Location: Southeast Michigan
Posts: 3,733
Default

#1 Use arrays
#2 Use parameters
nickbits is online now   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:14 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.