Object reference not set to an instance of an object

K

ken lee

the error is "Object reference not set to an instance of an object".
the code are like this.

please help.....
======================
Public Sub ddItems_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)

Dim itemType As ListItemType = e.Item.ItemType

If (itemType = ListItemType.EditItem) Then

Dim dataset1 As New DataSet

Dim dataset2 As New DataSet

Dim myListType As DropDownList = CType(e.Item.FindControl("ddType"),
DropDownList)

Dim myRowType As DataRowView = CType(e.Item.DataItem, DataRowView)

Dim myType As String = myRowType("OrderItems_TypeName").ToString()

Dim myListType2 As DropDownList = CType(e.Item.FindControl("ddCategory"),
DropDownList)

Dim myRowType2 As DataRowView = CType(e.Item.DataItem, DataRowView)

Dim myType2 As String = myRowType2("Category_Description").ToString()



dataset1.Tables.Add(New DataTable("Type"))

dataset1.Tables("Type").Columns.Add("Type_ID",
System.Type.GetType("System.Int32"))

dataset1.Tables("Type").Columns.Add("Type_Name",
System.Type.GetType("System.String"))

dataset2.Tables.Add(New DataTable("Category"))

dataset2.Tables("Category").Columns.Add("Category_ID",
System.Type.GetType("System.Int32"))

dataset2.Tables("Category").Columns.Add("Category_Name",
System.Type.GetType("System.String"))

Dim TypeRow As DataRow

Dim CategoryRow As DataRow

If Request.QueryString("id") = 1 Then

TypeRow = dataset1.Tables("Type").NewRow()

TypeRow("Type_ID") = 1

TypeRow("Type_Name") = "Hardware"

dataset1.Tables("Type").Rows.Add(TypeRow)

TypeRow = dataset1.Tables("Type").NewRow()

TypeRow("Type_ID") = 2

TypeRow("Type_Name") = "Software"

dataset1.Tables("Type").Rows.Add(TypeRow)

ElseIf Request.QueryString("id") = 3 Then

TypeRow = dataset1.Tables("Type").NewRow()

TypeRow("Type_ID") = 3

TypeRow("Type_Name") = "Office Supplies"

dataset1.Tables("Type").Rows.Add(TypeRow)

CategoryRow = dataset2.Tables("Category").NewRow()

CategoryRow("Category_ID") = 1

CategoryRow("Category_Name") = "Blank"

dataset2.Tables("Category").Rows.Add(CategoryRow)

CategoryRow = dataset2.Tables("Category").NewRow()

CategoryRow("Category_ID") = 2

CategoryRow("Category_Name") = "Comsumable"

dataset2.Tables("Category").Rows.Add(CategoryRow)

CategoryRow = dataset2.Tables("Category").NewRow()

CategoryRow("Category_ID") = 3

CategoryRow("Category_Name") = "NonComsumable"

dataset2.Tables("Category").Rows.Add(CategoryRow)

CategoryRow = dataset2.Tables("Category").NewRow()

CategoryRow("Category_ID") = 4

CategoryRow("Category_Name") = "MaintainKit"

dataset2.Tables("Category").Rows.Add(CategoryRow)







End If

myListType.DataTextField = "Type_Name"

myListType.DataValueField = "Type_ID"

myListType.DataSource = dataset1.Tables("Type")

myListType.DataBind()

myListType2.DataTextField = "Category_Name"

myListType2.DataValueField = "Category_ID"

myListType2.DataSource = dataset2.Tables("Category")

myListType2.DataBind()

If Request.QueryString("id") = 1 Then

myListType.Items.FindByText(myType).Selected = True

Else

'this code made problem....

myListType2.Items.FindByText(myType2).Selected = True



End If

End If

End Sub



=================================================
 
M

Marina

This is a long code segment. It would be nice if you told us which line is
causing the problem, it could be any of these.

It all boils down to you trying to use a variable or a method return value
which happens to be null. But you assume that it will not be, and then you
get the exception.
 

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

Top