-
Mar 10th, 2015, 12:09 PM
#1
Thread Starter
Member
[RESOLVED] Needing your expertise - assign database value to array
Dim conn As New OleDbConnection("connection string")
Dim cmd As OleDbCommand
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
cmd = New OleDbCommand(String.Format("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = {0} and CorrectAnswer = 'Y'", x), conn)
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
Last edited by Serigraphie; May 14th, 2015 at 01:11 PM.
-
Mar 10th, 2015, 01:05 PM
#2
Re: Needing your expertise - assign database value to array
try this:
Code:
Dim conn As New OleDbConnection("connection string")
Dim cmd As OleDbCommand
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
cmd = New OleDbCommand(String.Format("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = {0} and CorrectAnswer = 'Y'", x), conn)
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2015, 08:17 PM
#3
Re: Needing your expertise - assign database value to array
Originally Posted by .paul.
try this:
Code:
Dim conn As New OleDbConnection("connection string")
Dim cmd As OleDbCommand
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
cmd = New OleDbCommand(String.Format("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = {0} and CorrectAnswer = 'Y'", x), conn)
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
I would tend to do that slightly differently:
Code:
Dim conn As New OleDbConnection("connection string")
Dim cmd As New OleDbCommand("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = @QuestionID and CorrectAnswer = 'Y'", conn)
Dim prm = cmd.Parameters.Add("@QuestionID", OleDbType.Integer)
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
prm.Value = x
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
Don't forget that the connection requires opening and closing too.
-
Mar 10th, 2015, 03:18 PM
#4
Thread Starter
Member
Re: Needing your expertise - assign database value to array
Thank you Paul! Will definitely try it on my next shift at work and will let you know and rate you as well if it works!
-
Mar 10th, 2015, 09:32 PM
#5
Re: Needing your expertise - assign database value to array
jmcilhinney's Parameters solution is the best choice for your app...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 11th, 2015, 04:05 AM
#6
Thread Starter
Member
Re: Needing your expertise - assign database value to array
Dim conn As New OleDbConnection("connection string")
Dim cmd As OleDbCommand
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
cmd = New OleDbCommand(String.Format("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = {0} and CorrectAnswer = 'Y'", x), conn)
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
Last edited by Serigraphie; May 14th, 2015 at 01:12 PM.
-
Mar 11th, 2015, 05:16 AM
#7
Thread Starter
Member
Re: Needing your expertise - assign database value to array
Dim conn As New OleDbConnection("connection string")
Dim cmd As OleDbCommand
Dim CorrectAnswer(19) As String
For x As Integer = 1 To 20
cmd = New OleDbCommand(String.Format("Select AnswerValue from tbl_ECM_Questionnaire where QuestionID = {0} and CorrectAnswer = 'Y'", x), conn)
CorrectAnswer(x - 1) = cmd.ExecuteScalar.ToString
Next
Last edited by Serigraphie; May 14th, 2015 at 01:12 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|