Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Can someone please advise me how or what i need to assign to the varicale 'ddl' as i a little stuck after getting this error 'variable 'ddl' is used before it has been assigned a value'

I am trying to populate my dropdown with data from database using the querystring.

VB
If Request.QueryString("userid") <> "" Then

         Dim ddlResult As DataSet
         ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

         Dim ddl As DropDownList

         For Each encounter In ddlResult.Tables(0).Rows

             Dim ddlString As String = encounter.Item("description").ToString

             Dim newListItem As ListItem
             newListItem = New ListItem("description")
             ddl.Items.Add(newListItem)

         Next

     End If

thank you

I have tried adding a little bit more code as shown below but the DDL is not showing up on the page. I cant add the DDL direct to the page because for some reason it stops the button from working i use to open the page.

VB
If Request.QueryString("userid") <> "" Then

          Dim ddl As DropDownList = New DropDownList

          Dim ddlResult As DataSet
          ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

          Dim dv As New HtmlGenericControl("div")
          dv.ID = "sysData"

          For Each encounter In ddlResult.Tables(0).Rows

              Dim ddlString As String = encounter.Item("description", "description").ToString

              Dim newListItem As ListItem
              newListItem = New ListItem("description", "description")


              ddl.Items.Add(newListItem)
              dv.Controls.Add(ddl)
              sysData.Controls.Add(dv)

          Next

      End If
Posted
Updated 22-Jan-15 21:43pm
v4

Instantiate the ddl
Dim ddl As New DropDownList
 
Share this answer
 
All you have done is create a variable ddl. You need to assign a value to that variable before you can add items to it.
E.g.
VB
Dim ddl As DropDownList = this.Page.Master.FindControl("YourDropDownList")

If this is WinForm then
VB
Dim ddl As DropDownList = this.YourDropDownList

(you need to substitute the id of the drop down list on your page/form for "YourDropDownList") or you could use
VB
Dim ddl As DropDownList = New DropDownList()
- but no-one is going to see your list
 
Share this answer
 
Comments
Member 11355710 23-Jan-15 3:40am    
I have tried your code and the error has gone, but you are right in saying no one will see the DDL. Can you tell me why i cant see the DDL on screen.
CHill60 23-Jan-15 4:22am    
Because you have just created a variable of type DropDownList and populated it. You can use that DropDownList in your local code but for it to be visible it has to be added to the list of controls for the page/form.
If you already have a dropdown list on your page/form then you need to set the local variable to that list ... as I tried to do with the first two examples in my solution.
That DropDownList is already on the form and already visible - you just need to populate it
Member 11355710 23-Jan-15 4:27am    
Ok i tried both examples but neither work im afraid. It tells me that 'this' is not declared and if i remove that the code seems happy but still nothing showing
CHill60 23-Jan-15 4:39am    
Sorry - my fault. That first line is more of a guide than the absolute solution.
You need to use the FindControl function to locate the dropdown on your page (I'm guessing this is not winform). If you don't have a master page then the syntax will be different (- and you'll need to change "YourDropDownList" to the name/id of the one on your page).
If you look up the reference for "FindControl" then you should see the options
Member 11355710 23-Jan-15 4:59am    
Not at all your help is very much appriciated. I do have a masterpage but not matter what i do the DDL doesnt show. Im not sure if i am missing something else in the code as i have noticed i am not using the 'ddlString' variable anywhere, would this matter?
change
VB
Dim ddl As DropDownList

to
VB
Dim ddl As New DropDownList

use the New keyword to create a new instance of DropDownList

if you already added DropDownList to your aspx page like below
ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" />

then you don't need to create new one. access it by ID, sample code
VB
For Each encounter In ddlResult.Tables(0).Rows

          Dim ddlString As String = encounter.Item("description").ToString

          Dim newListItem As ListItem
          newListItem = New ListItem("description")
          DropDownList1.Items.Add(newListItem)

      Next
 
Share this answer
 
v2
DDl is not initialized - Dim ddl As New DropDownList
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900