findcontrol

N

nicholas

I have a page on which several dropdownlists are placed dynamically.
So, sometimes there are no ddl's and sometimes there a are let's say 10
ddl's.

These ddl's are created in my code.

Now, that is no problem. The problem is that when I want to INSERT values
INTO a table in my database, I should be able to get the value of the
selecteditem of the dropdowns.
And as these controls are not realy on the page, I always get an error.
(object reference not set to an instance of an object)

I tried several combinations with findcontrol, but nothing works...

Any help is appreciated ! THX

Here is 1 of my tries:

############# code ########
if txt_counter.text > 0 then 'this tells me how
much dropdownlists were created on page load (they were named: dd_1, dd_2,
etc)

Dim counter as integer

for counter=1 to txt_counter.text

Dim mydropdown1 As dropdownlist =
Page.FindControl("dd_" & counter)

If Not mydropdown1 Is Nothing Then

'insert the listproductoptions
Dim dbComm1 As New SQLCommand()
dbComm1.CommandType = CommandType.Text
dbComm1.CommandText = "INSERT INTO
tbl_listproductsoptions(listproductID, optionID) VALUES (@listproductID,
@optionID)"
dbComm1.Connection = MyConnection



dbComm1.Parameters.Add("@listproductID",SqlDbType.int)

dbComm1.Parameters.Add("@optionID",SqlDbType.int)


dbComm1.Parameters("@listproductID").Value =
intNewID
dbComm1.Parameters("@optionID").Value =
mydropdown1.selecteditem.value


'MyConnection.Open()
dbComm1.ExecuteScalar()

end if

counter = counter + 1

Next

end if
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top