problem how to pass value from one procedure to another

D

Dan

Hi,

I create 5 dropdownlist in code-behind. I want to insert their
selectedvalues in a table by making a string separated with ":"
I can get their selecedvalues but i'm stuck when i want to execute the
insert.
The error is "name dd is not declared" (in line: selvalue = selvalue &
Session("dd" & dd.id) & ":"

Any idea how to solve this?
Thanks for help
Dan

Dim dd() As DropDownList
Dim i As Integer

for i= 1 to 5
dd(i) = New DropDownList
dd(i).ID = id 'can be anything
next

For Each d As DropDownList In dds
AddHandler d.SelectedIndexChanged, AddressOf dropd
Next

Protected Sub dropd(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dd As DropDownList = CType(sender, DropDownList)
Session("dd" & id) = dd.SelectedValue
End Sub

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim i As Integer
Dim selvalue As String
For i = 1 To 5
selvalue = selvalue & Session("dd" & dd.id) & ":"
Next
sql="insert ....."
....
 
C

Cor Ligthert[MVP]

Dan,

I don't know what other errrors there can be, but in my idea is dd (the
dropdownList) not declared, in all other methods you are getting it from the
sender or declare it new.

Cor
 
D

Dan

Thanks for replying.
I think my explanation was not good enough. I ommitted some code to restrict
the message.

This is the real problem: there are a unknown number of dropdownlist
(questions of a survey) (i took 5 as example) depending of the user who
create the questions.
The dropdownlists (some are not visible) have the property AutoPostBack =
true, because the value of dropdownlist x can make another further
dropdownlist visible. So each time an user introduces a value, the depending
DD will be visible or remains unvisible. This is solved.

Now, i want to collect all the selectedvalues and put them into a table.
But i can't find a way to pass the collected selectedvalues in the submit
procedure.


Friend dds As New List(Of DropDownList)
....
Dim dd() As DropDownList

For i = 1 To (number of dropdownlist)
dd(i) = New DropDownList
dd(i).ID = id 'id is the id of the related quesion in the table
dd(i).AutoPostBack = True
'fed from table
....
form1.Controls.Add(dd(i))
....

For Each d As DropDownList In dds
AddHandler d.SelectedIndexChanged, AddressOf dropd
Next

Protected Sub dropd(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dd As DropDownList = CType(sender, DropDownList)
session(dd.ID)=dd.selectedValue
End Sub

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
'how to get the selectvalue here?
End Sub

Thanks
 
A

Andrew Morton

Dan said:
Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
'how to get the selectvalue here?

I think you need something like DirectCast(sender,
DropDownList).SelectedValue (and I hope you have Option Strict On at the top
of the script).

Andrew
 
D

Dan

Thanks but i don't understand what you mean.
I have the selectedvalue in procedure Sub dropd.
The problem is: how to put it in procedure Sub submit_Click in order to
insert it in the table?
Where should i put your code?
thanks
 
C

Cor Ligthert[MVP]

Andrew,

Just ignoring my post?

As the computer tells,
The error is "name dd is not declared" (in line: selvalue = selvalue &
Session("dd" & dd.id) & ":"

In the procedure

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim i As Integer
Dim selvalue As String
For i = 1 To 5
selvalue = selvalue & Session("dd" & dd.id) & ":"
Next
sql="insert ....."

Then it has nothing to do with an ID, it is as is told. The object dd is not
declared.
And as we could see that it was declared in another method the change that
it is a global object is low.

Cor
 
D

Dan

Cor,

i don't ignore your post, but please, forget Andrew and give me at least a
hint (i reformulated the global problem two posts above.
Thanks
 
C

Cor Ligthert[MVP]

Dan,

In my idea are you thinking that dd is a dropdownlist box, but it is not
even declared in your procedure somewhere else you write.

dim dd as dropdownlist = DirectCast(sender, DropdownList)
(You use CType, but DirectCast is better in this case because it does not
have to convert, the sender is your dropdownlist in an object)

I have the idea that it is something the same.

Cor
 
D

Dan

Thanks

Steve Gerrard said:
Okay, at the submit_Click event, the sender is the submit button, so that
is no help. You have two choices: look through the dropdowns themselves,
or look in the Session object. If you are maintaining ViewState, the
dropdowns should be present and have the current SelectedValue. Your dropd
event handler will also have stored any that changed in the Session
object, if you are using that instead.

If you are going to use ViewState, you don't need the event handler to
store anything in Session. Just gather the selected values from your dd
array of drop down lists.

If you are going to use the Session object, you should initialize it with
the initial values of all the dropdown selected values, so there is one
value for each drop down. That will get updated if any are changed by the
user, using your event. Then you can retrieve all the selected values from
the Session object in your submit event. Since you assigned IDs to the
dropdowns from a table of questions, you can use the same table to come up
with the IDs you need to retrieve the stored values.
Air code for that looks like
For each nID in tableOfQuestionIDs
selvalue = selvalue & Session(nID) & ":"
Next nID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top