We help IT Professionals succeed at work.
Get Started

SelectedIndex

CochiseCounty
on
304 Views
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
    BindDataList
  End If
End Sub

Sub BindDataList
      Dim conWorkShop As SqlConnection
    Dim cmdSelect As SqlCommand
    Dim strConString as String    
    strConString=ConfigurationSettings.AppSettings("conString")
      conWorkShop = New SqlConnection( strConString  )
    conWorkShop.Open()    
      cmdSelect = New SqlCommand( "Select State as ID, City, Address, StudentID From StudentInfo WHERE StudentID='"&Session("StudentID")&"'", conWorkShop )
      strStudentInfo.DataSource = cmdSelect.ExecuteReader()    
      strStudentInfo.DataBind()  
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()      
      strConString=ConfigurationSettings.AppSettings("conString")
      conWorkShop = New SqlConnection( strConString  )
    conWorkShop.Open()    
    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
</Script>
<html>
<head><title>Student Profile</title></head>
<body>
<form Runat="Server" ID="Form2">
<asp:DataList
      ID="strStudentInfo"
      DataKeyField="StudentID"  
      CellPadding="10"
      EditItemStyle-BackColor="lightgrey"
      Runat="Server">
      <ItemTemplate>
            <br><FONT face="Arial"><FONT size="2">Address:<FONT color="#cc0033">*</FONT></FONT></FONT>
            <asp:TextBox
                  ID="txtAddress"
                  MaxLength="100"
                  Text='<%# Container.DataItem( "Address" )%>'
                  Runat="Server" />
                  
            <br><FONT face="Arial"><FONT size="2">City:<FONT color="#cc0033">*</FONT></FONT></FONT>
            <asp:TextBox
                  ID="txtCity"
                  MaxLength="30"
                  Text='<%# Container.DataItem( "City" )%>'
                  Runat="Server" />      
                              
            <asp:dropdownlist
                  ID="dropState"                        
                  DataSource="<%# GetCategories() %>"
                  DataTextField="State"
                  DataValueField="ID"       
                  SelectedIndex='<%# GetSelectedIndex(Container.DataItem("ID")) %>'                  
                  Runat="server" >                        
            </asp:dropdownlist>      
      </ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
Comment
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