Scott Forsyth's WebLog

Postings on IIS, ASP.NET, SQL Server, Webfarms and general system admin.

August 2005 - Posts

Using connection strings from web.config in ASP.NET v2.0

ASP.NET v2.0 has a couple new ways to reference connection strings stored in the web.config or machine.config file.

A typical web.config file in v2.0 could have the following section which is placed directly under the root <configuration> section.

<connectionStrings>
    <
remove name="LocalSqlServer"
/>
    <
add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"
/>
    <add name="MainConnStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|main.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

connectionStrings>
    <
remove name="LocalSqlServer"
/>
    <
add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"
/>
    <add name="MainConnStr" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|main.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

You can reference this directly from code using:

[C#]
string connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;

[VB]
Dim connStr As String = ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString

Note that the namespace for this is System.Configuration so for a console application the full namespace is required.


Or you can reference this declaratively within the ConnectionString property of a SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
  ConnectionString="<%$ ConnectionStrings:MainConnStr %>"
  SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
/>

Posted: Aug 26 2005, 11:18 AM by OWScott | with 16 comment(s) |
Filed under:
Book Review - Building Intelligent .NET Applications

Book Review - Building Intelligent .NET Applications
(Agents, Data Mining, Rule-Based Systems, and Speech Processing)
Sara Morgan Rea

Building Intelligent .NET Applications is an excellent primer book into the world of Artificial Intelligence (AI) in the business world, specifically related to Microsoft technologies.

Building Intelligent .NET Applications is an introduction to the world of Artificial Intelligence (AI) for .NET programmers. It is the first book I have seen that shows professional .NET developers how to incorporate AI into their daily programming. In this accessible guide, developers learn how to enhance new and existing .NET applications with intelligent agents, data mining, rule-based systems, and speech processing.

Sara dives quite deep into four different branches of the vast world of AI with a great balance of conceptual theory, code samples and real world scenarios. She leads the reader though the complete process of obtaining the technologies to full implication with complete code. Both Visual Basic.Net and C# can be downloaded online while the book gives all examples in Visual Basic.Net.

Sara explores four of the most popular AI technologies by building real-world sample applications that readers can use as the basis for their own applications.  Some of the more interesting portions include; Applications that talk-critical for companies seeking to automate their call centers, Speech-enabled mobile applications, Multimodal speech applications, Data-mining predictions, which uncover trends and patterns in large quantities of data, Rule-based programming for applications that can be more reactive to their environments, Multiple software agents that are able to keep remote users up to date and sample applications for Windows and the Web.

The book starts out with a one chapter overview called "Instruction" which is exactly that. It introduces the reader to Business Artificial Intelligence and lays the groundwork for the rest of the book.  Immediately in chapter two the book dives into Microsoft Speech Server which is the first of four main technologies that are covered in this book.  Microsoft Speech Server is covered until Chapter 5 when the book dives into Data-Mining predictions.  Chapter 7 gets into Rule-based systems and Chapter 8 into building Agents.  

Chapter 9 finishes off the book with an excellent overview of Artificial Intelligence.  In fact, for an overview of AI and Microsoft's investment into it now and in the near future, the final chapter of the book was my favorite.   Sara painted an exciting picture of what is in store, as well as opening my eyes to things that exist already. AI isn't a thing of the distant future; in fact there is an exciting array of mature technologies in use and available today.

Personally I felt that Chapter 9 would have made a better introduction chapter.  I didn't feel that Artificial Intelligent or Business AI was covered in much depth in the first chapter of the book.  By the time chapter 2 dove in deep into the first branch of the four topics, I still had some unanswered overview questions in my mind.  After reading Chapter 9 though, the need I felt for more general information was met.  

Now with Microsoft Speech server, applications that can talk and interact intelligently with a user is not only possible, it's relatively easy and affordable, even for the small business.  Developers can create powerful, intelligent applications that are specific to their business.  You can create fully database-driven talking applications that understand speech, talk back (not like a rebellious 15-year-old) and respond differently to each unique situation.  This can be used for a telephone application, someone sitting in front of a dumb terminal with audio capability or for a fully configured computer application.  Dream big, the options are endless, the solutions are within reach.

Running reports against data has been common for decades, but consider intelligent agents that will dig, analyze, determine a new direction to dig by itself, and return relevant patterns and trends in the data that were never discovered before.  Sara covers this very topic with theory, code examples, scenarios and clear and precise explanations.

Agents that self perpetuate, learn their new environment and respond accordingly are the way of the future.  The most obvious and painfully in-your-face examples are malicious worms and spyware applications.  Worms lodge themselves in an environment, take advantage of their new home by finding important information like a list of emails addresses, and then they spread automatically, continuing this vicious cycle.  Spyware agents also install themselves in an environment and start interacting with it to get information to send back to their creator.  Now, consider the endless possibilities where Agents can be used for good, and are in use today.  The author covers this very topic.

I wouldn't say this book is a general overview of Business Intelligent Design, but rather a specific look at four major technologies and a few minor technologies.  The Microsoft products covered are Microsoft Visual Studio.Net, Microsoft Speech Server and SASDK, Microsoft SQL Server, Online Analytical Processing (OLAP), BizTalk Server, Microsoft Agent, Background Intelligent Transfer Service (BITS) and I'm sure a couple other smaller technologies that I didn't list.  In addition to these, Sara briefly covers SQL Server 2005, Analysis Services 2005, and Longhorn with Avalon, Indigo, and WinFS.

One of the characteristics of a good technical book is making the complex subjects sound simple. The author has done a tremendous job of that in this book. The range of topics that she covers at first glance seems complex, but at no point does she leave the reader overwhelmed. At the same time she doesn't over explain or drag on needlessly.

This book is about the IA (Intelligent Applications) part of AI (Artificial Intelligence). It focuses on Microsoft solutions for Speech solutions, Agents, Data Mining and Rule-Based Systems, and does a great job of it.

The following sample chapters from the book are available online at Addison-Wesley's site:

Data-Mining Predictions

Creating .NET Applications That Talk

http://www.awprofessional.com/bookstore/product.asp?isbn=0321246268&rl=1

Enabling Roles in ASP.NET v2.0

By default the Roles provider is defined in machine.config but it isn't enabled.  Attempting to use the Roles feature before it is enabled will throw the following error:

"The Role Manager feature has not been enabled."

It's easy to enable though.  The ASP.NET v2.0 quickstart explains how to enable this:
http://beta.asp.net/QUICKSTART/aspnet/doc/security/membership.aspx#roles

Since the provider is already defined in machine.config, you can use the same provider or define a new one.  The advantage of using the one in machine.config is that the server administrator can keep it up to date and consistent with the other providers.  I'll give two examples, one inheriting the default machine-level provider and one specifying a new one.  These goes in the <system.web /> section of web.config.

Example 1 - Inherit the machine-level provider

Notice that the defaultProvider name is AspNetSqlRoleProvider which is what is specified in machine.config by default.  It's essential to use this provider name if you will inherit the provider settings.

<roleManager
                    enabled="true"
                    cacheRolesInCookie="true"
                    defaultProvider="AspNetSqlRoleProvider"
                    cookieName=".ASPXROLES"
                    cookiePath="/"
                    cookieTimeout="30"
                    cookieRequireSSL="false"
                    cookieSlidingExpiration="true"
                    createPersistentCookie="false"
                    cookieProtection="All" />

Example 2 - Override and specify all roleManager settings

I took this example directly from http://beta.asp.net.  Notice that the defaultProvider name can be anything you want as long as it matches the provider name.  If you use AspNetSqlRoleProvider which is the name that machine.config uses by default, then make sure to put <remove name="AspNetSqlRoleProvider" /> before the <add />  tag.  Also notice connectionStringName which needs to be defined in machine.config or web.config and point to a database that is prepared with the asp.net v2.0 schema.

<roleManager
                    enabled="true"
                    cacheRolesInCookie="true"
                    defaultProvider="QuickStartRoleManagerSqlProvider"
                    cookieName=".ASPXROLES"
                    cookiePath="/"
                    cookieTimeout="30"
                    cookieRequireSSL="false"
                    cookieSlidingExpiration="true"
                    createPersistentCookie="false"
                    cookieProtection="All">
            <providers>
                <add name="QuickStartRoleManagerSqlProvider"
                    type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                    connectionStringName="ASPNETDB"
                    applicationName="SecurityQuickStart"/>
            </providers>
        </roleManager>

In case you are curious and for perspective, I'll include the default machine.config definition for the roleManager section.

        <roleManager>
            <providers>
                <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/"
                    type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add name="AspNetWindowsTokenRoleProvider" applicationName="/"
                    type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
            </providers>
        </roleManager>

Posted: Aug 19 2005, 03:14 PM by OWScott | with 2 comment(s)
Filed under:
More Posts