Object reference error with cookie

D

David C

I am trying to default the SelectedValue of a DropDownList control to a
value stored in a cookie. I am getting the following error:

"Object reference not set to an instance of an object."

The error is occurring in the code behind page on the line below for the
ddl.SelectedValue = ....

If Not Request.Cookies("mystaffid") Is Nothing Then
'default task FROM to value in cookie (current user)
ddl = row.FindControl("ddlEnteredBy")
ddl.SelectedValue = Request.Cookies("mystaffid").ToString
End If

Thanks.
David
 
G

Gregory A. Beamer

I am trying to default the SelectedValue of a DropDownList control to
a value stored in a cookie. I am getting the following error:

"Object reference not set to an instance of an object."

The error is occurring in the code behind page on the line below for
the ddl.SelectedValue = ....

If Not Request.Cookies("mystaffid") Is Nothing Then
'default task FROM to value in cookie (current user)
ddl = row.FindControl("ddlEnteredBy")
ddl.SelectedValue =
Request.Cookies("mystaffid").ToString
End If

While this is not a solution, the first thing I would do is separate the
Cookie Request from the selected value set. The reason is this will
eliminate any problem with the drop down list.

But, you might say, there should be no problem with the dropdownlist. I
accept that, but since I don't see your definition

Dim ddl as DropDownList

you could be implicitly casting as a control, which will not have a
selected value and can cause issues. Just eliminate issues.

The other side of the coin is pull the cookie first (this is pseudocode,
so you have to get the correct object names):

Dim cook as Cookie = Request.Cookies("mystaffid")

If Not cook is Nothing Then
If(cook.HasKeys) Then
End If
End If

This is a safer way of approaching. You can still ToString() the cookie,
if that works for you, but I would also do that like this:

Dim staffId as String = cook.ToString()

These are just some ideas to troubleshoot and leave the code where you
can more easily break it (debugging sense of break) and check values.
When you combine statements, it makes it harder to debug.

Peace and Grace,
 
D

David C

Gregory A. Beamer said:
While this is not a solution, the first thing I would do is separate the
Cookie Request from the selected value set. The reason is this will
eliminate any problem with the drop down list.

But, you might say, there should be no problem with the dropdownlist. I
accept that, but since I don't see your definition

Dim ddl as DropDownList

you could be implicitly casting as a control, which will not have a
selected value and can cause issues. Just eliminate issues.

The other side of the coin is pull the cookie first (this is pseudocode,
so you have to get the correct object names):

Dim cook as Cookie = Request.Cookies("mystaffid")

If Not cook is Nothing Then
If(cook.HasKeys) Then
End If
End If

This is a safer way of approaching. You can still ToString() the cookie,
if that works for you, but I would also do that like this:

Dim staffId as String = cook.ToString()

These are just some ideas to troubleshoot and leave the code where you
can more easily break it (debugging sense of break) and check values.
When you combine statements, it makes it harder to debug.

Peace and Grace,

Above the code I sent is

Dim ddl As DropDownList

because I use the variable again later. I expected that since I already was
passing the test for

If Not Request.Cookies("mystaffid") Is Nothing Then

that I was reading the cookie value OK. I use that cookie value in other
places so I know that it has a value, I wasn't sure if the way I was
referring to it in the code behind was wrong.

David
 
G

Gregory A. Beamer

Above the code I sent is

Dim ddl As DropDownList

because I use the variable again later. I expected that since I
already was passing the test for

If Not Request.Cookies("mystaffid") Is Nothing Then

that I was reading the cookie value OK. I use that cookie value in
other places so I know that it has a value, I wasn't sure if the way I
was referring to it in the code behind was wrong.


If the cookie is getting value that you are using elsewhere, then the
ddl is the most likely issue:

ddl = row.FindControl("ddlEnteredBy")

If the control is not found, this will be null and throw the same error.

Peace and Grace,
 
D

David C

Gregory A. Beamer said:
If the cookie is getting value that you are using elsewhere, then the
ddl is the most likely issue:

ddl = row.FindControl("ddlEnteredBy")

If the control is not found, this will be null and throw the same error.

Peace and Grace,

That was it...a misspelled control. However, now the SelectedValue is not
showing on the new blank Insert record. I put the Page_Load sub below to
see if you can find any reason why the ddl.SelectedValue is not showing up.
Thanks.
David

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Request.QueryString("tid") Is Nothing Then
If Request.QueryString("tid") = "0" Then
'Job sent has no open tasks so set to insert mode
fvTask.ChangeMode(FormViewMode.Insert)
Dim row As FormViewRow = fvTask.Row
Dim ddl As DropDownList
Dim tx As TextBox = row.FindControl("txtToDoDate")
tx.Text = DateTime.Today.ToShortDateString
If Not Request.Cookies("mystaffid") Is Nothing Then
Dim strStaffID As String =
Request.Cookies("mystaffid").Value
'txtMsg.Text = "strStaffID = " & strStaffID
'default task FROM to value in cookie (current user)
ddl = row.FindControl("ddlEnteredBy")
ddl.SelectedValue = strStaffID
End If
'Default priority to High
ddl = row.FindControl("ddlPriority")
ddl.SelectedValue = "0"
fvTask.DataBind()
Exit Sub
End If
End If

End Sub
 
D

David C

Gregory A. Beamer said:
If the cookie is getting value that you are using elsewhere, then the
ddl is the most likely issue:

ddl = row.FindControl("ddlEnteredBy")

If the control is not found, this will be null and throw the same error.

Peace and Grace,

Nevermind, I put it in the RowDatabound event and it worked fine. It did not
work in Page_Load.

David
 
M

Mr. Arnold

David said:
I am trying to default the SelectedValue of a DropDownList control to a
value stored in a cookie. I am getting the following error:

"Object reference not set to an instance of an object."

The error is occurring in the code behind page on the line below for the
ddl.SelectedValue = ....

If Not Request.Cookies("mystaffid") Is Nothing Then
'default task FROM to value in cookie (current user)
ddl = row.FindControl("ddlEnteredBy")
ddl.SelectedValue = Request.Cookies("mystaffid").ToString
End If

Thanks.
David

If the data is 'null' Request.Cookies("mystaffid").ToString, then a
'null' cannot be cast to an object. Everything in .NET is an object.
String is an object.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top