Connecting Tech Pros Worldwide Forums | Help | Site Map

Data Access Problem when importing custom file

Alex
Guest
 
Posts: n/a
#1: Nov 17 '05
Hi,

I have an aspx file that creates a custom class object and calls a
method which should return a DataSet. It throws a:

Description: The application attempted to perform an operation not
allowed by the security policy. To grant this application the required
permission please contact your system administrator or change the
application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the
permission of type System.Data.SqlClient.SqlClientPermission,
System.Data, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 failed.

Error.

When the methods from DB.cs is included in the aspx file it works
ok!!!

What is the problem? And how can I resolve it so that I have code
seperationg.

Here are the files in more detail:

aspx page:
<%@ Import Namespace="DBAccess" %>
protected void Page_Load(Object Src, EventArgs E)
{
DB db = new DB();
DataSet ds = db.GetDataSet("spu_Marketer", "Marketer");

MyDataGrid.DataSource=ds.Tables["Marketer"].DefaultView;
MyDataGrid.DataBind();

AnotherDataGrid.DataSource=ds.Tables["Marketer"].DefaultView;
AnotherDataGrid.DataBind();

}

DB.cs

namespace DBAccess
{
/// <summary>
/// ADO.NET data access using the SQL Server Managed Provider.
/// </summary>
public class DB
{
// connection to data source
private SqlConnection con;

private void OpenCon()
{
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
}
}

public DataSet GetDataSet(string procName, string sString)
{
OpenCon();
SqlDataAdapter sdc = new SqlDataAdapter(procName, con);
sdc.SelectCommand.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();
sdc.Fill(ds, sString);

return ds;
}

}
}

S. Justin Gengo
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Data Access Problem when importing custom file


Alex,

Are you using integrated security on the sql server?

If you gave the aspnet account (or another that the web site is running
under) access to the sql server and are using integrated security it is most
likely that the user account that you are calling this code from is
different and it doesn't have access.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


"Alex" <alex@hotpop.com> wrote in message
news:c5c70ed9.0308120459.7aa264a0@posting.google.c om...[color=blue]
> Hi,
>
> I have an aspx file that creates a custom class object and calls a
> method which should return a DataSet. It throws a:
>
> Description: The application attempted to perform an operation not
> allowed by the security policy. To grant this application the required
> permission please contact your system administrator or change the
> application's trust level in the configuration file.
>
> Exception Details: System.Security.SecurityException: Request for the
> permission of type System.Data.SqlClient.SqlClientPermission,
> System.Data, Version=1.0.5000.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089 failed.
>
> Error.
>
> When the methods from DB.cs is included in the aspx file it works
> ok!!!
>
> What is the problem? And how can I resolve it so that I have code
> seperationg.
>
> Here are the files in more detail:
>
> aspx page:
> <%@ Import Namespace="DBAccess" %>
> protected void Page_Load(Object Src, EventArgs E)
> {
> DB db = new DB();
> DataSet ds = db.GetDataSet("spu_Marketer", "Marketer");
>
> MyDataGrid.DataSource=ds.Tables["Marketer"].DefaultView;
> MyDataGrid.DataBind();
>
> AnotherDataGrid.DataSource=ds.Tables["Marketer"].DefaultView;
> AnotherDataGrid.DataBind();
>
> }
>
> DB.cs
>
> namespace DBAccess
> {
> /// <summary>
> /// ADO.NET data access using the SQL Server Managed Provider.
> /// </summary>
> public class DB
> {
> // connection to data source
> private SqlConnection con;
>
> private void OpenCon()
> {
> if (con == null)
> {
> con = new[/color]
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);[color=blue]
> con.Open();
> }
> }
>
> public DataSet GetDataSet(string procName, string sString)
> {
> OpenCon();
> SqlDataAdapter sdc = new SqlDataAdapter(procName, con);
> sdc.SelectCommand.CommandType = CommandType.StoredProcedure;
>
> DataSet ds = new DataSet();
> sdc.Fill(ds, sString);
>
> return ds;
> }
>
> }
> }[/color]


Alex
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Data Access Problem when importing custom file


Morning Justin,

Thanks for you post. I'm using a specific user/pwd for the DB as
defined in the connection string. For completeness here it is:
<appSettings>
<add key="ConnectionString"
value="server=SomeServer;database=XXX;uid=XXX;pwd= yy;Trusted_Connection=yes"
/>
</appSettings>


I defined a specific DB user which the asp should use. Remember that
if I take the two files and put the code into the aspx page it works.
Simply cutting and pasting the two methods, and calling the method
directly (not creating the DB object) works.

You are thinking that when I split them accross two files it's trying
to use my WINDOWS username/pass to access the DB? I will look into it.
Where would this setting be?

Do you think it would be better to use a codebehind page rather than
include an assembly? I would prefer an assembly as this is how it
should be!

Thank you kindly for you help.

Alex
"S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message news:<Ot0p2jNYDHA.440@tk2msftngp13.phx.gbl>...[color=blue]
> Alex,
>
> Are you using integrated security on the sql server?
>
> If you gave the aspnet account (or another that the web site is running
> under) access to the sql server and are using integrated security it is most
> likely that the user account that you are calling this code from is
> different and it doesn't have access.
>
> Sincerely,
>
> --
> S. Justin Gengo, MCP
> Web Developer[/color]
S. Justin Gengo
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Data Access Problem when importing custom file


Alex,

Yes, I think it's trying to use your windows login.

For the assembly I would just hard code the connection string in the
connection object. I believe that will solve your problem.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


"Alex" <alex@hotpop.com> wrote in message
news:c5c70ed9.0308122349.5b0fc995@posting.google.c om...[color=blue]
> Morning Justin,
>
> Thanks for you post. I'm using a specific user/pwd for the DB as
> defined in the connection string. For completeness here it is:
> <appSettings>
> <add key="ConnectionString"
>[/color]
value="server=SomeServer;database=XXX;uid=XXX;pwd= yy;Trusted_Connection=yes"[color=blue]
> />
> </appSettings>
>
>
> I defined a specific DB user which the asp should use. Remember that
> if I take the two files and put the code into the aspx page it works.
> Simply cutting and pasting the two methods, and calling the method
> directly (not creating the DB object) works.
>
> You are thinking that when I split them accross two files it's trying
> to use my WINDOWS username/pass to access the DB? I will look into it.
> Where would this setting be?
>
> Do you think it would be better to use a codebehind page rather than
> include an assembly? I would prefer an assembly as this is how it
> should be!
>
> Thank you kindly for you help.
>
> Alex
> "S. Justin Gengo" <sjgengo@aboutfortunate.com> wrote in message[/color]
news:<Ot0p2jNYDHA.440@tk2msftngp13.phx.gbl>...[color=blue][color=green]
> > Alex,
> >
> > Are you using integrated security on the sql server?
> >
> > If you gave the aspnet account (or another that the web site is running
> > under) access to the sql server and are using integrated security it is[/color][/color]
most[color=blue][color=green]
> > likely that the user account that you are calling this code from is
> > different and it doesn't have access.
> >
> > Sincerely,
> >
> > --
> > S. Justin Gengo, MCP
> > Web Developer[/color][/color]


Alex
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Data Access Problem when importing custom file


Hey,

I found what the problem was!
Ok, this is the set up I *had*. My project files resided on
\\server\myPersonalSpace

On the IIS box (different server) I had set up a Web Site poiting to
the folder on myPersonalSpace. I was invoking the file from DW which
has a site defined poiting to myPersonalSpace.

When I moved all the files from myPersonalSpace and dumped in the
folder in wwwroot, and re-defined the site in DW to point to this
folder. Everything worked.

It was the fact that the IIS server was running the files accross the
network that caused the problems. Now I have a nice set up with a
codebehind class, and a Database layer (dll in bin directory).

Works much better, but I do have another interesting problem.


marketerList.DataSource=data.GetList("spu_getMarke ters", "Marketer");
marketerList.DataBind();

GetList is your typical method that runs a stored proc. marketerList
is a dropdownlist containing names of marketers. The list is populated
first time it is loaded. When the page is posted back, my lists don't
get populated anymore.
Of course, it's not running the stored proc as I bypass it through

if(Page.IsPostBack)
{ //Do nothing }
else
{ GetList...}

I can't see why it's not preserving state between requests. I haven't
defined an "action" in the form tag. Any ideas?

Thanks a lot,
Alex

PS. I'll be sure to check out your code repository ;)
PSS. And i'll release what I write as well....assuming I get
permission....
http://www.alexpop.com -- The first ever website I wrote.
http://www.abridgegolf.com -- The last one I did.
S. Justin Gengo
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Data Access Problem when importing custom file


Alex,

Great to hear that you solved it!

Repopulation not taking place is almost always due to viewstate not being
enabled (for the page, the placeholder a dynamic control is in, the dynamic
control itself, a regular control, a panel a regular control is in, etc.)

I suggest looking at all levels of viewstate you may have pertaining to this
particular control. It's probably disabled at some level.

About placing code in my repository: If you'd like to submit some to share
with others that would be great! Of course I'll give you credit for
anything included.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


"Alex" <alex@hotpop.com> wrote in message
news:c5c70ed9.0308140627.74251513@posting.google.c om...[color=blue]
> Hey,
>
> I found what the problem was!
> Ok, this is the set up I *had*. My project files resided on
> \\server\myPersonalSpace
>
> On the IIS box (different server) I had set up a Web Site poiting to
> the folder on myPersonalSpace. I was invoking the file from DW which
> has a site defined poiting to myPersonalSpace.
>
> When I moved all the files from myPersonalSpace and dumped in the
> folder in wwwroot, and re-defined the site in DW to point to this
> folder. Everything worked.
>
> It was the fact that the IIS server was running the files accross the
> network that caused the problems. Now I have a nice set up with a
> codebehind class, and a Database layer (dll in bin directory).
>
> Works much better, but I do have another interesting problem.
>
>
> marketerList.DataSource=data.GetList("spu_getMarke ters", "Marketer");
> marketerList.DataBind();
>
> GetList is your typical method that runs a stored proc. marketerList
> is a dropdownlist containing names of marketers. The list is populated
> first time it is loaded. When the page is posted back, my lists don't
> get populated anymore.
> Of course, it's not running the stored proc as I bypass it through
>
> if(Page.IsPostBack)
> { //Do nothing }
> else
> { GetList...}
>
> I can't see why it's not preserving state between requests. I haven't
> defined an "action" in the form tag. Any ideas?
>
> Thanks a lot,
> Alex
>
> PS. I'll be sure to check out your code repository ;)
> PSS. And i'll release what I write as well....assuming I get
> permission....
> http://www.alexpop.com -- The first ever website I wrote.
> http://www.abridgegolf.com -- The last one I did.[/color]


Closed Thread