Dont understand why this does not work

M

Mark Goldin

Very simplified:

namespace humanres

{

public class main2 : System.Web.UI.Page

{

public DataSet Employee

{

get

{

return Employee;

}

set

{

Employee = value;

}

........



Then to access Employee from the user control:

humanres.main2.Employee but it's (Employee) not there.

I am very confused.



Can some please, please explain what am I missing?
 
M

Mark Fitzpatrick

You need a place to store the DataSet object. Try the following:

namepace humanres
{
public class main2 : System.Web.UI.Page
{
private DataSet _Employee = null;

public DataSet Employee
{
get
{return _Employee;}
set
{_Employee = value;}
}
}
}

That way the DataSet is getting stored somewhere in the object. Remember, a property is an accessor for a variable that enables setting the variable, and getting the variable. You need a seperate variable somewhere to actually store the object. In the sample code I just added an underscore to the name, but it could be anything as the name is not important, just that the get reuturns an actual variable and the set is able to set some variable.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage

Very simplified:

namespace humanres

{

public class main2 : System.Web.UI.Page

{

public DataSet Employee

{

get

{

return Employee;

}

set

{

Employee = value;

}

.......



Then to access Employee from the user control:

humanres.main2.Employee but it's (Employee) not there.

I am very confused.



Can some please, please explain what am I missing?
 
K

Karl

Well, your example is a little too simple but...
Employee is a property of your main2 class located in the humanres namespace. Employee is an instance property, as opposed to a static propery. I wouldn't even think humanres.main2.Employee would compile.

From your user control, you could do

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

Page is the actual instance of your main2 class.

Do do it your way, you'd do

public static DatSet Employee{
get { return Employee; }
set { Employee = value; }
}

but I'm about 95% sure that isn't what you want and will not behave as you expect.

Also, I imagine it's just a typo, but your property "Employee"'s get accessor returns itself, which will have viscious side-effects....

Karl

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


Very simplified:

namespace humanres

{

public class main2 : System.Web.UI.Page

{

public DataSet Employee

{

get

{

return Employee;

}

set

{

Employee = value;

}

.......



Then to access Employee from the user control:

humanres.main2.Employee but it's (Employee) not there.

I am very confused.



Can some please, please explain what am I missing?
 
M

Mark Goldin

How am I accessing Employee from the user control?

You need a place to store the DataSet object. Try the following:

namepace humanres
{
public class main2 : System.Web.UI.Page
{
private DataSet _Employee = null;

public DataSet Employee
{
get
{return _Employee;}
set
{_Employee = value;}
}
}
}

That way the DataSet is getting stored somewhere in the object.
Remember, a property is an accessor for a variable that enables setting the
variable, and getting the variable. You need a seperate variable somewhere
to actually store the object. In the sample code I just added an underscore
to the name, but it could be anything as the name is not important, just
that the get reuturns an actual variable and the set is able to set some
variable.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage

Very simplified:

namespace humanres

{

public class main2 : System.Web.UI.Page

{

public DataSet Employee

{

get

{

return Employee;

}

set

{

Employee = value;

}

.......



Then to access Employee from the user control:

humanres.main2.Employee but it's (Employee) not there.

I am very confused.



Can some please, please explain what am I missing?
 
G

Greg Burns

In your usercontrol use this line:

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

Greg
 
M

Mark Goldin

I tried that.
Here is an 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

Source Error:


Line 56: SqlConnection conn = new SqlConnection("Initial
Catalog=manpower;Data Source=devsrv00;uid=SoSTransWebUser;pwd=yurymark;");
Line 57: SqlHelper.FillDataset(conn, CommandType.Text, "select * from
udf_getEmployeeDetails(" + EmployeeId + ")",
Line 58: ds, new string[]{"employee"});
 
G

Greg Burns

(Using Mark's example) Have you populated your private _Employee variable in
your main2 webform prior to trying to access it in the usercontrol using
main2's public Employee property?

Greg

Mark Goldin said:
I tried that.
Here is an 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

Source Error:


Line 56: SqlConnection conn = new SqlConnection("Initial
Catalog=manpower;Data Source=devsrv00;uid=SoSTransWebUser;pwd=yurymark;");
Line 57: SqlHelper.FillDataset(conn, CommandType.Text, "select * from
udf_getEmployeeDetails(" + EmployeeId + ")",
Line 58: ds, new string[]{"employee"});


Greg Burns said:
In your usercontrol use this line:

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

Greg
 
M

Mark Goldin

No, I did not populate it.
I am trying to fill Employee with data in a user control and it fails.

Greg Burns said:
(Using Mark's example) Have you populated your private _Employee variable in
your main2 webform prior to trying to access it in the usercontrol using
main2's public Employee property?

Greg

Mark Goldin said:
I tried that.
Here is an 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

Source Error:


Line 56: SqlConnection conn = new SqlConnection("Initial
Catalog=manpower;Data Source=devsrv00;uid=SoSTransWebUser;pwd=yurymark;");
Line 57: SqlHelper.FillDataset(conn, CommandType.Text, "select * from
udf_getEmployeeDetails(" + EmployeeId + ")",
Line 58: ds, new string[]{"employee"});


Greg Burns said:
In your usercontrol use this line:

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

Greg

How am I accessing Employee from the user control?

You need a place to store the DataSet object. Try the following:

namepace humanres
{
public class main2 : System.Web.UI.Page
{
private DataSet _Employee = null;

public DataSet Employee
{
get
{return _Employee;}
set
{_Employee = value;}
}
}
}

That way the DataSet is getting stored somewhere in the object.
Remember, a property is an accessor for a variable that enables setting
the
variable, and getting the variable. You need a seperate variable somewhere
to actually store the object. In the sample code I just added an
underscore
to the name, but it could be anything as the name is not important,
just
that the get reuturns an actual variable and the set is able to set
some
variable.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage

Very simplified:

namespace humanres

{

public class main2 : System.Web.UI.Page

{

public DataSet Employee

{

get

{

return Employee;

}

set

{

Employee = value;

}

.......



Then to access Employee from the user control:

humanres.main2.Employee but it's (Employee) not there.

I am very confused.



Can some please, please explain what am I missing?
 
G

Greg Burns

Well all the code that has been provided assumed that main2 filled the
dataset and you were trying to read it in the usercontrol.

I am terribly confused.

Are you trying to read the usercontrol's dataset from the webform? The
reverse of what I (we) thought.

Greg
 
M

Mark Goldin

What I am trying to do is this:
I want to have a dataset that will be accessable from any part of the
project.
Some user contols will read it to populate other controls with data.
Some controls will save dataset to the server.
 
G

Greg Burns

When you say project do your mean a single webform, or multiple webforms?
If multiple, then you need to store it in cache or session so that you read
it from other pages. OR fill you dataset on every page_load on every
webform that you need it.

If you want the dataset to be accessible from multiple forms and
usercontrols on those forms, then simply use cache. If the dataset is
specific to a single user, then use session.

If you are loading it on EVERY page_load, then it makes sense (to me at
least) to not use cache or sesssion, and use the technique Karl showed to
make the ds on the webform accessible to all it's child usercontrols.

Maybe post some code.

Greg
 
G

Greg Burns

These "components" sounds more like a Data Access Layer class than a
usercontrol.

Greg
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top