473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Trying to use an array with DirectCast

The problem.

I have created an exam using the control wizard. Most answers are text answers I created the SQL db. Now when I try to read the data it always gives me cast error. I decided to put in if elseif statements to make sure that worked and everything is fine but I am sure there must be another and BETTER way.

I am new at this but need to figure a way to get it done. The only solution I had was the one attached but there has to be a way to use an array or create a new web ui control from a string.

When I try to do this: QLabel = {"QLabel" & (i) & ".text") and use I get an error tried using DirectCast on QLabel and I get a cast error any suggestions?


The code is below.

Code: ( text )
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArg  s) Handles Wizard1.FinishButtonClick
  2.  
  3.         'Get Membership GUID 
  4.         'Label2.Text = "Geeting User"
  5.         Dim instance As MembershipUser = Membership.GetUser()
  6.         Dim value As Object
  7.         If instance Is Nothing Then
  8.             Label2.Text = "NO USER IS LOGGED IN"
  9.             Exit Sub
  10.         End If
  11.         value = instance.ProviderUserKey.ToString
  12.         Dim NameId = value.ToString
  13.         'SQL Input  
  14.  
  15.         Dim QGroup
  16.         Dim QAnswer As Object
  17.         Dim QLabel
  18.  
  19.  
  20.  
  21.  
  22.         QGroup = Profile.Opportunity
  23.         Dim i As Integer
  24.         i = 0
  25.         Do Until i > 26
  26.             i = i + 1
  27.  
  28.             ' QAnswer = ("QAnswer" & (i) & ".Text")
  29.             ' QLabel = ("QLabel" & (i) & ".Text")
  30.             If i = 1 Then
  31.                 QLabel = QLabel1.Text
  32.                 QAnswer = QAnswer1.Text
  33.             ElseIf i = 2 Then
  34.                 QLabel = QLabel2.Text
  35.                 QAnswer = QAnswer2.Text
  36.             ElseIf i = 3 Then
  37.                 QLabel = QLabel3.Text
  38.                 QAnswer = QAnswer3.Text
  39.             ElseIf i = 4 Then
  40.                 QLabel = QLabel4.Text
  41.                 QAnswer = QAnswer4.Text
  42.             ElseIf i = 5 Then
  43.                 QLabel = QLabel5.Text
  44.                 QAnswer = QAnswer5.Text
  45.             ElseIf i = 6 Then
  46.                 QLabel = QLabel6.Text
  47.                 QAnswer = QAnswer6.Text
  48.             ElseIf i = 7 Then
  49.                 QLabel = QLabel7.Text
  50.                 QAnswer = QAnswer7.Text
  51.             ElseIf i = 8 Then
  52.                 QLabel = QLabel8.Text
  53.                 QAnswer = QAnswer8.Text
  54.             ElseIf i = 9 Then
  55.                 QLabel = QLabel9.Text
  56.                 QAnswer = QAnswer9.Text
  57.             ElseIf i = 10 Then
  58.                 QLabel = QLabel10.Text
  59.                 QAnswer = QAnswer10.Text
  60.             ElseIf i = 11 Then
  61.                 QLabel = QLabel11.Text
  62.                 QAnswer = QAnswer11.Text
  63.             ElseIf i = 12 Then
  64.                 QLabel = QLabel12.Text
  65.                 QAnswer = QAnswer12.Text
  66.  
  67.          ' ..... Up to i =27
  68.  
  69. End If
  70.  
  71.             ' If QLabel.GetType().ToString() = "System.Web.UI.WebControls.Label" Then
  72.             ' Dim txtLabel As Label = DirectCast(QLabel, Label)
  73.             ' txtLabel.Text = Trim(txtLabel.Text)
  74.             ' End If
  75.  
  76.             Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
  77.             Dim myConnection As New SqlConnection(connStr)
  78.             Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
  79.  
  80.             Dim myCommand As New SqlCommand(SQL, myConnection)
  81.  
  82.             myCommand.Parameters.AddWithValue("@UserID", NameId)
  83.             myCommand.Parameters.AddWithValue("@QGroup", QGroup)
  84.             myCommand.Parameters.AddWithValue("@QLabel", QLabel)
  85.             myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
  86.  
  87.             'For Testing Only
  88.             'Label2.Text = NameId
  89.  
  90.             Try
  91.                 myConnection.Open()
  92.                 myCommand.ExecuteNonQuery()
  93.                 myConnection.Close()
  94.             Catch ex As Exception
  95.                 'For Testing Only
  96.                 Label1.Visible = True
  97.                 Label1.Text = ex.Message.ToString
  98.             End Try
  99.         Loop
  100.  
May 23 '07 #1
8 2694
kenobewan
4,871 Expert 4TB
What is the error you receive?
May 24 '07 #2
What is the error you receive?
Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Label'.
May 24 '07 #3
Frinavale
9,735 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArg  s) Handles Wizard1.FinishButtonClick
  2.  
  3.         'Get Membership GUID 
  4.         'Label2.Text = "Geeting User"
  5.         Dim instance As MembershipUser = Membership.GetUser()
  6.         Dim value As Object
  7.         If instance Is Nothing Then
  8.             Label2.Text = "NO USER IS LOGGED IN"
  9.             Exit Sub
  10.         End If
  11.         value = instance.ProviderUserKey.ToString
  12.         Dim NameId = value.ToString
  13.         'SQL Input  
  14.  
  15.         Dim QGroup
  16.         Dim QAnswer As Object
  17.         Dim QLabel
  18.  
  19.         QGroup = Profile.Opportunity
  20.         Dim i As Integer
  21.         i = 0
  22.         Do Until i > 26
  23.             i = i + 1
  24.  
  25.             ' QAnswer = ("QAnswer" & (i) & ".Text")
  26.             ' QLabel = ("QLabel" & (i) & ".Text")
  27.             If i = 1 Then
  28.                 QLabel = QLabel1.Text
  29.                 QAnswer = QAnswer1.Text
  30.             ElseIf i = 2 Then
  31.                 QLabel = QLabel2.Text
  32.                 QAnswer = QAnswer2.Text
  33.             ElseIf i = 3 Then
  34.                 QLabel = QLabel3.Text
  35.                 QAnswer = QAnswer3.Text
  36.                    ' ..... Up to i =27
  37.  
  38. End If
  39.  
  40.             ' If QLabel.GetType().ToString() = "System.Web.UI.WebControls.Label" Then
  41.             ' Dim txtLabel As Label = DirectCast(QLabel, Label)
  42.             ' txtLabel.Text = Trim(txtLabel.Text)
  43.             ' End If
  44.  
  45.             Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
  46.             Dim myConnection As New SqlConnection(connStr)
  47.             Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
  48.  
  49.             Dim myCommand As New SqlCommand(SQL, myConnection)
  50.  
  51.             myCommand.Parameters.AddWithValue("@UserID", NameId)
  52.             myCommand.Parameters.AddWithValue("@QGroup", QGroup)
  53.             myCommand.Parameters.AddWithValue("@QLabel", QLabel)
  54.             myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
  55.  
  56.             'For Testing Only
  57.             'Label2.Text = NameId
  58.  
  59.             Try
  60.                 myConnection.Open()
  61.                 myCommand.ExecuteNonQuery()
  62.                 myConnection.Close()
  63.             Catch ex As Exception
  64.                 'For Testing Only
  65.                 Label1.Visible = True
  66.                 Label1.Text = ex.Message.ToString
  67.             End Try
  68.         Loop
  69.  
First of all you're declaring QLabel without stating what it is. (I just find this kind of strange)

Then you're Setting QLabel to be the text stored in a Label.
Maybe try declaring what type of object QLabel is before setting it to something

Could you please post a copy of the error.
May 24 '07 #4
[
First of all you're declaring QLabel without stating what it is. (I just find this kind of strange)

Then you're Setting QLabel to be the text stored in a Label.
Maybe try declaring what type of object QLabel is before setting it to something

Could you please post a copy of the error.
I have declared it several ways all with various results. Nothing seems to work.

RTW
May 25 '07 #5
Frinavale
9,735 Expert Mod 8TB
I have declared it several ways all with various results. Nothing seems to work.

RTW
What is the error you're getting?
May 25 '07 #6
I have declared it several ways all with various results. Nothing seems to work.

RTW
I have made the test using a wizard control with steps. VS 2005 .net 2.0 VB

I did not want to go back and forth to the server so I wrote the update statement at the end of the questions.

Some question sets are as long as 100 questions and we have over 35 question sets.

Looking at the code below that works it seems to me there must be an easier way of accompolishing this. I have tried everything that I have read or found for the past 5 times and nothing seems to work.

If anyone can help please do so.

Here is what I have that works.

Expand|Select|Wrap|Line Numbers
  1.  'SQL Input 
  2.  
  3. Dim QGroup
  4. Dim QAnswer As String
  5. Dim QLabel As String
  6.  
  7. 'Inserted dummy string text
  8. QLabel = "N/A"
  9. QAnswer = "N/A"
  10.  
  11. QGroup = Profile.Opportunity
  12. Dim i As Integer
  13. i = 0
  14. Do Until i > 11
  15. i = i + 1
  16.  
  17.  
  18. If i = 1 Then
  19. QLabel = QLabel1.Text
  20. QAnswer = QAnswer1.Text
  21. ElseIf i = 2 Then
  22. QLabel = QLabel2.Text
  23. QAnswer = QAnswer2.Text
  24. ElseIf i = 3 Then
  25. QLabel = QLabel3.Text
  26. QAnswer = QAnswer3.Text
  27. ElseIf i = 4 Then
  28. QLabel = QLabel4.Text
  29. QAnswer = QAnswer4.Text
  30. ElseIf i = 5 Then
  31. QLabel = QLabel5.Text
  32. QAnswer = QAnswer5.Text
  33. ElseIf i = 6 Then
  34. QLabel = QLabel6.Text
  35. QAnswer = QAnswer6.Text
  36. ElseIf i = 7 Then
  37. QLabel = QLabel7.Text
  38. QAnswer = QAnswer7.Text
  39. ElseIf i = 8 Then
  40. QLabel = QLabel8.Text
  41. QAnswer = QAnswer8.Text
  42. ElseIf i = 9 Then
  43. QLabel = QLabel9.Text
  44. QAnswer = QAnswer9.Text
  45. ElseIf i = 10 Then
  46. QLabel = QLabel10.Text
  47. QAnswer = QAnswer10.Text
  48. ElseIf i = 11 Then
  49. QLabel = QLabel11.Text
  50. QAnswer = QAnswer11.Text
  51. ElseIf i = 12 Then
  52. QLabel = QLabel12.Text
  53. QAnswer = QAnswer12.Text
  54. End If
  55.  
  56. Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
  57. Dim myConnection As New SqlConnection(connStr)
  58. Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
  59.  
  60. Dim myCommand As New SqlCommand(SQL, myConnection)
  61.  
  62. myCommand.Parameters.AddWithValue("@UserID", NameId)
  63. myCommand.Parameters.AddWithValue("@QGroup", QGroup)
  64. myCommand.Parameters.AddWithValue("@QLabel", QLabel)
  65. myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
  66.  
  67. 'For Testing Only
  68. 'Label2.Text = NameId
  69.  
  70. Try
  71. myConnection.Open()
  72. myCommand.ExecuteNonQuery()
  73. myConnection.Close()
  74. Catch ex As Exception
  75. 'For Testing Only
  76. Label1.Visible = True
  77. Label1.Text = ex.Message.ToString & (i)
  78. End Try
  79. Loop
  80.  
May 25 '07 #7
Frinavale
9,735 Expert Mod 8TB
I have made the test using a wizard control with steps. VS 2005 .net 2.0 VB

I did not want to go back and forth to the server so I wrote the update statement at the end of the questions.

Some question sets are as long as 100 questions and we have over 35 question sets.

Looking at the code below that works it seems to me there must be an easier way of accompolishing this. I have tried everything that I have read or found for the past 5 times and nothing seems to work.

If anyone can help please do so.

Here is what I have that works.
So you fixed the problem you were having before?
May 27 '07 #8
So you fixed the problem you were having before?
I don't think this is the correct way can I not just create some sort of loop incrementing the contorl each time? This way I don't have to write all these lines of code. Some exams are 100's of questions.

RTW
May 30 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
2
by: Able | last post by:
Dear friends Help me with an array task, I'm too old for this. I have an two dimensional array myArr(x,y), imagine y is rows. I want to preserve the existing array and add a new row to it....
4
by: Petro | last post by:
Good morning The following code generates the exception: "Option Strict On disallows implicit conversion from System.Array to 1-dimensional array of 'String'. I've been unable to find the...
9
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
19
by: Maarten | last post by:
i'm making a form where you can add textboxes with a button. the max number of textboxes to add is 100 but i also must create an array of al thes textboxes, is there a way of creating an array...
5
by: mark | last post by:
I know I am being incredibly dunderheaded about this consider the following: STRICT ON Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}} Private Sub T1(ByVal a As Array) Dim i, j As Integer For i =...
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
9
by: =?Utf-8?B?U0FM?= | last post by:
Is it possible to Cast an Object to a String Array the way I'm doing it in the code below? I'm trying to reuse code I have without creating new code if I can. Dim asNames() As String Dim...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.