467,097 Members | 1,198 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,097 developers. It's quick & easy.

Global property

I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks
Nov 18 '05 #1
  • viewed: 2313
Share:
15 Replies
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
....
End Sub

....
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks

Nov 18 '05 #2
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyValue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew****************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the specific type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks


Nov 18 '05 #3
Yes,
Your problem statement didn't mention that.

If you need this from multiple page, the best solution is to make them
implement an interface and cast the page to said interface...if this is what
you need, lemme know.

Karl

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyValue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew****************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks



Nov 18 '05 #4
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyValue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew****************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks



Nov 18 '05 #5
Ah, but I would love to see how to do that (the interface design) if you get
a chance. I have an app that utilizes a few user controls across a couple
of webforms. They all need to share the same state. My current method is
extremely ugly. I've made all the user controls have a public property (say
MySettings). In the page load I initialize all the usercontrols MySettings
properties to match the Page's MySettings. Every time there is a change, I
resync all the usercontrols (by raising custom events registered in the
hosting Page). It is very ugly as you can imagine.

I probably should have just shoved MySettings into a Session variable, but I
was trying to keep everything in ViewState instead. (Not sure I am made the
right decision here)

It is very interesting to me to be able to read a common property from the
hosting page directly. My problem is, like I mentioned, my usercontrols are
spread across mutliple webforms and doing a Select Case on a TypeOf seems
uglier yet.

Greg
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:e4***************@tk2msftngp13.phx.gbl...
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you

try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyValue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew****************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
> I have main aspx page with a number of user controls.
> How can I create a global property that will be visible in every user > control?
>
> Thanks
>
>



Nov 18 '05 #6
You can either do it with a base page or an interface.

Public Interface ISettings
ReadOnly Property Settings() As Hashtable
End Interface

your pages:

Public Class TestPage
Inherits Page
Implements ISettings

private _settings as Hashtable
Public ReadOnly Property Settings() As Hashtable Implements
ISettings.Settings
Get
return _settings
End Get
End Property
...
End Class

Public Class TestPage2
Inherits Page
Implements ISettings

private _settings as Hashtable
Public ReadOnly Property Settings() As Hashtable Implements
ISettings.Settings
Get
return _settings
End Get
End Property
...
End Class

your user control:

if typeof (Page) is ISettings then
settings = ctype(Page, ISettings).Settings
end if
Is that what you wanted?

Karl

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
Ah, but I would love to see how to do that (the interface design) if you get a chance. I have an app that utilizes a few user controls across a couple
of webforms. They all need to share the same state. My current method is
extremely ugly. I've made all the user controls have a public property (say MySettings). In the page load I initialize all the usercontrols MySettings properties to match the Page's MySettings. Every time there is a change, I resync all the usercontrols (by raising custom events registered in the
hosting Page). It is very ugly as you can imagine.

I probably should have just shoved MySettings into a Session variable, but I was trying to keep everything in ViewState instead. (Not sure I am made the right decision here)

It is very interesting to me to be able to read a common property from the
hosting page directly. My problem is, like I mentioned, my usercontrols are spread across mutliple webforms and doing a Select Case on a TypeOf seems
uglier yet.

Greg
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:e4***************@tk2msftngp13.phx.gbl...
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if
you
try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of
test to be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyValue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew****************@TK2MSFTNGP14.phx.gbl...
> create a public property
>
> Say your page is of type WebForm1
>
> Public Class WebForm1
> inherits System.Web.UI.Page
>
>
> private _myvalue as string = "SomeValue"
> public readonly property MyValue() as string
> get
> return _myvalue
> end get
> end property
>
> Sub Page Load...
> ...
> End Sub
>
> ...
> End class
>
> In your user controls, you can access MyValue via:
>
> dim pageValue as string = ctype(Page, WebForm1).MyValue
>
>
> Every user control has access to the Page, and by casting it to the
specific
> type, you have access to it's public property/methods..
>
> Karl
>
>
> "Mark Goldin" <ma********@comcast.net> wrote in message
> news:eH****************@TK2MSFTNGP12.phx.gbl...
> > I have main aspx page with a number of user controls.
> > How can I create a global property that will be visible in every user > > control?
> >
> > Thanks
> >
> >
>
>



Nov 18 '05 #7
I believe so. Thanks!

Greg

Is that what you wanted?

Karl

Nov 18 '05 #8
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew**************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the specific type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks


Nov 18 '05 #9
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew**************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
> I have main aspx page with a number of user controls.
> How can I create a global property that will be visible in every user
> control?
>
> Thanks
>
>



Nov 18 '05 #10
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an interface (says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew**************@TK2MSFTNGP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.Page
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyValue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@comcast.net> wrote in message
news:eH****************@TK2MSFTNGP12.phx.gbl...
> I have main aspx page with a number of user controls.
> How can I create a global property that will be visible in every user
> control?
>
> Thanks
>
>



Nov 18 '05 #11
That makes two of us. :^)

Greg

"Mark Goldin" <ma********@comcast.net> wrote in message
news:OZ****************@TK2MSFTNGP14.phx.gbl...
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an

interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
> How do I reference such a property from a user control?
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in > message news:ew**************@TK2MSFTNGP14.phx.gbl...
>> create a public property
>>
>> Say your page is of type WebForm1
>>
>> Public Class WebForm1
>> inherits System.Web.UI.Page
>>
>>
>> private _myvalue as string = "SomeValue"
>> public readonly property MyValue() as string
>> get
>> return _myvalue
>> end get
>> end property
>>
>> Sub Page Load...
>> ...
>> End Sub
>>
>> ...
>> End class
>>
>> In your user controls, you can access MyValue via:
>>
>> dim pageValue as string = ctype(Page, WebForm1).MyValue
>>
>>
>> Every user control has access to the Page, and by casting it to the
> specific
>> type, you have access to it's public property/methods..
>>
>> Karl
>>
>>
>> "Mark Goldin" <ma********@comcast.net> wrote in message
>> news:eH****************@TK2MSFTNGP12.phx.gbl...
>> > I have main aspx page with a number of user controls.
>> > How can I create a global property that will be visible in every
>> > user
>> > control?
>> >
>> > Thanks
>> >
>> >
>>
>>
>
>



Nov 18 '05 #12

string pageValue = ((WebForm1)Page).MyValue

or via the interface:

string pageValue ((ISettings)Page).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OZ****************@TK2MSFTNGP14.phx.gbl...
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an

interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew**************@TK2MSFTNGP14.phx.gbl...
> create a public property
>
> Say your page is of type WebForm1
>
> Public Class WebForm1
> inherits System.Web.UI.Page
>
>
> private _myvalue as string = "SomeValue"
> public readonly property MyValue() as string
> get
> return _myvalue
> end get
> end property
>
> Sub Page Load...
> ...
> End Sub
>
> ...
> End class
>
> In your user controls, you can access MyValue via:
>
> dim pageValue as string = ctype(Page, WebForm1).MyValue
>
>
> Every user control has access to the Page, and by casting it to the
specific
> type, you have access to it's public property/methods..
>
> Karl
>
>
> "Mark Goldin" <ma********@comcast.net> wrote in message
> news:eH****************@TK2MSFTNGP12.phx.gbl...
> > I have main aspx page with a number of user controls.
> > How can I create a global property that will be visible in every user> > control?
> >
> > Thanks
> >
> >
>
>



Nov 18 '05 #13
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;

public DataSet Employee

{

get

{return _Employee;}

set

{_Employee = value;}

}

in the user control:

DataSet ds = ((humanres.main2)Page).Employee;

....

SqlHelper.FillDataset(conn, CommandType.Text, "select * from udf_getEmployeeDetails(" + EmployeeId + ")",

ds, new string[]{"employee"});

fails with the following error:



Server Error in '/humanres' Application.
--------------------------------------------------------------------------------

Value cannot be null. Parameter name: dataSet
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.ArgumentNullException: Value cannot be null. Parameter name: dataSet
Line 53: DataSet ds = ((humanres.main2)Page).Employee;
.....

Line 55: SqlHelper.FillDataset(conn, CommandType.Text, "select * from udf_getEmployeeDetails(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"});


"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:eV**************@TK2MSFTNGP11.phx.gbl...

string pageValue = ((WebForm1)Page).MyValue

or via the interface:

string pageValue ((ISettings)Page).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Mark Goldin" <ma********@comcast.net> wrote in message
news:OZ****************@TK2MSFTNGP14.phx.gbl...
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an

interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
> How do I reference such a property from a user control?
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote

in
> message news:ew**************@TK2MSFTNGP14.phx.gbl...
>> create a public property
>>
>> Say your page is of type WebForm1
>>
>> Public Class WebForm1
>> inherits System.Web.UI.Page
>>
>>
>> private _myvalue as string = "SomeValue"
>> public readonly property MyValue() as string
>> get
>> return _myvalue
>> end get
>> end property
>>
>> Sub Page Load...
>> ...
>> End Sub
>>
>> ...
>> End class
>>
>> In your user controls, you can access MyValue via:
>>
>> dim pageValue as string = ctype(Page, WebForm1).MyValue
>>
>>
>> Every user control has access to the Page, and by casting it to the
> specific
>> type, you have access to it's public property/methods..
>>
>> Karl
>>
>>
>> "Mark Goldin" <ma********@comcast.net> wrote in message
>> news:eH****************@TK2MSFTNGP12.phx.gbl...
>> > I have main aspx page with a number of user controls.
>> > How can I create a global property that will be visible in every user >> > control?
>> >
>> > Thanks
>> >
>> >
>>
>>
>
>



Nov 18 '05 #14
Mark,
Your dataset is null...you never do _Employee = new DataSet()...The simplest
way to solve your problem:

private DataSet _Employee = new DataSet(); //instead of = null;

The reason no on suggested this is that declaring a dataset in a page and
populating it in a user-control isn't obvious (at best) and suspect (at
worst). I would consider examining if that's a sound architecture. Since
I'm not sure what you are trying to do, short of your initial post asking
how to make a variable visible to all user controls, I can't offer any
suggestions.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;
public DataSet Employee
{
get
{return _Employee;}
set
{_Employee = value;}
}
in the user control:
DataSet ds = ((humanres.main2)Page).Employee;
....
SqlHelper.FillDataset(conn, CommandType.Text, "select * from
udf_getEmployeeDetails(" + EmployeeId + ")",
ds, new string[]{"employee"});
fails with the following error:
Server Error in '/humanres' Application.
Value cannot be null. Parameter name: dataSet
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.ArgumentNullException: Value cannot be null.
Parameter name: dataSet

Line 53: DataSet ds = ((humanres.main2)Page).Employee;
.....
Line 55: SqlHelper.FillDataset(conn, CommandType.Text, "select * from
udf_getEmployeeDetails(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"});
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eV**************@TK2MSFTNGP11.phx.gbl...

string pageValue = ((WebForm1)Page).MyValue

or via the interface:

string pageValue ((ISettings)Page).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OZ****************@TK2MSFTNGP14.phx.gbl...
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an

interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier. (I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
> How do I reference such a property from a user control?
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote
in
> message news:ew**************@TK2MSFTNGP14.phx.gbl...
>> create a public property
>>
>> Say your page is of type WebForm1
>>
>> Public Class WebForm1
>> inherits System.Web.UI.Page
>>
>>
>> private _myvalue as string = "SomeValue"
>> public readonly property MyValue() as string
>> get
>> return _myvalue
>> end get
>> end property
>>
>> Sub Page Load...
>> ...
>> End Sub
>>
>> ...
>> End class
>>
>> In your user controls, you can access MyValue via:
>>
>> dim pageValue as string = ctype(Page, WebForm1).MyValue
>>
>>
>> Every user control has access to the Page, and by casting it to the
> specific
>> type, you have access to it's public property/methods..
>>
>> Karl
>>
>>
>> "Mark Goldin" <ma********@comcast.net> wrote in message
>> news:eH****************@TK2MSFTNGP12.phx.gbl...
>> > I have main aspx page with a number of user controls.
>> > How can I create a global property that will be visible in every

user >> > control?
>> >
>> > Thanks
>> >
>> >
>>
>>
>
>



Nov 18 '05 #15
Your ds that your are passing to SqlHelper.Fill is still null. It has never been istantiated.

The VB equivalent is you have this: (not sure have to express this in c#)

Dim conn As New SqlClient.SqlConnection(ConfigurationSettings.AppS ettings("ConnectionString"))
Dim ds As DataSet
SqlHelper.FillDataset(conn, CommandType.Text, "select * from users where empid=1", ds, New String() {"employee"})

You need to have this:
Dim conn As New SqlClient.SqlConnection(ConfigurationSettings.AppS ettings("ConnectionString"))
Dim ds As NEW DataSet ' <---- gotta istantiate it first!!!
SqlHelper.FillDataset(conn, CommandType.Text, "select * from users where empid=1", ds, New String() {"employee"})
The reason your dataset is null, is becuase you assign it to the Employee ds property of your webform. And your not showing any code where you istantiate it anywhere.

These little bits and pieces of code without context are not helping me. (Maybe somebody else can help out here... please!)

Greg

"Mark Goldin" <ma********@comcast.net> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;

public DataSet Employee

{

get

{return _Employee;}

set

{_Employee = value;}

}

in the user control:

DataSet ds = ((humanres.main2)Page).Employee;

...

SqlHelper.FillDataset(conn, CommandType.Text, "select * from udf_getEmployeeDetails(" + EmployeeId + ")",

ds, new string[]{"employee"});

fails with the following error:
Server Error in '/humanres' Application.
------------------------------------------------------------------------------

Value cannot be null. Parameter name: dataSet
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.ArgumentNullException: Value cannot be null. Parameter name: dataSet
Line 53: DataSet ds = ((humanres.main2)Page).Employee;
....

Line 55: SqlHelper.FillDataset(conn, CommandType.Text, "select * from udf_getEmployeeDetails(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"});


"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:eV**************@TK2MSFTNGP11.phx.gbl...

string pageValue = ((WebForm1)Page).MyValue

or via the interface:

string pageValue ((ISettings)Page).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Mark Goldin" <ma********@comcast.net> wrote in message
news:OZ****************@TK2MSFTNGP14.phx.gbl...
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyValue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an

interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyValue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@comcast.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
> How do I reference such a property from a user control?
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote

in
> message news:ew**************@TK2MSFTNGP14.phx.gbl...
>> create a public property
>>
>> Say your page is of type WebForm1
>>
>> Public Class WebForm1
>> inherits System.Web.UI.Page
>>
>>
>> private _myvalue as string = "SomeValue"
>> public readonly property MyValue() as string
>> get
>> return _myvalue
>> end get
>> end property
>>
>> Sub Page Load...
>> ...
>> End Sub
>>
>> ...
>> End class
>>
>> In your user controls, you can access MyValue via:
>>
>> dim pageValue as string = ctype(Page, WebForm1).MyValue
>>
>>
>> Every user control has access to the Page, and by casting it to the
> specific
>> type, you have access to it's public property/methods..
>>
>> Karl
>>
>>
>> "Mark Goldin" <ma********@comcast.net> wrote in message
>> news:eH****************@TK2MSFTNGP12.phx.gbl...
>> > I have main aspx page with a number of user controls.
>> > How can I create a global property that will be visible in every user >> > control?
>> >
>> > Thanks
>> >
>> >
>>
>>
>
>



Nov 18 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Steve | last post: by
16 posts views Thread by Roman Ziak | last post: by
23 posts views Thread by David Colliver | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.