Monday, 29 March 2010

The infamous EntityConnection error:

 

Hi Friends and neighbours ,

I encountered the infamous EntityConnection errror, “The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid....”
while unit testing one of  the business component for the solution that I am working on. This particular business component uses an Entity object.

if you have used the The ADO.NET Entity Framework  you know that you need to create the entity data model. When the entity data model or EDM is created using Visual Studio, in this case Visual Studio 2008, a file called “app.config” is also created. This file is created on the same level or should we say next to the EDM file.

project_setup


The app.config file contain the details of the database connection. Since my VS solution is broken up on different layers, one project for each layer, the EDM and app.config file were created on the Data Access layer Project hence my unit tests classes, located on the Test project level, were not able to find the connection details.

The solution : I simply made a copy of the app.config  file available to my Test business project.

TestBusiness

Now if you break up your solution into different projects, with each project representing a layer of your application, don’t forget to either, make a copy of the app.cofnig file available to your ASP NET project or alternative modify your web.config file with the connection string properties found in the app.config file.

Until next time,

“Don’t underestimate the power of The Dark Side”

Wednesday, 24 March 2010

ASP NET group validation

Hi Boy and Girls,

Group validation is a very handy feature of ASP NET. It let you use one validation for a related group of controls,

The scenario:

Let’s say we want to display a list of items that have been updated within a given period range.


text_boxes_control

We want our users to pick a date range, start and end date, and based on the dates picked our application will show the data.  On our UI We have provided one date picker for each text box.

date_picker 

Our application must handle, gracefully,
’abnormal’ scenarios.

Even that our above control has two date pickers, we must handle abnormal scenarios. On this simple example we can see two scenarios that our application must handle:

1. User forgets to input the date on one or both text boxes fields.
2. User doesn’t use the data picker provided by us but instead input the date directly

By looking on these two scenarios we can see that we need two validations for each input box.

ASP NET provide us with “Group Validation”. it is very easy to implement. Basically it is a  3 steps process.


1.  We add the property ‘ValidationGroup’ to each text box. As a value for this property We can use something like “TextBoxesGroup” so we end up with something like this:


<asp:TextBox ID="txtEndDate" ValidationGroup="TextBoxes" runat="server" />

2. We add our ASP NET validator components; Remember we need two of them: one to make our fields ‘required’ AKA compulsory and the other for enforcing the right date format.

So  we will add the <<asp:RequiredFieldValidator/> and the <asp:RegularExpressionValidator>components to our user control.

3. We add the property ValidationGroup to each of our validatos.  As a value for the ValidationGroup property we must specify the value that we used for the text boxes property ValidationGroup.

<asp:RequiredFieldValidator ID="RequiredValidator” runat="server" ValidationGroup="TextBoxes"
      ControlToValidate="txtStartDate"  ErrorMessage="your error message”" />

<asp:RegularExpressionValidator  ID="DateRegularExpressionValidator" ValidationGroup="TextBoxes"
        ValidationExpression="your RegEx expressions"
ControlToValidate="txtStartDate"   ErrorMessage="You error message" runat="server" />

4. We add the property ValidationGroup="TextBoxes" to the submit button.We will need this so our SearchProductsButton only validate the controls grouped under the TextBoxes validation group.

<asp:Button ID="SearchProductsButton" runat="server" OnClick="Search_Click" Text="Search Products" ValidationGroup="TextBoxes" />


Other solutions:

1.We could have built our validation on the server code, otherwise known as ‘code behind. This solution will cause the validation to be run on the server side, generating extra round trips.

2. Make our text boxes read-only. If we make our text boxes read only we will still able to populate them using JavaScript, however, If you try to retrieve the text box values, from the code that is tied to the user control or page, you will see only emptiness. Well not emptiness; You will see an empty string for each text box control value.

I tested my solution using IE 8 and Chrome and it worked on both of them.

If some one was able to retrieve the values of  read-only text boxes, from the code behind, let me know. :

-)

   Don’t underestimate the power of the dark side.