Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

ASP .NET dropDownList problem

  • 10-06-2004 03:22PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 937 ✭✭✭Diddy Kong


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


  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 937 ✭✭✭Diddy Kong


    thank you very much.....


Advertisement