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 ) -
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArg s) Handles Wizard1.FinishButtonClick
-
-
'Get Membership GUID
-
'Label2.Text = "Geeting User"
-
Dim instance As MembershipUser = Membership.GetUser()
-
Dim value As Object
-
If instance Is Nothing Then
-
Label2.Text = "NO USER IS LOGGED IN"
-
Exit Sub
-
End If
-
value = instance.ProviderUserKey.ToString
-
Dim NameId = value.ToString
-
'SQL Input
-
-
Dim QGroup
-
Dim QAnswer As Object
-
Dim QLabel
-
-
-
-
-
QGroup = Profile.Opportunity
-
Dim i As Integer
-
i = 0
-
Do Until i > 26
-
i = i + 1
-
-
' QAnswer = ("QAnswer" & (i) & ".Text")
-
' QLabel = ("QLabel" & (i) & ".Text")
-
If i = 1 Then
-
QLabel = QLabel1.Text
-
QAnswer = QAnswer1.Text
-
ElseIf i = 2 Then
-
QLabel = QLabel2.Text
-
QAnswer = QAnswer2.Text
-
ElseIf i = 3 Then
-
QLabel = QLabel3.Text
-
QAnswer = QAnswer3.Text
-
ElseIf i = 4 Then
-
QLabel = QLabel4.Text
-
QAnswer = QAnswer4.Text
-
ElseIf i = 5 Then
-
QLabel = QLabel5.Text
-
QAnswer = QAnswer5.Text
-
ElseIf i = 6 Then
-
QLabel = QLabel6.Text
-
QAnswer = QAnswer6.Text
-
ElseIf i = 7 Then
-
QLabel = QLabel7.Text
-
QAnswer = QAnswer7.Text
-
ElseIf i = 8 Then
-
QLabel = QLabel8.Text
-
QAnswer = QAnswer8.Text
-
ElseIf i = 9 Then
-
QLabel = QLabel9.Text
-
QAnswer = QAnswer9.Text
-
ElseIf i = 10 Then
-
QLabel = QLabel10.Text
-
QAnswer = QAnswer10.Text
-
ElseIf i = 11 Then
-
QLabel = QLabel11.Text
-
QAnswer = QAnswer11.Text
-
ElseIf i = 12 Then
-
QLabel = QLabel12.Text
-
QAnswer = QAnswer12.Text
-
-
' ..... Up to i =27
-
-
End If
-
-
' If QLabel.GetType().ToString() = "System.Web.UI.WebControls.Label" Then
-
' Dim txtLabel As Label = DirectCast(QLabel, Label)
-
' txtLabel.Text = Trim(txtLabel.Text)
-
' End If
-
-
Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
-
Dim myConnection As New SqlConnection(connStr)
-
Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
-
-
Dim myCommand As New SqlCommand(SQL, myConnection)
-
-
myCommand.Parameters.AddWithValue("@UserID", NameId)
-
myCommand.Parameters.AddWithValue("@QGroup", QGroup)
-
myCommand.Parameters.AddWithValue("@QLabel", QLabel)
-
myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
-
-
'For Testing Only
-
'Label2.Text = NameId
-
-
Try
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
myConnection.Close()
-
Catch ex As Exception
-
'For Testing Only
-
Label1.Visible = True
-
Label1.Text = ex.Message.ToString
-
End Try
-
Loop
-
8 2694
What is the error you receive?
What is the error you receive?
Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Label'.
-
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArg s) Handles Wizard1.FinishButtonClick
-
-
'Get Membership GUID
-
'Label2.Text = "Geeting User"
-
Dim instance As MembershipUser = Membership.GetUser()
-
Dim value As Object
-
If instance Is Nothing Then
-
Label2.Text = "NO USER IS LOGGED IN"
-
Exit Sub
-
End If
-
value = instance.ProviderUserKey.ToString
-
Dim NameId = value.ToString
-
'SQL Input
-
-
Dim QGroup
-
Dim QAnswer As Object
-
Dim QLabel
-
-
QGroup = Profile.Opportunity
-
Dim i As Integer
-
i = 0
-
Do Until i > 26
-
i = i + 1
-
-
' QAnswer = ("QAnswer" & (i) & ".Text")
-
' QLabel = ("QLabel" & (i) & ".Text")
-
If i = 1 Then
-
QLabel = QLabel1.Text
-
QAnswer = QAnswer1.Text
-
ElseIf i = 2 Then
-
QLabel = QLabel2.Text
-
QAnswer = QAnswer2.Text
-
ElseIf i = 3 Then
-
QLabel = QLabel3.Text
-
QAnswer = QAnswer3.Text
-
' ..... Up to i =27
-
-
End If
-
-
' If QLabel.GetType().ToString() = "System.Web.UI.WebControls.Label" Then
-
' Dim txtLabel As Label = DirectCast(QLabel, Label)
-
' txtLabel.Text = Trim(txtLabel.Text)
-
' End If
-
-
Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
-
Dim myConnection As New SqlConnection(connStr)
-
Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
-
-
Dim myCommand As New SqlCommand(SQL, myConnection)
-
-
myCommand.Parameters.AddWithValue("@UserID", NameId)
-
myCommand.Parameters.AddWithValue("@QGroup", QGroup)
-
myCommand.Parameters.AddWithValue("@QLabel", QLabel)
-
myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
-
-
'For Testing Only
-
'Label2.Text = NameId
-
-
Try
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
myConnection.Close()
-
Catch ex As Exception
-
'For Testing Only
-
Label1.Visible = True
-
Label1.Text = ex.Message.ToString
-
End Try
-
Loop
-
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.
[
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
I have declared it several ways all with various results. Nothing seems to work.
RTW
What is the error you're getting?
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. - 'SQL Input
-
-
Dim QGroup
-
Dim QAnswer As String
-
Dim QLabel As String
-
-
'Inserted dummy string text
-
QLabel = "N/A"
-
QAnswer = "N/A"
-
-
QGroup = Profile.Opportunity
-
Dim i As Integer
-
i = 0
-
Do Until i > 11
-
i = i + 1
-
-
-
If i = 1 Then
-
QLabel = QLabel1.Text
-
QAnswer = QAnswer1.Text
-
ElseIf i = 2 Then
-
QLabel = QLabel2.Text
-
QAnswer = QAnswer2.Text
-
ElseIf i = 3 Then
-
QLabel = QLabel3.Text
-
QAnswer = QAnswer3.Text
-
ElseIf i = 4 Then
-
QLabel = QLabel4.Text
-
QAnswer = QAnswer4.Text
-
ElseIf i = 5 Then
-
QLabel = QLabel5.Text
-
QAnswer = QAnswer5.Text
-
ElseIf i = 6 Then
-
QLabel = QLabel6.Text
-
QAnswer = QAnswer6.Text
-
ElseIf i = 7 Then
-
QLabel = QLabel7.Text
-
QAnswer = QAnswer7.Text
-
ElseIf i = 8 Then
-
QLabel = QLabel8.Text
-
QAnswer = QAnswer8.Text
-
ElseIf i = 9 Then
-
QLabel = QLabel9.Text
-
QAnswer = QAnswer9.Text
-
ElseIf i = 10 Then
-
QLabel = QLabel10.Text
-
QAnswer = QAnswer10.Text
-
ElseIf i = 11 Then
-
QLabel = QLabel11.Text
-
QAnswer = QAnswer11.Text
-
ElseIf i = 12 Then
-
QLabel = QLabel12.Text
-
QAnswer = QAnswer12.Text
-
End If
-
-
Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
-
Dim myConnection As New SqlConnection(connStr)
-
Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
-
-
Dim myCommand As New SqlCommand(SQL, myConnection)
-
-
myCommand.Parameters.AddWithValue("@UserID", NameId)
-
myCommand.Parameters.AddWithValue("@QGroup", QGroup)
-
myCommand.Parameters.AddWithValue("@QLabel", QLabel)
-
myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
-
-
'For Testing Only
-
'Label2.Text = NameId
-
-
Try
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
myConnection.Close()
-
Catch ex As Exception
-
'For Testing Only
-
Label1.Visible = True
-
Label1.Text = ex.Message.ToString & (i)
-
End Try
-
Loop
-
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |