locked
Why SqlDataSource picks wrong data in a concurrent environment? RRS feed

  • Question

  • User246057125 posted

    I have few GridView controls attached to SqlDataSource. The SqlDataSource is bind to the connection string in web.config file. In a concurrent environment, the SqlDataSource starts picking cached or probably other user's data.

    How to safely use SqlDataSource in a concurrent environment and how to dispose it?

    Saturday, February 25, 2012 3:43 AM

Answers

All replies

  • User3866881 posted

    How to safely use SqlDataSource in a concurrent environment

    SqlDataSource is born supporting "Concurrency" problem,what you can do is just bind to the GridView and set some properties:

    And then bind the SqlDataSource to the GridView,http://www.asp.net/web-forms/tutorials/data-access/accessing-the-database-directly-from-an-aspnet-page/implementing-optimistic-concurrency-with-the-sqldatasource-cs

    Please notice that the SqlDataSource will manage it itself,so there's no need for you to Dispose it manually。

    Reguards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 26, 2012 8:22 PM
  • User246057125 posted

    My problem is that SqlDataSource is picking any other connection values and binding the GridView with that. Don't know from where it is picking this wrong connection. I double checked SqlDataSource connection string and it is picking each time from the web.config file and not from any connection variable somewhere.

    I have enabled that Optimistic Concurrency option though. I also want to know how to set this option from code-behind.

    Monday, February 27, 2012 12:38 AM
  • User3866881 posted

    My problem is that SqlDataSource is picking any other connection values and binding the GridView with that. Don't know from where it is picking this wrong connection. I double checked SqlDataSource connection string and it is picking each time from the web.config file and not from any connection variable somewhere.

    Show us your web.config as well as your aspx codes of SqlDataSource。

    Monday, February 27, 2012 12:53 AM
  • User246057125 posted

    @Decker:

    Below is the code of GridView:

                       <asp:GridView id="gvUpdates" runat="server" DataSourceID="sqldLastUpdates">
                            <rowstyle font-names="Arial" font-size="Small" HorizontalAlign="Center" />
                            <headerstyle backcolor="AliceBlue" Font-Bold="True" Font-Names="Arial" Font-Size="Small" />
                            <alternatingrowstyle backcolor="#E0E0E0" />
                        </asp:GridView>   
                        <asp:SqlDataSource ID="sqldLastUpdates" runat="server" ConnectionString="<%$ ConnectionStrings:ConStringPWS %>"></asp:SqlDataSource>                 

    And this is my web.config code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 
        Note: As an alternative to hand editing this file you can use the 
        web admin tool to configure settings for your application. Use
        the Website->Asp.Net Configuration option in Visual Studio.
        A full list of settings and comments can be found in 
        machine.config.comments usually located in 
        \Windows\Microsoft.Net\Framework\v2.x\Config 
    -->
    <configuration>
    	<connectionStrings>
    		<add name="ConStringPWS" connectionString="Data Source=.;Initial Catalog=pWS;Integrated Security=true;" providerName="System.Data.SqlClient" />
       	</connectionStrings>
      <system.web>
    		<!-- 
                Set compilation debug="true" to insert debugging 
                symbols into the compiled page. Because this 
                affects performance, set this value to true only 
                during development.
            -->
        <sessionState mode="InProc"  stateConnectionString="tcpip=127.0.0.1:42424" stateNetworkTimeout="60" sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI" sqlCommandTimeout="600" customProvider="" cookieless="true" cookieName="ASP.NET_SessionId" timeout="6000" allowCustomSqlDatabase="false" regenerateExpiredSessionId="true" partitionResolverType="" useHostingIdentity="true">
          <providers>
          <clear />
          </providers>
        </sessionState>
    
        <httpHandlers>
          <remove path="*.asmx" verb="*" />
          <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
          <add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
          <add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
          <add path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
        </httpHandlers>
        <httpModules>
          <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </httpModules>    
    		<compilation debug="true">
        <assemblies>
          <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
          <add assembly="System.Drawing.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
          <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.Shared, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.Enterprise.Framework, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
          <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        </assemblies>
        <buildProviders>
          <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </buildProviders>
      </compilation>
    		<!--
                The <authentication> section enables configuration 
                of the security authentication mode used by 
                ASP.NET to identify an incoming user. 
            -->
    		<authentication mode="Windows" />
    		<!--
                The <customErrors> section enables configuration 
                of what to do if/when an unhandled error occurs 
                during the execution of a request. Specifically, 
                it enables developers to configure html error pages 
                to be displayed in place of a error stack trace.
    
            <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                <error statusCode="403" redirect="NoAccess.htm" />
                <error statusCode="404" redirect="FileNotFound.htm" />
            </customErrors>
            -->
        
      </system.web>
     <location allowOverride="true" inheritInChildApplications="true">
      <appSettings>
       <add key="CrystalImageCleaner-AutoStart" value="true" />
       <add key="CrystalImageCleaner-Sleep" value="60000" />
       <add key="CrystalImageCleaner-Age" value="120000" />
      </appSettings>
     </location>
    </configuration>
                

    Monday, February 27, 2012 1:01 AM
  • User3866881 posted

    It seems that everything is going well with your codes offered to us,and have you made sure that you haven't dynamically changed the conn str by setting something like SqlDataSource.ConnectionString……?

    Monday, February 27, 2012 1:07 AM
  • User246057125 posted

    As far as I remember, I have not changed it dynamically.

    Monday, February 27, 2012 1:11 AM
  • User3866881 posted

    As far as I remember, I have not changed it dynamically.

    This looks strange……Plz delete the whole page after you back up the codes there,and then remove the conn str in the web.config。Then create a new page and according to the Wizard of SqlDataSource……to see what's happening?

    Monday, February 27, 2012 1:15 AM