Showing posts with label formview1. Show all posts
Showing posts with label formview1. Show all posts

Monday, March 26, 2012

Object reference not set to an instance of an object

Please Help

I have the following code ..

Dim

getDateCMP231As Label

getDateETH325 =

CType(FormView1.FindControl("ETH325DateLabel.text"), Label)
txtETH235.Text = getDateETH325.Text.ToString

Which throws out the following error below. I cannot seem to find the problem.

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 80: Line 81: getDateETH325 = CType(FormView1.FindControl("ETH325DateLabel.text"), Label)Line 82: txtETH235.Text = getDateETH325.Text.ToStringLine 83: Line 84: getDateETH212 = CType(FormView1.FindControl("ETH212DateLabel"), Label)

I hate these errors -- got a lot of experience with them and thankfully, I think yours is easy...change line 81 to

.FindControl("ETH325DateLabel"), Label)

(dump the .text)


She still no work.

The thing is I have the same type of code on another page (below) and that works, no problem. I am pulling the little hair that i already have trying to sort this out.

Any idea's

Dim

getFNameAs Label

getFName =

CType(FormView1.FindControl("FirstNameLabel"), Label)

firstName.Text = getFName.Text


Try this ...see if you get a different result.

Private departmentAs String =CType(FormView1.FindControl("Label1"), TextBox).Text

There is an error message on 'Private' saying '"Private" is not valid on a local variable declaration '


One thing to add to your code is a try/catch block: it could be that the FindControl does return null (if the control is not found). Therefore, I suggest to put these 2 lines in a try catch block.

HTH

I may be being dumb, but you seem to have several variables used in this bit of code and I think you may be getting confused between them.

You declare "getDateCMP231" as a label, butdon't appear to use it!

You do the assignment "getDateETH325 =CType(FormView1.FindControl("ETH325DateLabel.text"), Label)" but what is "getDateETH325" where is it declared? Is this supposed to be "getDateCMP231"?

Then you assign the text in the label control "ETH325DateLabel" to "txtETH235.text" which I assume is a control on your page.

Try looking carefully at the naming of the controls/variables and see if you are getting mixed up!



Thanks, I managed to sort it out, but i don't know why it works.

I moved the variable declaration (below), just below the "Inherits System.Web.UI.Page" and it works. I tried to move it back to the sub procedure and again i get the error.Hmm Thanks again

Dim getDateETH325As Label


Thanks, I managed to sort it out, but i don't know why it works.

I moved the variable declaration (below), just below the "Inherits System.Web.UI.Page" and it works. I tried to move it back to the sub procedure and again i get the error.Hmm Thanks again

Dim getDateETH325As Label

Saturday, March 24, 2012

Object reference not set to an instance of an object

Hi,

I want to access in code-behind a label within the ItemTemplate of a
Formview.

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>

code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text

I get the error: "Object reference not set to an instance of an object"

Could somebody tell me what's wrong in my code?
Thanks
BenTry

FormView1.Row.FindControl(...)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Ben" <b@.bnwrote in message
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Hi,
>
I want to access in code-behind a label within the ItemTemplate of a
Formview.
>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
>
I get the error: "Object reference not set to an instance of an object"
>
Could somebody tell me what's wrong in my code?
Thanks
Ben
>
>
>


On Jun 26, 2:06 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.orgwrote:

Quote:

Originally Posted by

Try
>
FormView1.Row.FindControl(...)
>
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
>
"Ben" <b@.bnwrote in message
>
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
>

Quote:

Originally Posted by

Hi,


>

Quote:

Originally Posted by

I want to access in code-behind a label within the ItemTemplate of a
Formview.


>

Quote:

Originally Posted by

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>


>

Quote:

Originally Posted by

code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text


>

Quote:

Originally Posted by

I get the error: "Object reference not set to an instance of an object"


>

Quote:

Originally Posted by

Could somebody tell me what's wrong in my code?
Thanks
Ben


hi
,

if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}

thanks
Masudur
As a general rule, it's a good idea to break up the statements in a single
line multi-expression code line into their own lines of code to facilitate
setting breakpoints and being able to examine values. The way you have it
now, it would be extremely difficult to tell what is going on.

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Ben" wrote:

Quote:

Originally Posted by

Hi,
>
I want to access in code-behind a label within the ItemTemplate of a
Formview.
>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
>
I get the error: "Object reference not set to an instance of an object"
>
Could somebody tell me what's wrong in my code?
Thanks
Ben
>
>
>
>

Object reference not set to an instance of an object

Hi,
I want to access in code-behind a label within the ItemTemplate of a
Formview.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
I get the error: "Object reference not set to an instance of an object"
Could somebody tell me what's wrong in my code?
Thanks
BenTry
FormView1.Row.FindControl(...)
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Ben" <b@.bn> wrote in message
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
> Hi,
> I want to access in code-behind a label within the ItemTemplate of a
> Formview.
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
> DataSourceID="SqlDataSource1" >
> <ItemTemplate>
> <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
> %>'></asp:Label>
> code-behind:
> Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
> I get the error: "Object reference not set to an instance of an object"
> Could somebody tell me what's wrong in my code?
> Thanks
> Ben
>
>
On Jun 26, 2:06 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.org> wrote:
> Try
> FormView1.Row.FindControl(...)
> --
> Eliyahu Goldin,
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
> "Ben" <b@.bn> wrote in message
> news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
>
>
>
>
>
>
hi
,
if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}
thanks
Masudur
As a general rule, it's a good idea to break up the statements in a single
line multi-expression code line into their own lines of code to facilitate
setting breakpoints and being able to examine values. The way you have it
now, it would be extremely difficult to tell what is going on.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Ben" wrote:

> Hi,
> I want to access in code-behind a label within the ItemTemplate of a
> Formview.
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
> DataSourceID="SqlDataSource1" >
> <ItemTemplate>
> <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
> %>'></asp:Label>
> code-behind:
> Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
> I get the error: "Object reference not set to an instance of an object"
> Could somebody tell me what's wrong in my code?
> Thanks
> Ben
>
>

Object reference not set to an instance of an object

Hi,
I want to access in code-behind a label within the ItemTemplate of a
Formview.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
I get the error: "Object reference not set to an instance of an object"
Could somebody tell me what's wrong in my code?
Thanks
BenHi Ben,
The direct answer is: nothing wrong.
It's probably an issue outside given code. Checks FormView1.Row and FormView
1.DataItem
are not null. If yes then be sure you have at least one record in FormView1'
s
source and DataBind is performed on the FormView1. You cannot retrieve contr
ols
if they are not processed.
Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
B> FormView1.FindControl("nameLabel")
B>