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.


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


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


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.


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

Also find this post on


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


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


Written by cavemansblog

August 11, 2009 at 4:52 pm

Posted in ASP.Net, Sudheer Reddy Battula

Tagged with