Caveman's Blog

My commitment to learning.

Archive for the ‘ASP.Net’ Category

EF4: Searching Japanese text won’t work

leave a comment »


Problem: EF4 does not to return any data when searching for Japanese names from a user table.

Setup: The web application layers use the following technologies

ASP.Net <—-> Business Layer <—-> EF 4 <—-> SQL Server 2008

Stored procedures are being employed for data retrieval operations. All the stored procedure parameters are of nvarchar type and the data is stored in the tables as nvarchar type as well.

Troubleshooting:

1. Stored procedures work fine in SSMS.

exec sp_search_user N'Japanese text', N'Japanese Text'

Here is what I got from SQL Profiler: exec sp_search_user ‘??’, ‘??’

The Japanese text got replaced with ??

2. Adding RequestEncoding=”utf-8″ ResponseEncoding=”utf-8″ attributes to the Page directive had no impact on the outcome.

3. The data reached the data access layer intact and here is the code that makes a call to the function import:

public IQueryable SearchUsers(string first_name, string last_name)
{
//db is the database context
ObjectResult SearchResult = db.SearchUsers(first_name, last_name);
IQueryable users = from tmp in SearchResult.AsQueryable() select tmp;
return users;
}

4. I have also verified that the East Asian Language pack was indeed installed on the application server.

Solution: Apparently, I found out (with help from  TinMgAye) that the edmx file could not update the data types of the stored procedure to nvarchar from varchar.

There could be a flaw inside the edmx definition about nvarchar and varchar. How we can verify is, firstly make sure your stored procedure in SQL is accepting input parameter as nvarchar. Then try to remove all the function definition and stored procedure from edmx and update edmx again to include the stored procedure and function. Or you know what you are doing mode…. Right click edmx >> Open with >> choose XML editor, then look for stored procedure name in function tag and check the parameter type there.

<Function Name="Your SP Name"><Parameter Name="Para Name" Type="nvarchar" Mode="In" /> </Function>

Cheers !

Written by cavemansblog

June 22, 2012 at 8:56 pm

Failed to enable constraints

leave a comment »


Debugging a “failed to enable constraint” error when filling a data table was very hard until I found this wonderful tip. This blog post is only for documentation purpose and the full credit goes to PaulStock for this awesome response.

This problem is usually caused by one of the following

  • null values being returned for columns not set to AllowDBNull
  • duplicate rows being returned with the same primary key.
  • a mismatch in column definition (e.g. size of char fields) between the database and the dataset

Try running your query natively and look at the results, if the resultset is not too large. If you’ve eliminated null values, then my guess is that the primary key columns is being duplicated.

Or, to see the exact error, you can manually add a Try/Catch block to the generated code like so and then breaking when the exception is raised:

enter image description here

Then within the command window, call GetErrors method on the table getting the error.
For C#, the command would be ? dataTable.GetErrors()
For VB, the command is ? dataTable.GetErrors

enter image description here

This will show you all datarows which have an error. You can get then look at the RowError for each of these, which should tell you the column that’s invalid along with the problem. So, to see the error of the first datarow in error the command is:
? dataTable.GetErrors(0).RowError
or in C# it would be ? dataTable.GetErrors()[0].RowError

enter image description here

Reference:

1. Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

Written by cavemansblog

June 10, 2012 at 10:26 am

How to: Web application architecture – Part 1

with 3 comments


This is a multi-part blog post that will detail the process of architecting a web application. The first part of this article is a list of terms that one might want to be familiar with,when designing a web application. I have tried to structure this glossary based on the design needs of a web application in general and promise to update this list as and when I can remember more.

Project Scope

Prototype

Source Control tool (Subversion, TFS, Visual Source safe, IBM Clearcase)

Bug/Issue tracker (HP Quality Center, IBM Clear Quest, JIRA, etc)

Infrastructure Solution, Code Solution, DB Solution

Data security and compliance (SSL, encryption, web service authentication, etc)

Core Framework

Application Scalability

Smoke testing (Selenium, etc)

Stress test (VSTS, Silk, MS – Web stress tool, etc)

Continuous Integration (Hudson, Cruise Control, etc)

Content Management, Search engine optimization, Analytics,

Infrastructure: Web Servers, DB Servers, Back office servers, CDN, etc

Production/Mock deployment environment: DNS, Firewall, Load Balancer, Sticky Session audit, failover, mail.

Deployment Process

Hudson Production Job

Rollback on failed deployment

SQL Merge / change script

Why is my web application slow?

with 10 comments


Web application slowness in different environments in a software factory-line is something that we all must have come-across at some point or the other. Here is my perspective on “where to begin” to address this issue in a general sense for a Microsoft centric web application. Let as assume that our application was developed using ASP.Net and a SQL Server database.

Web Server

1. Go though the Event Viewer log for any errors, warnings and informational messages. Watch out for messages that were logged by your application and another other applications on your web server.

2. Check the IIS logs to see if there is any unusual response rate i.e. errors (http 500, 404, etc).

3. The application pool in IIS can be a source of the slowness of the application.

4. The web server could have ran out of disk space (lack of error log rolling and backup service).

5. IIS crash because of a memory leak, thread locking, etc. Check out Troubleshooting an IIS Crash/Hang

6. JavaScript interaction with UI objects (Flash, Applets, Silverlight).

7. Make sure that the web server is up-to-date with all the latest “software patches” 😉 (oops !!!  service packs)

8. Make sure that the database connection pool settings are correct.

9. Consider rendering the website content using a content delivery network (CDN) service provider like Akamai, Amazon CloudFront, Microsoft Azure, AT&T, etc.

10. If none of the above seems to cause an issue then read-on.

Application Code

1. Analyzing the web request and the web response across multiple pages of the application using tools like Charles, Fiddler, Firebug, etc can provide you lot of information that you would not know otherwise.

2. Narrow down the scope of the slowness in the page execution.

3. Not disposing objects after use can eat up lot of resources on the web server causing the slowness.

4. Make sure that the response times of all the Ajax, web service calls  are in line with the expectations.

5. Run extended load tests to determine if there might be a new cause of failure, that might not have been noticed during regular load tests.

6. Always employ best practices for implementing web site acceleration [1].

7. Consider fetching multiple result sets in one database call as opposed to one result set per database call. This will reduce the number of round trips to the database.

8. If none of the above seems to cause an issue then read-on.

Database

1. Low disk space on the SQL Server/Cluster.

2. Not following SQL Server best practices.

3. Go though the execution plans of the various suspect SQL scripts/statements to isolate the issue. Table scan can be a very costly operation as opposed to a index scan.

4. Run a SQL Trace for a few hours in an environment with lot of traffic and feed the trace file to the SQL Server Performance Tuning wizard.

5. Apply the recommendations of the SQL Server Performance Tuning wizard to the database to see if that helps.

6. Verify and make sure that the background SQL, SSIS and SSRS tasks are scheduled to run during off-peak hours.

7. Considering breaking down a huge database into smaller ones. as an example an e-commerce website should be accessing data from a Catalog, Marketing, Sales and Audit databases instead of one big database.

8. Consider regular archiving and cleanup of historical data from all databases.

9. Index defragmenting [2] can improve the SQL execution times too.

10. Sorting of huge record sets might be best left to he done at the application level than at the database level. This could be controversial depending on who you speak to. With this kind of a solution chances are that a modern day beefed-up web server in a web farm(load balanced environment) should be able to handle expensive data operations. This would conserve SQL Server processing time to handle more requests.

11. I hope your issue might have been resolved by now.

I will update this blog, as and when I can think of other ways to help the cause.

Good luck and happy programing !

kick it on DotNetKicks.com

Also find this post on

References:

1. Cost-effective website acceleration

2. Microsoft SQL Server 2000 Index Defragmentation Best Practices

ASP.Net MapPath

leave a comment »


Writing code in  a web application with not access to HttpContext and still need to map to a folder in the website? No problem.. you can use the System.Web.Hosting.HostingEnvironment.MapPath function.


//This is how you would map if you had access to the HttpContext object

System.Web.HttpContext.Current.Server.MapPath("~/App_Data/data.xml");

//and here is how you would map if you do not have access to the HttpContext object

System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/data.xml");

Written by cavemansblog

August 11, 2009 at 4:52 pm

Posted in ASP.Net, Sudheer Reddy Battula

Tagged with

Encrypting the Web.Config configurations

with one comment


The web.config is a file that is not accessible to the user of a web application via a browser. The dotnet framework shields this file from all kinds of external intrusion. However someone having access to the physical file can steal sensitive information.

This vulnerability can be overcome by using aspnet_regiis, an in-build ASP.Net tool.  Following these simple steps, sections of the web.config can be encrypted (and/or decrypted) to secure the configuration settings:

Step 1: Encrypt connection Strings in the web.config. This command uses RSAProtectedConfigurationProvider (uses TripleDES and RSA encryption) for encrypting sections of the web.config file. This command has to be executed in a Visual Studio 2005/2008 command prompt window at the location where the web.config resides:

aspnet_regiis -pef connectionStrings .

Step 2: Adding users to the ACL of the key containers using the –pa parameter. Give access to the ASP.NET user:

aspnet_regiis -pa “NetFrameworkConfigurationKey” “ASPNET”

Step 3: Decrypt Connection Strings to clear text

aspnet_regiis -pdf connectionStrings .

The best part is that, the web application code does not have to change one bit after the encryption of web.config file. Following is a before and after view of the Connection Strings section of a web.config file:

Before:

<connectionStrings>
  <add name="MembershipConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LOCALDB.MDF;Integrated Security=True;User Instance=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>

After:

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
  xmlns="http://www.w3.org/2001/04/xmlenc#">
    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
        <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
          <KeyName>Rsa Key</KeyName>
        </KeyInfo>
        <CipherData>
          <CipherValue>Qnp5eTaECH2wMwlRJjne61lKvEcRfZ3NDeCUhtw+wRKPmmY3Z3iZPBowIZwclU6gjdmY48/dhAEdUCLmzM5iEfHmPoyli+LMW5Yz1pQZM8I7iTFLLgeKux1CgUwP+bUtCpvP66Vu5wL5/veYSiR7kGzu/FgH+8vM6M3SYusHVGQ=</CipherValue>
        </CipherData>
      </EncryptedKey>
    </KeyInfo>
    <CipherData>
      <CipherValue>BPA8J8Vue27wc4COgJ2Pfk9XCe7umTNAKdTJ2WnsyIg/zpEpdL4H6zxE0b5DBFehWTARRAtxfJhNUz4mp+QZvBZ0Iod1utujiXP0+EiWZpY8v31s94lgFhE88cH/CoF/vZgHYyDfedBTtJtUyN1xpegxzHHy38IwDElXUhb3UHP3X3nRWPJ0AohYz8xZZRMzn8MEDJDWCoKErwtMpUbE08AEZHDQSU6ITSn5urNrgS+V2CYYwhY3t1VbA2b+mEQFxVin4bEvOl+O8HGl2kzPtq5rhN60FnbxjPyHwCeHvl8JOzjk/ND8go/w49N61eGrGOH2xFNFPw2mhGVkJD5Lm8nn7xeo30YSS+Ct9uGzLfs=</CipherValue>
    </CipherData>
  </EncryptedData>
</connectionStrings>

Tip: Here is how to secure appsettings

Encrypt appSettings: aspnet_regiis -pef appSettings .

Decrypt appSettings: aspnet_regiis -pdf appSettings .

References:
1. How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA
2. Encrypting configuration files using protected configuration

Factoring the Web.Config

with one comment


Web.Config is a XML document file in a ASP.Net application that is used to store configuration settings related to the web application. The web.config file contains information about database configuration, control module loading, security configuration, session state configuration, Custom configuration and compilation settings. Each web application in ASP.Net inherits their base web.config from the machine’s web.config located in %SystemRoot%\Microsoft.Net\Framework\v#.#.#.#\CONFIG. The applications own web.config is physically located at the root of the web application and sub directories inherit the configuration setting unless they have their own web.config with in the respective sub-directories.

Usually as the application size grows, so does the number of settings and the size of the web.config file. The settings in config file become so many that it will be pretty tough to manage them and also hard to read. There is one less known optional string attribute called “ConfigSource” that specifies the name of the include file in which the associated configuration section is defined, if such a file exists. The configsource attribute was introduced in .NET Framework 2.0 to support external configuration files. This attribute can be added to any configuration section to specify a an external file for that section. Following is an example of a before and after scenario for using the configsource attribute. This example demonstrates how the database connection settings can be separated into an external config file:

Before:

<configuration>
    <configSections>
        <section name="ConnectionStrings" restartOnExternalChanges="false"/>
    </configSections>
    <connectionStrings>
        <add name=“Inventory” connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
.
.
<configuration>

After:

Following are two sections from the web.config and the dbsetting.config files. The dbsettings.config file has the database connection configuration information.

web.config

<configuration>
    <configSections>
        <section name="ConnectionStrings" restartOnExternalChanges="false"/>
    </configSections>
    <connectionStrings configSource="dbsettings.config">

      </connectionStrings>
.
.
<configuration>

dbsettings.config

<configuration>
    <connectionStrings>
        <add name=“Inventory” connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
</configuration>

Example 2

Here is another example of separating the database configuration settings into multiple files. These settings are for Enterprise Library External file configuration source for Application blocks. Following are the sections of the web.config and the entlib.config files into which the database Connection string configuration has been separated:

Web.Config

<enterpriseLibrary.ConfigurationSource selectedSource=“File Configuration Source”>
    <sources>
        <add name=“File Configuration Source” type=“Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=X.X.X.X, Culture=neutral, PublicKeyToken=null” filePath=“entlib.config”/>
    </sources>
</enterpriseLibrary.ConfigurationSource>

entlib.config

<configuration>
    <connectionStrings>
        <add name=“Inventory”connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User  ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
</configuration>

The end result is that this feature of web.config file has been very useful to me so far in keeping the config files neat and simple to read. Also note that the restartOnExternalChanges property is set to false in the “section element for config sections” so that the application does not restart when a change to the external config file is made.

Tip: ConnectionStrings.com has nice list of connection strings for various databases.

References:

1. MSDN Online

Written by cavemansblog

June 4, 2009 at 2:39 pm