Data Access Problem when importing custom file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    Data Access Problem when importing custom file

    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 .SecurityExcept ion: Request for the
    permission of type System.Data.Sql Client.SqlClien tPermission,
    System.Data, Version=1.0.500 0.0, Culture=neutral ,
    PublicKeyToken= b77a5c561934e08 9 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="DBAc cess" %>
    protected void Page_Load(Objec t Src, EventArgs E)
    {
    DB db = new DB();
    DataSet ds = db.GetDataSet(" spu_Marketer", "Marketer") ;

    MyDataGrid.Data Source=ds.Table s["Marketer"].DefaultView;
    MyDataGrid.Data Bind();

    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(C onfigurationSet tings.AppSettin gs["ConnectionStri ng"]);
    con.Open();
    }
    }

    public DataSet GetDataSet(stri ng procName, string sString)
    {
    OpenCon();
    SqlDataAdapter sdc = new SqlDataAdapter( procName, con);
    sdc.SelectComma nd.CommandType = CommandType.Sto redProcedure;

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

    return ds;
    }

    }
    }
  • S. Justin Gengo

    #2
    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.co m> wrote in message
    news:c5c70ed9.0 308120459.7aa26 4a0@posting.goo gle.com...[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 .SecurityExcept ion: Request for the
    > permission of type System.Data.Sql Client.SqlClien tPermission,
    > System.Data, Version=1.0.500 0.0, Culture=neutral ,
    > PublicKeyToken= b77a5c561934e08 9 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="DBAc cess" %>
    > protected void Page_Load(Objec t Src, EventArgs E)
    > {
    > DB db = new DB();
    > DataSet ds = db.GetDataSet(" spu_Marketer", "Marketer") ;
    >
    > MyDataGrid.Data Source=ds.Table s["Marketer"].DefaultView;
    > MyDataGrid.Data Bind();
    >
    > 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(C onfigurationSet tings.AppSettin gs["ConnectionStri ng"]);[color=blue]
    > con.Open();
    > }
    > }
    >
    > public DataSet GetDataSet(stri ng procName, string sString)
    > {
    > OpenCon();
    > SqlDataAdapter sdc = new SqlDataAdapter( procName, con);
    > sdc.SelectComma nd.CommandType = CommandType.Sto redProcedure;
    >
    > DataSet ds = new DataSet();
    > sdc.Fill(ds, sString);
    >
    > return ds;
    > }
    >
    > }
    > }[/color]


    Comment

    • Alex

      #3
      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="Connection String"
      value="server=S omeServer;datab ase=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@aboutf ortunate.com> wrote in message news:<Ot0p2jNYD HA.440@tk2msftn gp13.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]

      Comment

      • S. Justin Gengo

        #4
        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.co m> wrote in message
        news:c5c70ed9.0 308122349.5b0fc 995@posting.goo gle.com...[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="Connection String"
        >[/color]
        value="server=S omeServer;datab ase=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@aboutf ortunate.com> wrote in message[/color]
        news:<Ot0p2jNYD HA.440@tk2msftn gp13.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]


        Comment

        • Alex

          #5
          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\myPers onalSpace

          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.Da taSource=data.G etList("spu_get Marketers", "Marketer") ;
          marketerList.Da taBind();

          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.IsPostB ack)
          { //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....assumin g I get
          permission....
          http://www.alexpop.com -- The first ever website I wrote.
          http://www.abridgegolf.com -- The last one I did.

          Comment

          • S. Justin Gengo

            #6
            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.co m> wrote in message
            news:c5c70ed9.0 308140627.74251 513@posting.goo gle.com...[color=blue]
            > Hey,
            >
            > I found what the problem was!
            > Ok, this is the set up I *had*. My project files resided on
            > \\server\myPers onalSpace
            >
            > 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.Da taSource=data.G etList("spu_get Marketers", "Marketer") ;
            > marketerList.Da taBind();
            >
            > 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.IsPostB ack)
            > { //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....assumin g I get
            > permission....
            > http://www.alexpop.com -- The first ever website I wrote.
            > http://www.abridgegolf.com -- The last one I did.[/color]


            Comment

            Working...