Thursday, February 4, 2010

ASP.NET Web PartsAS


ASP.NET Web Parts controls are an integrated set of controls for creating Web sites that enable end users to modify the content, appearance, and behavior of Web pages directly in a browser. The Web Parts control set consists of three main building blocks:  
  1. Personalization,
  2. User interface (UI) structural components, and
  3. Actual Web Parts UI controls.

  1. Personalization: Web Parts use a Personalization Provider.  This is so users that visit the site get a different perspective, depending on their personalization.  One user might want a red background while another might want blue.  This is their personalization, and it is stored in a database. By default, Web Parts are configured to use Sql server 2005 Express edition. For another version of Sql server 2005, there are few things which we must do to make it work smoothly.

First of all, we need to create a personalization database named aspnetdb. By default, Web Parts look for this database to keep the user personalization information. In order to do this, we must use the 'aspnet_regsql.exe' wizard.  Browse out to '%windir%\Microsoft.NET\Framework\' and run this command.  Once the wizard is up, choose to 'Configure SQL Server for application services'.  Select the correct server name (if using a named instance, be sure to include the server and named instance, such as 'fileserver\sql2k5instance').  If using SQL Server authentication, put in your username and password.  Select the database that will serve as your personalization database.  Now database is set up for membership and personalization.
   
  Now add a connection string to Web.config file with the key name ‘localSqlServer’. 
 <connectionStrings>
          <remove name="LocalSqlServer"/>
          <add name="LocalSqlServer" connectionString="Server=G01BR1P264; Database=aspnetdb; Integrated Security=SSPI; MultipleActiveResultSets=true;"providerName="System.Data.SqlClient"/>
    connectionStrings> 
   
  The name of the connection string is important, as Web Parts look by default for a connection string named 'LocalSqlServer'.  The data source is equally important and should reflect our database instance (be sure to include the name if using a named instance).  Since I have set up the 'aspnetdb' database as my personalization database, this will be what I use as the Initial Catalog.  Also, since only SQL Server 2005 Express Edition supports User Instances, I will set this property to 'false'. 
    Apart from these, either you have to user Window Authentication or Form Authentication. 
    Window Authentication: To use Window Authentication, Add the following to the Web.config. 
   <authentication mode="Windows">authentication> 
     Also, open the Inet Manger of your server, point to your website and open properties. Under the Directory security tab click on ‘Edit’ Anonymous access and Authenication control. 

Your browser may not support display of this image.


     Uncheck the Anonymous Access and check mark the integrated Windows authentication.  
Form Authentication: To use Form Authentication, Add the following in the web.config 
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="Home.aspx" cookieless="UseUri" />
      authentication> 
Here loginUrl will be your login page name and defaultUrl will be your Home Page. Cookieless is the option property,  if false, a cookie will be saved to a user machine.
Now we have to user the FormAuthentication class and it’s static member to add the user as authenticated. 
For example: suppose we have another database where we keep the user list and their login credentials. 
Add the connection string information of this database also to web.config. During login process, check the user input against the duser credentials store in a database table. If it is true then redirect user to home page using  
FormsAuthentication.RedirectFromLoginPage(UserName, false); 
This is a static method of the FormAuthentication Class which add the user to authenticated list and redirect to default page mentioned in a web.config.
Now we can whether user has been authenticated or not by 
HttpContext.Current.User.Identity.IsAuthenticated Which return true in case of authenticated user. 
There is one another method which is used to make user log out. 
FormsAuthentication.SignOut(); 
  1. User interface (UI) structural components: 

Deploying Reports on the server programmatically

 SSRS provides 3 ways to deploy the reports on the server. 
  • Using the BI Development studio.
  • Using the SQL Server Management Studio
  • Programmatically ( using RS.EXE utility)
The first two options provide a very simple and easy way of deployment. But there are some cases where these options won’t work. For example if you want to deploy the report on the server that’s not directly accessed from your network/domain.
The only option to deploy the report in this scenario is deploying reports programmatically.
RS.EXE is a Command-line utility which is used to perform deployment and other administrative task programmatically. This RS.EXE utility expects a file with the extension .RSS which is usually written in VB.NET. The RS file will contain script to create Report folder on the server, to create Report Data Source and to publish the reports as well. 
Steps:
  1. Copy all the RDL file and RS file to server where you want to deploy the reports. Please note that all the RDL files and RS file (script file) needs to placed in the same folder.
  2. Open the command prompt and type the following command to execute the script:
    For example if you have all the files on a location “D:\Reports”  

This command will read the script from the” FileName.rss” file and create a report folder for you, report data source and publish all the reports you have mentioned in the file.
Below is the code for your RSS file.
Dim ReportSource As String
Public Sub Main()
TRY
  ReportSource = "D:\Reports"    ‘Path of the folder where script and reports file are located 
     rs.Credentials = System.Net.CredentialCache.DefaultCredentials      
     
    If rs.GetItemType("/Reports") = Microsoft.SqlServer.ReportingServices2005.ItemTypeEnum.Folder Then   
          rs.DeleteItem("/Reports")      ‘Deleting the report folder if already exists
    End If
        
    rs.CreateFolder("Reports", "/", Nothing)    ‘Creating the Report folder            
    Console.WriteLine("Parent folder [Reports] created successfully.")
       
    CreateReportDataSource("Reports", "SQL", "Data Source=(local);Initial Catalog=DataBaseName")    ‘Creating the Report data source
    PublishReport("MyReport")    ‘publishing reports with the name MyReport.rdl’
   
‘Note: you can publish as many reports you want, just keep adding ‘PublishReport(ReportName) method here,
   
    Console.WriteLine("Tasks completed successfully.")
CATCH  ex As Exception
   THROW ex
END TRY
End Sub
Public Sub CreateReportDataSource(name As String, extension As String, connectionString As String)
    'Data source definition.
    Dim definition As New DataSourceDefinition()
    definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
    definition.ConnectString = connectionString
    definition.Enabled = True
    definition.Extension = extension
   
      TRY 
            rs.CreateDataSource(name, "/Reports", False, definition, Nothing)   
          Console.WriteLine("Data source: {0} created successfully.", name)
      CATCH e As Exception
            Console.WriteLine("ERROR creating data source: " + name)
            THROW e
      END TRY
   
End Sub 
Public Sub PublishReport(ByVal reportName As String) 
TRY
    Dim stream As FileStream = File.OpenRead(ReportSource + "\" + reportName + ".rdl")
    definition = New [Byte](stream.Length) {}
    stream.Read(definition, 0, CInt(stream.Length))
    stream.Close()

   
    rs.CreateReport(reportName, parentPath, False, definition, Nothing)  
    Console.WriteLine("Report: {0} published successfully.", reportName)        
CATCH e As Exception
      Console.WriteLine("ERROR while Publishing report: " + reportName)
      THROW e
END TRY
End Sub