Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist - not set to an instance of an object.

Status
Not open for further replies.

wallm

Programmer
Apr 4, 2005
69
GB
Hello

I have a dropdownlist that populates it's value with a sqldatasource from a field with datatype MONEY.

When I get the selectedvalue I'm using the code below but get the error below

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

Dim myShippingCost As DropDownList = CType(Wizard1.FindControl("DropDownlist1"), DropDownList)

Dim myShippingCosts As Integer = CType(myShippingCost.SelectedValue.ToString, Integer)

any help would be welcome.
 
Try with...
Code:
Dim myShippingCost As [b]New[/b] DropDownList = CType(Wizard1.FindControl("DropDownlist1"), DropDownList)
The basic cause of this type of error is:
You are trying to use a reference variable who's value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap. You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.
1. You are trying to access a string that has not been initialized. C# won't compile, VB will compile but throw runtime error.
2. You never created a new instance of an object. Same as a string, any reference type must be initialized. Strings and some other CTS types have a misconception of being value types, like Integers, and they are not. C# won't compile, VB will compile but throw runtime error.
3. You created the object, but killed it too soon.

Sharing the best from my side...

--Prashant--
 
Hi,
The error actually appears to be in the second line


Dim myShippingCosts As Integer = CType(myShippingCost.SelectedValue.ToString, Integer)
 
Try:
Code:
Dim myShippingCosts As Integer = CType(myShippingCost.SelectedItem.Value, Integer)
If that doesn't work, there isn't a SelectedItem.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top