Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP .NET dropDownList problem

Options
  • 10-06-2004 4:22pm
    #1
    Registered Users Posts: 937 ✭✭✭


    I have a drop down list which pulls data form a database, one value is selected and you click a button and it then displays (should) other details...i get "System.NullReferenceException: Object reference not set to an instance of an object." error when i click on the button to view the other details

    any help much appreciated

    The wonky bit of code:
    strState=ddl1.SelectedItem.Text

    Thats where it says the error is

    thanks


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Can you post the code used to bind the control? My intial guess is you should be using
    strState = ddl1.SelectedItem.Value
    


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    tried the value but the same problem code

    <script language="VB" runat="server">
    Dim MyArrayList as ArrayList
    Dim sItem as String
    Sub Page_Load(Source as Object, E as EventArgs)
    MyArrayList=New Arraylist
    if not Page.IsPostBack then
    Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    server.mappath("db1.mdb") & ";"
    Dim MySQL as string = "Select PT_ID from TempDb"
    Dim MyConn as New OleDbConnection(strConn)
    Dim objDR as OleDbDataReader
    Dim Cmd as New OleDbCommand(MySQL, MyConn)
    MyConn.Open()
    objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
    While objDR.Read()
    MyArrayList.add(objDR("PT_ID"))
    End While
    end if
    ddl1.datasource=MyArrayList
    ddl1.databind
    End Sub

    Sub doQuery(Source as Object, E as EventArgs)
    Dim DS As DataSet
    Dim MyConnection As OleDbConnection
    Dim MyCommand As OleDbDataAdapter
    Dim strState as string
    strState=ddl1.SelectedItem.Text
    Label1.Text="You chose: " + strState

    Dim MySQL as string
    MySQL="select DESC from TempDb where PT_ID='" & strState & "'"
    panel1.visible="true"
    label2.text=MySQL
    MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    server.mappath("db1.mdb") & ";")
    MyCommand = New OleDbDataAdapter(MySQL, MyConnection)
    DS = new DataSet()
    MyCommand.Fill(ds, "TempDb")

    MyDataGrid.DataSource=ds.Tables("TempDb").DefaultView
    MyDataGrid.DataBind()
    End Sub

    </script>


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Okay try this:
    Dim MyArrayList as ArrayList 
    Dim sItem as String
    Sub Page_Load(Source as Object, E as EventArgs)
       MyArrayList=New Arraylist 
       if not Page.IsPostBack then
          Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
          server.mappath("db1.mdb") & ";"
          Dim MySQL as string = "Select PT_ID from TempDb"
          Dim MyConn as New OleDbConnection(strConn)
          Dim objDR as OleDbDataReader
          Dim Cmd as New OleDbCommand(MySQL, MyConn)
          MyConn.Open()
    [color=green] 'Here's where I made my changes[/color]
          [color=red]ddl1.datasource=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
          ddl1.DataValueField = "PT_ID"
          ddl1.databind[/color]
    End Sub
    

    This will bind the datareader PT_ID field directly to you dropdownlist and gets rid of the arraylists. You can then reference the value using the code I posted first, being:
    strState = ddl1.SelectedItem.Value
    
    Forgive my vb code, I'm a c# boy these days. Try it and let me know :)


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    its missing an end if.....where do you want me to stick that??


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    ok i took out the page.ispostback statement and it worked....

    thanks for your help
    very much appreciated


  • Advertisement
  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    might have been a bit premature there....it isnt updating...take it thats what the page.isnotpostback was for......


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Dim MyArrayList as ArrayList 
    Dim sItem as String
    Sub Page_Load(Source as Object, E as EventArgs)
       MyArrayList=New Arraylist 
       if not Page.IsPostBack then
          Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
          server.mappath("db1.mdb") & ";"
          Dim MySQL as string = "Select PT_ID from TempDb"
          Dim MyConn as New OleDbConnection(strConn)
          Dim objDR as OleDbDataReader
          Dim Cmd as New OleDbCommand(MySQL, MyConn)
          MyConn.Open()
    [color=green] 'Here's where I made my changes[/color]
          [color=red]ddl1.datasource=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
          ddl1.DataValueField = "PT_ID"
          ddl1.databind
       [b]end if[/b][/color]
    End Sub
    

    There you go, lazy bones :D


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    thank you very much.....


Advertisement