Data Access Problem when importing custom file

Discussion in 'ASP .Net' started by Alex, Aug 12, 2003.

  1. Alex

    Alex Guest

    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;
    }

    }
    }
     
    Alex, Aug 12, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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, Aug 12, 2003
    #2
    1. Advertising

  3. Alex

    Alex Guest

    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" <> wrote in message news:<>...
    > 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
     
    Alex, Aug 13, 2003
    #3
  4. 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" <> wrote in message
    news:...
    > 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" <> wrote in message

    news:<>...
    > > 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
     
    S. Justin Gengo, Aug 13, 2003
    #4
  5. Alex

    Alex Guest

    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_getMarketers", "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.
     
    Alex, Aug 14, 2003
    #5
  6. 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" <> wrote in message
    news:...
    > 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_getMarketers", "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, Aug 14, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ravi sankar
    Replies:
    2
    Views:
    395
    abdul bari
    Aug 27, 2003
  2. plb
    Replies:
    2
    Views:
    369
  3. Mark Line
    Replies:
    3
    Views:
    428
    J Correia
    Oct 21, 2005
  4. Miguel Ramirez

    Importing excel file data from user's computer

    Miguel Ramirez, Oct 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    362
    Steve C. Orr [MVP, MCSD]
    Oct 5, 2006
Loading...

Share This Page