Saturday, March 31, 2012

Setting multiple drop down lists selectedvalue via sql reader

I have 4 dropdown lists which I am trying to set the selectedvalue from a sql reader. The reader uses a simple stored procedure:

SELECT EmployeeID
FROM dbo.tbl_Employees
WHERE CompanyID = @dotnet.itags.org.CompanyID

This will bring back 3-4 EmployeeID's. The drop down lists are pre-populated before this reader is called. All I am trying to do is set the selectedvalue of each drop down with the EmployeeID value returned from the stored proc - drop down 1 selectedvalue = EmployeeID 1, drop down 2 selectedvalue = EmployeeID 2 and so on.

Any Ideas?

Rich

You can use the DropDownList.SelectedValue to do that:

DropDownList1.SelectedValue = Convert.ToString(reader["ID"]);

That part I got - how do I loop to the next value in the reader? What I see happening is while in the While - End While block, the reader goes thru all returned values at once, I tried to use

DropDownList1.SelectedValue = Convert.ToString(reader["ID"])
DropDownList2.SelectedValue = Convert.ToString(reader["ID"])
DropDownList3.SelectedValue = Convert.ToString(reader["ID"])
with all drop downs with the same ID
 
Rich
 


Can you post your code and explain what exactly you are trying to do? Do you want same IDs in all three dropdowns?


I don't have much of the code yet - here is what I have:

sqlReader = sqlCmd.ExecuteReader

If sqlReader.Read = True Then

While sqlReader.Read()
ddDropDown01.SelectedValue = sqlReader.GetValue(0).ToString
End While

End If

As I debug this, I can see the loop action if the While - End While and watch the SelectedValue change on each loop.

What I am looking to do is this: execute the reader and return 4 EmployeeID's, set DropDown01.SelectedValue to EmployeeID 1, set DropDown02.SelectedValue to EmployeeID 2, set DropDown03.SelectedValue to EmployeeID 3 and set DropDown04.SelectedValue to EmployeeID 4.

Rich


Well in this case, if you only have 4 dropdowns, you can do 4 reads, then set the value of dropdownlist in each read.

sqlReader.Read();ddDropDown01.SelectedValue = sqlReader.GetValue(0).ToString();sqlReader.Read();ddDropDown02.SelectedValue = sqlReader.GetValue(0).ToString();sqlReader.Read();ddDropDown03.SelectedValue = sqlReader.GetValue(0).ToString();sqlReader.Read();ddDropDown04.SelectedValue = sqlReader.GetValue(0).ToString();

If there is a chance of having more dropdowns, then we can work on a looping structure.


Ok - so it was that easy. I thought that I had to loop it by default. I will not have more that 4 drop downs so this will work fine.

Thanks for your help with this - Rich.



Sure...


Ok - so after working with the answer for this post, I now think I need to loop this cause I will have more than 4 drop downs in some cases and less than 4 in others.

How can I call out the different dropdowns in the reader loop?

Rich



Well, without looping through the controls on the page, which can get messy, your options are limited.

At any 1 time, you should have a finite number of dropdown lists to work with. You'll probably jsut have to hardcode them into your loop. Its not elegant, but neither is this implementation strategy (ie. I would look for a different way of accomplishing what you are tyring to do. Maybe a list box to allow multiple selections?)

i = 0
While SDR.Read()
IF i = 0 THEN
ddl1.SelectedValue = Cstr(sdr.item(0))
elseif i = 1 THEN
ddl2.SelectedValue = Cstr(sdr.item(0))
....
end if
i+=1
Wend


Can you use i as part of the dropdown name? --> something like (dropdownname + i ).selectedvalue would become dropdown3.selectedvalue?

Rich


something to investigate would be adding the dropdowns to a hashtable and looping through the hashtable.


Ok - It's late and I think I am getting close.

Here is what I have so far:

sqlReader = sqlCmd.ExecuteReader

Dim ddname As New DropDownList
Dim i As Integer = 1

Do While sqlReader.Read()
ddname = Me.FindControl("ddCompanyEmployee0" + i.ToString)
ddname.Enabled = True
ddname.Visible = True
util.PopulateDropDownList("sp_EmployeeLookup", "", 0, ddname, "(Select Employee)")
ddname.SelectedValue = sqlReader("EmployeeID")
Session("CompanyEmployee0" + i.ToString) = sqlReader("EmployeeID")
i += 1
Loop

This works GREAT on one page where I have it. I copied it over to a second page where I need the same functionality and wham, does not work. On the page where it works, the only difference is the drop down name is "ddSelectEmployee0" not "ddCompanyEmployee0". When I step thru the page that works, I can get the DropDown info in the step "ddname = Me.FindControl("ddSelectEmployee0" + i.ToString)". In the page where this does not work, I get "ddname|nothing". I am almost sure that the two pages are identical in setup. Any ideas on this issue?

Rich


paste the code on the page which is not working..

As mentioned, the code is the same on both pages. The only change is the drop down name.

Rich


0 comments:

Post a Comment