Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2008

Tip: Looking for answers? Try searching our database.

problem how to pass value from one procedure to another

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 20 May 2008 22:34 GMT
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 ....."
...
Cor Ligthert[MVP] - 21 May 2008 04:32 GMT
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

> Hi,
>
[quoted text clipped - 35 lines]
> sql="insert ....."
> ...
Steve Gerrard - 21 May 2008 06:42 GMT
Inline comments:

> Hi,
>
[quoted text clipped - 6 lines]
>
> Any idea how to solve this?

> Dim dd() As DropDownList
> Dim i As Integer
[quoted text clipped - 3 lines]
> dd(i).ID = id        'can be anything
> next

What is id? It is not declared anywhere, and is never assigned a value. In every
sense of the word, it is nothing. It would make more sense to write something
like
   dd(i).ID = i
which would assign the value of i to the ID property of the dropdown.

> 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

Again, what is id? It is not declared anywhere, and is never assigned a value.
In every sense of the word, it is nothing. It would make more sense to write
something like
   Session("dd" & dd.ID) = dd.SelectedValue
which would store the selected value in a session variable with the name ddx,
where x is the value of the ID property of the drop down, as assigned in the
previous loop above.

> Protected Sub submit_Click(ByVal sender As Object, ByVal e As
> System.EventArgs)
[quoted text clipped - 4 lines]
> Next
> sql="insert ....."

This is closer, in that dd.id is presumably an attempt to get the ID property of
a drop down. Unfortunately, dd itself is never declared, and never assigned a
value, and this time it is dd that is nothing. It would make more sense to write
   selvalue = selvalue & Session("dd" & i)
which will retrieve each of the 5 values stored in session variables by the
event handler above - if all five have been set.

It might be a good idea to store something in the Session variables for all 5 to
start with, in case some of them never get set. The first loop would be a
suitable place to do that.
Dan - 21 May 2008 10:19 GMT
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

> Inline comments:
>
[quoted text clipped - 56 lines]
> all 5 to start with, in case some of them never get set. The first loop
> would be a suitable place to do that.
Andrew Morton - 21 May 2008 13:23 GMT
> 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
Dan - 21 May 2008 14:47 GMT
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

>> Protected Sub submit_Click(ByVal sender As Object, ByVal e As
>> System.EventArgs)
[quoted text clipped - 5 lines]
>
> Andrew
Andrew Morton - 21 May 2008 16:16 GMT
> Thanks but i don't understand what you mean.

Ignore what I wrote - it was rubbish. It is Steve Gerrard's post that makes
sense.

Andrew
Cor Ligthert[MVP] - 21 May 2008 18:05 GMT
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

>> Thanks but i don't understand what you mean.
>
> Ignore what I wrote - it was rubbish. It is Steve Gerrard's post that
> makes sense.
>
> Andrew
Dan - 21 May 2008 19:23 GMT
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

> Andrew,
>
[quoted text clipped - 28 lines]
>>
>> Andrew
Cor Ligthert[MVP] - 22 May 2008 04:51 GMT
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

> Cor,
>
[quoted text clipped - 34 lines]
>>>
>>> Andrew
Steve Gerrard - 22 May 2008 06:02 GMT
> Protected Sub dropd(ByVal sender As Object, ByVal e As
> System.EventArgs) Dim dd As DropDownList = CType(sender, DropDownList)
[quoted text clipped - 5 lines]
> 'how to get the selectvalue here?
> End Sub

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
Dan - 22 May 2008 14:12 GMT
Thanks

>> Protected Sub dropd(ByVal sender As Object, ByVal e As
>> System.EventArgs) Dim dd As DropDownList = CType(sender, DropDownList)
[quoted text clipped - 28 lines]
>        selvalue = selvalue & Session(nID) & ":"
>    Next nID

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.