Object reference not set to an instance of an object.

L

Laura K

I have been trying to figure this one out. I really need help. I have
a relationship between two tables - product and colors. I have a
repeater listing the products and then each product has a drop down
with the corresponding colors. However when I try to view the page I
get:

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 70:
Line 71:
Line 72: DropDownList1.DataSource =
dsGetProducts.Tables("colors")
Line 73: DropDownList1.DataBind()
Line 74:


Here is the code The error is near the bottom of the code. It is
basically stating that the dropdown datasource is not set to an
instance of an object. HELP!

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strProductCode = Request.QueryString("ProductCode")

'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_getDetailsTables",
connection)
command.CommandType = CommandType.StoredProcedure


' Add an input parameter and supply a value for it
command.Parameters.Add("@strProductCode", SqlDbType.VarChar,
50)
command.Parameters("@strProductCode").Value = strProductCode


Dim mySqlDataAdapter As SqlDataAdapter = New
SqlDataAdapter(command)

Dim dsGetProducts As New DataSet
mySqlDataAdapter.Fill(dsGetProducts, "Products")

dsGetProducts.Tables(0).TableName = "Products"
dsGetProducts.Tables(1).TableName = "Colors"


dsGetProducts.Relations.Add("ProductsToColors",
dsGetProducts.Tables("products").Columns("intProductID"),
dsGetProducts.Tables("colors").Columns("intProductID"), False)


DropDownList1.DataSource = dsGetProducts.Tables("colors")
DropDownList1.DataBind()


Repeater1.DataSource = dsGetProducts.Tables("products")
Repeater1.DataBind()

End Sub


Any help is appreciated.

Laura K
 
L

Laura K

After reading some replies to this error I experimented by adding

Dim dropdownlist1 As New DropDownList

Now I get the page and the drop downs but they are empty.
 
L

Laura K

Actually I had all that set up. I tried it many different ways. I did
received an successful answer on another board so I will mention it
here if anyone needs to use it. It seems that it is not enough to
declare the relationship and then bind the specific table data to each
control. I ended up creating a function which I used when creating the
dropdown menu. The function actually matched the specific id's
together and then was called from dropdown menu. It looked something
like this <asp:dropwdown DataSource='<%# Function( Container.DataItem,
"relationship" ) %>' etc etc

Thanks for the reply however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top