We help IT Professionals succeed at work.
Get Started


Last Modified: 2012-06-27
I have this page, it has a Databinding DropDownList control inside a Datalist control, what I want is the selected item in the dropdownlist control is the value of the datalist control. When I run this code, I received an error message

Object reference not set to an instance of an object. Please help. THanks.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">
Dim ddlDataSet as DataSet = New DataSet()
Public Sub Page_Load
  If Not IsPostBack Then
  End If
End Sub

Sub BindDataList
      Dim conWorkShop As SqlConnection
    Dim cmdSelect As SqlCommand
    Dim strConString as String    
      conWorkShop = New SqlConnection( strConString  )
      cmdSelect = New SqlCommand( "Select State as ID, City, Address, StudentID From StudentInfo WHERE StudentID='"&Session("StudentID")&"'", conWorkShop )
      strStudentInfo.DataSource = cmdSelect.ExecuteReader()    
End Sub

Function GetCategories() as DataSet
      Dim conWorkShop As SqlConnection
    Dim cmdSelect As SqlDataAdapter
    Dim strState As SqlDataAdapter
    Dim strConString as String
    Dim ddlDataSet As DataSet=New DataSet()      
      conWorkShop = New SqlConnection( strConString  )
    cmdSelect = New SqlDataAdapter( "Select ID, State, Code From USStates", conWorkShop )    
    cmdSelect.Fill(ddlDataSet, "State")
    Return ddlDataSet
End Function
 Function GetSelectedIndex(CID as String) as Integer
    Dim iLoop as Integer
    Dim dt as DataTable = ddlDataSet.Tables("State")
    For iLoop = 0 to dt.Rows.Count - 1
      If Int32.Parse(CID) = Int32.Parse(dt.Rows(iLoop)("ID")) then
        Return iLoop
      End If
    Next iLoop
  End Function
<head><title>Student Profile</title></head>
<form Runat="Server" ID="Form2">
            <br><FONT face="Arial"><FONT size="2">Address:<FONT color="#cc0033">*</FONT></FONT></FONT>
                  Text='<%# Container.DataItem( "Address" )%>'
                  Runat="Server" />
            <br><FONT face="Arial"><FONT size="2">City:<FONT color="#cc0033">*</FONT></FONT></FONT>
                  Text='<%# Container.DataItem( "City" )%>'
                  Runat="Server" />      
                  DataSource="<%# GetCategories() %>"
                  SelectedIndex='<%# GetSelectedIndex(Container.DataItem("ID")) %>'                  
                  Runat="server" >                        
Watch Question
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE