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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Server Error Meaning

Status
Not open for further replies.

oddball

Technical User
Mar 17, 2000
64
GB
Get this error when setting the value in a row being added to a datatable. Is only caused by the listbox control 'ProductDefinitionList', not the dropdown list control???????

==========================================================

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:


Line 56: ' Create a new row in the DataTable and fill with values from page controls
Line 57: objDataRow = myDataTable.NewRow()
Line 58: objDataRow("ProductDefinitionID") = ProductDefinitionList.SelectedItem.Value
Line 59: objDataRow("CategoryID") = CategoryList.SelectedItem.Value
Line 60: objDataRow("Description") = "test3"


Source File: C:\C2K\WAF\PortalVB\admin\register2.aspx Line: 58

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ASP.Register2_aspx.AddBtn_Click(Object sender, EventArgs E) in C:\C2K\WAF\PortalVB\admin\register2.aspx:58
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263

=========================================================

thanks guys,

si
 
Line 56: ' Create a new row in the DataTable and fill with values from page controls
Line 57: DataRow objDataRow = myDataTable.NewRow()
Line 58: objDataRow("ProductDefinitionID") = ProductDefinitionList.SelectedItem.Value
Line 59: objDataRow("CategoryID") = CategoryList.SelectedItem.Value
Line 60: objDataRow("Description") = "test3"
 
Cheers, but I now get this:

==========================================================

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30684: 'DataRow' is a type and cannot be used as an expression.

Source Error:

Line 55:
Line 56: ' Create a new row in the DataTable and fill with values from page controls
Line 57: DataRow objDataRow = myDataTable.NewRow()
Line 58: objDataRow("ProductDefinitionID") = ProductDefinitionList.SelectedItem.Value
Line 59: objDataRow("CategoryID") = CategoryList.SelectedItem.Value


Source File: C:\C2K\WAF\PortalVB\admin\register2.aspx Line: 57

==========================================================

Got the original syntax from Wrox Professional ASP.Net 1.0, so it should be ok. Could it be something to do with the listbox control refered to on line 58. If i replace:

= ProductDefinitionList.SelectedItem.Value

with:

= "2"

it works ok. Thanks for any further help,

si
 
does
= ProductDefinitionList.SelectedItem.Value.ToString()

work??
 
The problem is not the adding row, it is that your datatable has not been set.

dim dataset1 as new dataset()
dataset1.readxml(somefile) ' populate the datafile
dim mydatatable as dataset1.tables(0)
dim objDataRow = mydatatable.newrow()
...
myDataTable.Rows.Add(objdatarow)
Chris says: "It's time for a beer."
 
I've set the datatable up already in the page load subroutine. Was only listing the code the error returned - heres the full subroutine listings:

===========================================================

<script runat=&quot;server&quot;>

Dim myDataTable As DataTable

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

Email.Text = Request.QueryString(&quot;email&quot;)

Dim products As New ASPNetPortal.ProductsDB()
Dim myDataSet = products.GetAllProductDefinitions()
Dim myDataView1 As New DataView(myDataSet.Tables(&quot;tblProductDefinitions&quot;))
Dim myDataView2 As New DataView
Dim strCategoryID = CategoryList.SelectedItem.Value

' Filter the DataView for only rows of the selected Category
myDataView1.RowFilter = &quot;CategoryID = &quot; & strCategoryID

' Specify the DataView as the Data Source of the ProductList ListBox
' and Bind it
ProductDefinitionList.DataSource = myDataView1
ProductDefinitionList.DataValueField = &quot;ProductDefinitionID&quot;
ProductDefinitionList.DataTextField = &quot;Description&quot;
ProductDefinitionList.DataBind()

' Create a new DataTable in the existing DataSet for the Companies
' Selected Products
myDataTable = myDataSet.Tables.Add(&quot;tblCompanyProducts&quot;)
myDataTable.Columns.Add(&quot;ProductDefinitionID&quot;, System.Type.GetType(&quot;System.Int32&quot;))
myDataTable.Columns.Add(&quot;CategoryID&quot;, System.Type.GetType(&quot;System.Int32&quot;))
myDataTable.Columns.Add(&quot;Description&quot;, System.Type.GetType(&quot;System.String&quot;))
myDataView2 = myDataTable.DefaultView

End Sub

Sub AddBtn_Click(ByVal sender As Object, ByVal E As EventArgs)

Dim objDataRow As DataRow

' Create a new row in the DataTable and fill with values from page controls
DataRow objDataRow = myDataTable.NewRow()
objDataRow(&quot;ProductDefinitionID&quot;) = ProductDefinitionList.SelectedItem.Value
objDataRow(&quot;CategoryID&quot;) = CategoryList.SelectedItem.Value
objDataRow(&quot;Description&quot;) = &quot;test3&quot;
myDataTable.Rows.Add(objDataRow)


' Specify the new DataTable as the Data Source of the CompanyProducts
' ListBox and Bind it
CompanyProducts.DataSource = myDataTable
CompanyProducts.DataValueField = &quot;ProductDefinitionID&quot;
CompanyProducts.DataTextField = &quot;Description&quot;
CompanyProducts.DataBind()


Test.Text = CategoryList.SelectedItem.Value

End Sub


Sub RegisterBtn_Click(ByVal sender As Object, ByVal E As EventArgs)

' Set the user's authentication name to the userId
FormsAuthentication.SetAuthCookie(Email.Text, False)

' Redirect browser back to home page'
Response.Redirect(&quot;~/DesktopDefault.aspx&quot;)

End Sub

</script>

===========================================================

cheers,

si
 
Aha - you need to enclose everything in your Page_Load sub with the following:

If Not IsPostBack Then

...

End If

That should do it Chris says: &quot;It's time for a beer.&quot;
 
Same as before :(

===========================================================

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:


Line 59:
Line 60: ' Create a new row in the DataTable and fill with values from page controls
Line 61: objDataRow = myDataTable.NewRow()
Line 62: objDataRow(&quot;ProductDefinitionID&quot;) = ProductDefinitionList.SelectedItem.Value
Line 63: objDataRow(&quot;CategoryID&quot;) = CategoryList.SelectedItem.Value


Source File: C:\C2K\WAF\PortalVB\admin\register2.aspx Line: 61

===========================================================

What did that line mean?

keep the advice coming guys ;)
 
It means that the object you are referencing (myDataTable) is NULL. Hence, when you do: myDataTable.NewRow() what is really happenning is: NULL.NewRow() which throws a Null Reference exception. I would look at the code that instantiates the variable myDataTable to make sure that it is working as you expect. Also, just before the line throwing the exception, you could try:

If myDataTable = Nothing Then
' Show some error message here
End If


Or better yet, if you are using VS.NET, set a breakpoint at the point where myDataTable is being instantiated and then run, when it breaks, check the values... Chris says: &quot;It's time for a beer.&quot;
 
Just started using VS.Net so will try the break point.

But can you explain why it works when i substitute the reference to the listbox selected value for an integer as below:

===========================================================

' Create a new row in the DataTable and fill with values from page controls
objDataRow = myDataTable.NewRow()
objDataRow(&quot;ProductDefinitionID&quot;) = &quot;5&quot; <--
objDataRow(&quot;CategoryID&quot;) = CategoryList.SelectedItem.Value
objDataRow(&quot;Description&quot;) = &quot;test5&quot;
myDataTable.Rows.Add(objDataRow)

===========================================================

Will get back to you when i've tried the break point,

cheers,

si
 
OOps, I just looked at your error message again, and found that the error was on line 58 - my fault. The problem I think is that you have no Value attribute set for your listbox. You should use
objDataRow(&quot;ProductDefinitionID&quot;) = ProductDefinitionList.SelectedItem.Text.ToString()

instead, to choose the selection. Sorry about that. The above code should work fine, assuming you actually make a selection.
Chris says: &quot;It's time for a beer.&quot;
 
Could be right. Have just replaced with two dummy dropdownlists:

===========================================================

' Create a new row in the DataTable and fill with values from page controls
objDataRow = myDataTable.NewRow()
objDataRow(&quot;ProductDefinitionID&quot;) = TestNumList.SelectedItem.Value
objDataRow(&quot;CategoryID&quot;) = CategoryList.SelectedItem.Value
objDataRow(&quot;Description&quot;) = TestTextList.SelectedItem.Value
myDataTable.Rows.Add(objDataRow)

===========================================================

It worked with no errors. Still doesn't work entirely right, but thats a different problem in my code.
 
Still Can't get it to work!

Using the following code caused the same 'Object reference not set to an instance of an object' error:

==========================================================

objDataRow(&quot;ProductDefinitionID&quot;) = ProductDefinitionList.SelectedItem.Text.ToString()

==========================================================

It has to be a problem with the code with reference to the listbox control, because when i swap to a dropdownlist control, i no longer get the error.

Should there be syntax changes between the two controls when referencing the value & text of the selected item?

cheers,

si
 
Think i know what the problem is. The control doesn't have a selected option even when an ListItem is clicked upon, which could be seen by viewing the source of the resulting HTML. It just so happens that the dropdown list defaults to the first ListItem as its selected value, whilst the ListBox doesn't do that, creating the null value error.

Why am i not getting a selected value for my control?

help please!

si
 
Hy guys ive had this probles since a long long time.
There is some roundabout way to get the text of the selected item , I dunno how.
Coz i tried stepping thru my code , but i always get a &quot;-1&quot; for the Selected Index.
Have tried to search a lot for it , but cant find any solution .PLease let me know if there is a way to get to the actual item in the listbox.
I am ppulating my listbox from a database
 
try this

Dim objDataRow as DataRow
Set objDataRow = myDataTable.NewRow()
Daren J. Lahey
Just another computer guy...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top