Question Error selecting a parameter from a dropdownlist

Coffer

Member
Joined
Jun 26, 2009
Messages
7
Programming Experience
5-10
I have a problem I cannot find a solution to. I have a drop-down list that is dynamically filled with a set of values. By viewing the source code in the browser I can tell that this list is correctly filled with the "value" and "text" parameters.

But if I try to retrieve the value using ddl.SelectedItem.Value I get the error "Object reference not set to an instance of an object" which means that there is no parameter in "value" to retrieve ...

Page_Load() contains an "If Not IsPagePostback" in which the list is filled. I don't want to use AutoPostback = True since I want to retrieve the value-parameter when I click a "save" button. I must have missed something fundamental here that I do not grasp ...

The code to fill the list looks like this:
VB.NET:
Expand Collapse Copy
Dim ds as Dataset ()
ds.ReadXml([I]the path to an xml file[/I])
ddl.Items.Add(New ListItem("destination", "destination"))
ddl.DataSource = ds.Tables(0).DefaultView
ddl.DataBind()
This snippet does not generate any errors: Everything is printed as it should! But inside my Submit-function I cannot be retrieve the parameter:
VB.NET:
Expand Collapse Copy
objcmd.parameter.add("?destination", OdbcType.VarChar).Value = ddl.SelectedItem.Value
Can anyone help me? I really need to get this right...
 
Say that you want to populate ddl with the following xml data:


PHP:
Expand Collapse Copy
<?xml version="1.0" encoding="utf-8" ?>
<Members>
  <member>
    <MemberID>1</MemberID>
    <MemberName>Mike</MemberName>
  </member>
  <member>
    <MemberID>2</MemberID>
    <MemberName>Bob</MemberName>  
</member>
  <member>
    <MemberID>3</MemberID>
    <MemberName>Ben</MemberName>  
</member>
  <member>
    <MemberID>4</MemberID>
    <MemberName>Julie</MemberName>  
</member>
  <member>
    <MemberID>5</MemberID>
    <MemberName>Oliver</MemberName>  
</member>
</Members>

Then in the code-behind:
VB.NET:
Expand Collapse Copy
    Private Sub PopulateDropDownList()
        Dim dsMembers As New DataSet
        Try
            dsMembers.ReadXml(Server.MapPath("~/files/XMLFile.xml"))
            DropDownList1.DataSource = dsMembers
            DropDownList1.DataValueField = "MemberID"
            DropDownList1.DataTextField = "MemberName"
            DropDownList1.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
 
Back
Top