dropdownlist databind!! please help..

son

Regular
Joined
Feb 21, 2004
Messages
67
hi all
i have been on this problem for like 3 days now its time for some help cause it driving me insane..

i am using SQL SERVER 2000 where my database is stored.. i am using the northwind database.

i am using vb.net and asp.net. i have a webform1 that has the connection to the database on the startup page i am using the server explorer to add the sqlconnection , sqladapater and the dataset components.. i am caching the the components if the page is not posted back to the server and filling the dataset with the data: my code on the first form is as follows

Visual Basic:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            'add the tables to the cache
                        addtocache("SqlDataAdapter2", SqlDataAdapter2) '"adaptCustomerCustomerDemo"
            

            'fill the datasets
            SqlDataAdapter2.Fill(DsCustomerCustomerDemo1)
            addtocache("DsCustomerCustomerDemo1", DsCustomerCustomerDemo1) 'dsCustomerCustomerDemo
             End If

    End Sub


Sub addtocache(ByVal name As String, ByVal obj As Object)
        If Not IsNothing(Cache(name)) Then Return
        Cache.Add(name, obj, Nothing, DateTime.MaxValue, System.TimeSpan.FromMinutes(20), _
Caching.CacheItemPriority.Default, Nothing)
    End Sub
on the main page(webform1) i have a link which takes you to another webform2 and i have a dropdownlist on webform2 which needs to be databinded to fill up the dropdownlist which i have..my code on webform2 is as follows

Visual Basic:
imports system.data.sqlclient

public sqlDataAdapter2 as sqlDataAdapter  ' 
Public dsCustomerCustomerDemo1 As dsCustomerCustomerDemo

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        
        dsCustomerCustomerDemo1 = Cache("dsCustomerCustomerDemo1")

        If Not IsPostBack Then
       
            drpCustomerCustomerDemo.DataBind()

        End If

    End Sub

can someone please help because nothing is filling up my dropdownlist..
thanks in advance... hope i explained myself ok...
 
hi thanks for your reply:

the datasource i am setting in the webform2 by selecting the dropdownlist and in the properties window of the dropdownlist,
the datasource property to DsCustomerCustomerDemo (name of the dataset), datatextfield to customername, datavalue field customerid. so the datasource is showing in the html aspx file.

Cassio said:
Where are you setting the datasource of your dropdownlist?
 
hi just a short note to say that i solved my problem..

just for anyone to know... my SQLADATADAPTER was giving me problems to basically what i did was selected the SQLDATAADAPTER and went on the SELECT COMMAND property opened the heirarchy and build again the command text property with the build in tool...

saved my work and it worked fine...
 
hi .. another problem cropped up help..
i have got this piece of code below and its not entering in the for ... next loop for me... i cant understand why?

Visual Basic:
 Public adaptOrders As SqlDataAdapter
 Public dsorders As dsorders
 Public dscustomers As dscustomers

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
       

        'get the cached variables
        adaptOrders = Cache("adptorders")
        dsorders = Cache("dsorders")
        dscustomers = Cache("dscustomers")

        'run the first time the page is loaded
        If Not IsPostBack Then


            'for each row in the table
            Dim dscustomers As New dsCustomers
            Dim rownext As dsCustomers.CustomersRow
            For Each rownext In dscustomers.Customers
                Dim lstNew As New ListItem
                lstNew.Text = rownext.CustomerID
                lstNew.Value = rownext.CustomerID
                ' add the list item to the drop-down list
                drpCustomer.Items.Add(lstNew)
            Next
            'select the first item in the list
            drpCustomer.SelectedIndex = 0


        End If
    End Sub

what i am trying to do is bind the drpcustomer dropdownlist with the cached dataset that i have on webform1, which also has the connection to the database (i am using sql server 2000) and also the data
Adapter is on this page.. the drpcustomer dropdownlist is on webform3... would appreciate it if someone can help.. thanks in advance sonia...
 
solved t problem

ah ah ah!!! this was really frustrating i solved t problem but with a lot of playing around....

for whoever is interested... this is what i had to declare also with the code which i had posted above..

Visual Basic:
Public adaptOrders As SqlDataAdapter
Public orders As dsorders
Public  dscustomers As New dscustomers ' i kept on getting an error here reference to object not set - so just add NEW

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
       

        'get the cached variables
        adaptOrders = Cache("adptorders")
        dsorders = Cache("dsorders")
        dscustomers = Cache("dscustomers")

        'run the first time the page is loaded
        If Not IsPostBack Then


            'for each row in the table
            Dim dscustomers As New dsCustomers
 dscustomers = Cache("dscustomers") ' i also had to add the cache of my dataset here cause i was also getting instance of object not set

            Dim rownext As dsCustomers.CustomersRow
            For Each rownext In dscustomers.Customers
                Dim lstNew As New ListItem
                lstNew.Text = rownext.CustomerID
                lstNew.Value = rownext.CustomerID
                ' add the list item to the drop-down list
                drpCustomer.Items.Add(lstNew)
            Next
            'select the first item in the list
            drpCustomer.SelectedIndex = 0


        End If
    End Sub

works fine..

what i would like to know is why did i have to always and and new instance of the object if its declared in the top of class as PUBLIC... if someone could reply back to me would appreciate... i am not satisfied only that it worked.. i would love to know the explanation of it cause i spent a lot of time figuring it out and it was very very frustrating...

thanks in advance... :)
 
You are declaring the dsCustomers twice - once at the page level and once within the postback check, if the dataset is required in multiple routines in the page then declaring at the page level is okay, if this is the only routine that uses it then there is no need to declare it outside the page_load

You may want to consider checking if the cache object returns null - as your code makes no checks (hence the reason you were getting the original errors)

Also you do not appear to be either filling the dataset anywhere in that code sample or storing the dataset into the Cache object - are there other routines used in this page?
 
Back
Top