ASP.NET 2.0 update

Published 9/27/2005 by Henry in ASP.NET
Tags:

Through this link you can find a lot of information about ASP.NET 2.0:

http://msdn.microsoft.com/asp.net/beta2/faq/default.aspx

Henry Cordes
My thoughts exactly...


Data Access Application Block (EntLib)

Published 9/22/2005 by Henry in EntLib
Tags:

When I first started working with the Enterprise Library, I found it hard to find information about how to use it. I even heard a story about a developing-team that decided not to use it because they could not figure out fast enough how to use it.
So I will try to make some sort of Data Access Application Block walkthrough and hope this will make it easier for people to start using the Enterprise Library.

Consuming app
Suppose we got a webapplication with one page. We are going to use the Northwind database. In the Northwind database there is a stored procedure called:
CustOrdersOrders
which needs the parameter:
@CustomerID nvarchar(5).

On the page in our webapp we got a Datagrid. In this grid we want to show the data that the stored procedure returns.
We are going to use the Data Access Application Block for access to the database.

Web Application
First we create a web Application. The language I use is C#. I renamed the WebForm1.aspx to Default.aspx and put a datagrid on the page and named it dGrid, I left 'Auto Generate Columns' checked. Enterprise Library Configuration (Tool) Next we open the Enterprise Library Configuration Tool, we click on ‘File’ > ‘Open Application” or click the ‘Open’ icon.

Open application 
Open Existing Application

Browse to the directory where the web.config of the Web Application is located selected it and click on ‘Open’. Now we see the following:

New application 
New Application

You can rename the ‘Application’ node, I named it ‘WebAppEntLib’. You right-click the ‘Application’ node and choose ‘New’ > ‘Data Access Application Block’.

New dataaccesappblock 

As you see the DAAB (Data Access Application Block) is installed (with the Configuration Application Block).

New DAAB
New DAAB

Configuration Application Block
The ‘Configuration Application Block’ is installed with the Data Access Application Block and is used to store the information about which method is used to store the config information (XML File) and where this XML based dataacces config file can be found. By chancing the ‘FileName’ under ‘XML File Storage Provider’ under the ‘dataConfiguration’ node in the name with a directory name before the name (\dataConfiguration.config) you can control where the config file will be stored. If you don’t do this the file will be in the same directory as the web.config or app.config file.

Data Access Application Block
With the Configuration Tool we make the configuration settings needed to make the Data Acces Block work for us. Database Types With this node it is possible to define other database types (Oracle, MySql etc.). With the Enterprise Library the Oracle database type is included and can easily be chosen.

Oracle db
Oracle database

Sql Connection String
With the Sql Connection String node the connectionstring used to connect to the database can be defined.
Default the following nodes are provided, we use the following values:

  • Database - ‘Northwind’
  • Integrated Security - ‘False’
  • Server - ‘(local)’

We also need the nodes (with the following values):

  • Uid (userid) - ‘developer’
  • Pwd (Password) - ‘developer’

Through a right click and choosing the menu’s new > ‘parameter’ and supplying as name ‘uid’ with the Value the username we need to connect to our database (in our case ‘developer’).
And once more with New > Password parameter we supply ‘pwd’ as Name and in our case ‘developer’ as password (by clicking the ellipse).
We also change the default node name ‘SQL Connection String’ into ‘Northwind Connection String’ by right clicking the node name and choosing ‘Rename’.

Database Instances
With the Database Instances node we define database instances. A great benefit from the Data Access Application Block is that it is relatively easy to make an application communicate with multiple databases. We define which Connectionstring, Database Type and Name of the Database we are going to use through this node.

  • Connectionstring node - ‘Northwind Connection String’ (dropdown)
  • DatabaseTypeNode - ‘Sql Server’ (dropdown) Name - ‘Northwind’

choose databasetypenode 

We save this settings with File > Save Application or through the disk icon. All settings now are written in the dataconfiguration.config file <P align=left>We save this settings with File &gt; Save Application or through the disk icon.<BR>All settings now are written in the dataconfiguration.config file.<BR></P>

Example (NOT COMPLETE):

<dataConfiguration>
 <xmlSerializerSectiontype="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<enterpriseLibrary.databaseSettingsxmlns:xsd=http://www.w3.org/2001/XMLSchemaxmlns:xsi=http://www.w3.org/2001/XMLSchema-instancedefaultInstance="Northwind" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
  <databaseTypes>
    <databaseTypename="Sql Server"type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0,   Culture=neutral, PublicKeyToken=null"/>
  </databaseTypes>
  <instances>
    <instancename="Northwind"type="Sql Server"connectionString="Northwind Connection String"/>
  </instances>
  <connectionStrings>
    <connectionStringname="Northwind Connection String">
    <parameters>
      <parametername="database"value="Northwind"isSensitive="false"/>
      <parametername="Integrated Security"value="False"isSensitive="false"/>
      <parametername="uid"value="developer"isSensitive="false"/>
      <parametername="pwd"value="developer"isSensitive="true"/> <parametername="server"value="(local)"isSensitive="false"/>
    </parameters>
    </connectionString>
  </connectionStrings>
  </enterpriseLibrary.databaseSettings>
 </xmlSerializerSection>
</dataConfiguration>



In Visual Studio we highlight our project and select Project > Add Reference. On the .NET tab, we use "Browse..." to locate and set references to the following dll's:

 

  • Microsoft.Practices.EnterpriseLibrary.Configuration
  • Microsoft.Practices.EnterpriseLibrary.Data

Now we open the code-behind file for our startingpage: default.aspx.cs and add the following directives:

using Microsoft.Practices.EnterpriseLibrary.Data;using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

In default.aspx code-behind we create the method BindDataFromDAAB(), which we call in the Page_Load() 

   1:  private void BindDataFromDAAB()
   2:  { 
   3:      DataRow dataRow; 
   4:      
   5:      DataTable dataTable = new DataTable(); 
   6:      dataTable.Columns.Add(new DataColumn("OrderID", typeof(Int32))); 
   7:      dataTable.Columns.Add(new DataColumn("OrderDate", typeof(DateTime))); 
   8:      dataTable.Columns.Add(new DataColumn("RequiredDate", typeof(DateTime))); 
   9:      dataTable.Columns.Add(new DataColumn("ShippedDate", typeof(DateTime))); 
  10:      
  11:      //Create a database object 
  12:      Database dataBase = DatabaseFactory.CreateDatabase(); 
  13:      //In case of multiple databases 
  14:      //Database dataBase=DatabaseFactory.CreateDatabase("Northwind"); 
  15:      
  16:      //Create command wrappers to specify parameters 
  17:      DBCommandWrapper dbCommandWrapper = dataBase.GetStoredProcCommandWrapper("CustOrdersOrders"); 
  18:      //Fill parameter 
  19:      dbCommandWrapper.AddInParameter("@CustomerID", DbType.String,"BOTTM"); 
  20:      
  21:      using (IDataReader dataReader = dataBase.ExecuteReader(dbCommandWrapper))
  22:      { 
  23:          while (dataReader.Read())
  24:          { 
  25:              dataRow = dataTable.NewRow(); 
  26:              // 0 
  27:              OrderID, string dataRow[0] = dataReader.Int32(0); 
  28:              // 1 
  29:              OrderDate, datetime dataRow[1] = dataReader.GetDateTime(1); 
  30:              // 2 
  31:              RequiredDate, datetime dataRow[2] = dataReader.GetDateTime(2); 
  32:              // 3 
  33:              ShippedDate, datetime dataRow[3] = dataReader.GetDateTime(3); 
  34:              dataTable.Rows.Add(dataRow); 
  35:          } 
  36:          dataReader.Close(); 
  37:      } 
  38:      dGrid.DataSource = dataTable; 
  39:      dGrid.DataBind();
  40:  }


This code is pretty unreusable, but I think it gets the point across.
In the real world you could make a dataacces class to do all the database related stuff, a class Orders in which all the Orders related stuff would be. So you would not have all the data and business logic in the webpage code-behind. 


Henry Cordes 
My thoughts exactly... 

Using subreports in a Crystal Reports XI report in a ASP.NET webapplication, with the datasource dynamically set.
While using a Crystal Reports report which contains one or more subreports, the following syntax (the most logical) does not give the expected behaviour:

What happens is that the main report is shown with data as expected, but the sub report(s) are always empty.
When I googled, I found some people had the same experience. Nobody came up with an answer that solved this problem.
A developer on my team asked Business Objects by mail and at first got some links that were not even related to the problem. After a few weeks he recieved the following code snippet, which after we changed it to fit our purpose did the trick.
Code snippet

The solution is to loop through all sections in the main report and than loop through all objects per section.
In the 'objects' loop we check to find if the ObjectKind is of ObjectKind.SubreportObject.
If this is the case we have to check if the name of this subreport matches the subreport we need.
If this is the case the datasource is set with the following syntax:
crSubReportDoc.Database.Tables[0].SetDataSource(ds);

I suspect that this is the trick.
The looping to find the right subreport object might not have any use.

Henry Cordes
My thoughts exactly...