Tuesday, July 12, 2011

Friday, April 30, 2010

very simple tip, most of the time forgotten, is the feature of having page breaks on the schema surface, to organize printing.

Here's a screen cast showing just that (printing a database schema from sql server management studio



Monday, November 02, 2009

Issue : Javascript alert message box showing up twice within an UpdatePanel, after throwing an exception

see also : article on codegain.com about the same subject.

Finally, after a lot of playing around to reproduce what was happening, I found the solution. at first I had the following javascript code :

<script type="text/javascript" language="javascript">

//registers the event by adding it to the endrequests collections of the instance
//This will allow it to actually listen to the event when it happens, and execute
//the code in the onEndRequest function
function pageLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);
}

//function which will execute after the C# code gets to its end
//it will then catch the exception and display it on the alert message box
//the substring is just to avoid having the full exception definition,
//and display only the message
function onEndRequest(sender, args) {
if (args.get_error() != null) {
var msg = args.get_error().message;
alert(msg.substring(msg.indexOf(":", 0) + 1));
args.set_errorHandled(true);
return false;
}
else
{ return false; }
}
</script>


then I had too buttons on the page. everytime a button with no exception is clicked, the next time, the exception shows twice, and keeps incrementing on each postback.



here’s the full code listing for the test asp.net page and C#, with the corrected javascript.



 



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<
title></title>
<
script type="text/javascript" language="javascript">

//registers the event by adding it to the endrequests collections of the instance
//This will allow it to actually listen to the event when it happens, and execute
//the code in the onEndRequest function
function pageLoad() {
Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(onEndRequest); //>>Key line of code for the correction
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);
}

//function which will execute after the C# code gets to its end
//it will then catch the exception and display it on the alert message box
//the substring is just to avoid having the full exception definition,
//and display only the message
function onEndRequest(sender, args) {
if (args.get_error() != null) {
var msg = args.get_error().message;
alert(msg.substring(msg.indexOf(":", 0) + 1));
args.set_errorHandled(true);
return false;
}
else
{ return false; }
}
</script>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
asp:ScriptManager runat="server" ID="script1" />
<
asp:UpdatePanel runat="server" ID="pnlExceptions">
<
ContentTemplate>
<
h4>Exceptions thrown from C#, and displayed in a javascript alert box (works with the update panel)</h4>
<
asp:Button runat="server" ID="txtExceptionInAlert" Text="Trigger Exception" OnClick="txtExceptionInAlert_Click" />
<
asp:Button ID="Button1" runat="server" Text="Asynchronous post back button" />
</
ContentTemplate>
</
asp:UpdatePanel>
</
div>
</
form>
</
body>
</
html>




 



using System;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
///
throwing the exception at the click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void txtExceptionInAlert_Click(object sender, EventArgs e)
{
throw new Exception("This is an exception thrown from C# code");
}
}


 



 



To Reproduce the error, comment this line in the PageLoad : Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(onEndRequest);



the thing, is that in Javascript, I wonder how we can tell it it’s a postback, and not a new pageLoad.



 



Hope this helps !

Sunday, October 05, 2008

codefluent, code generation and destiny :)

At the airport (Casablanca to Paris), I read on a french magazine ("Programmez"), on the cd offered with trial software (one of them was called codefluent.

3 weeks later :
looking for rent in Paris, I get in touch with the owner, we sit an discuss at a coffee place, to find that the owner is a Microsoft France veteran, and has his own company (a Microsoft partner).

we discuss, then agree on the apartment. then on another meeting, we discuss software in detail, to find out that indeed he's the owner of CodeFluent.

I will probably test this code generator, which I'm sure is promising quality when developing .net applications.

The company's name is softfluent, the software is codefluent  

Saturday, June 07, 2008

Development or Business Intelligence

Thinking about the need for Business intelligence, and how close this field has been brought to the developer, it pushed me to dig into it a little, and now, I find myself working with SSIS and SSAS quite a bit, which gave me more flexibility in my conception, and avoid complicating life through coding windows services when SSIS would do a great job;

Also, finally I understand the difference between analytical and transactional databases; Business intelligence principles are important for to understand for many devleopers working in small to medium sized projects, in order to avoid pitfalls such as :

- trying to create dashboards with long running complex queries;

- using C# and windows services, when Sql Server Integration Services could do the job.

- understand where reporting on a transactional database should stop, and switch to an OLAP database

This and a lot more I'm still finding out. Of course, once on a large project, everyone has a specialty, yet, for the last four years, I've been working on small to medium size project (3 roles at ones often), which lead me to work with web development, business intelligence and sql server 2005 administration all at once.

I'll be posting tips on Business Intelligence, that hopefully would be helpful!

here are some useful links for Business Intelligence Videos :

http://www.trainingspot.com

I personnaly have purchased the videos from trainingspot, and they have been very helpful in understanding the concepts, really great!

http://www.learnintegrationservices.com

plannning to check out this integration services videos. not too expensive :)

Regards !

Wednesday, May 28, 2008

A positive look at unit testing

Often we find ourselves under stress trying to deliver a module on time. Some of the first reflexes that come to mind, are to eliminate going through some layers, or simplifying (especially the asp.net layer), and many other reflexes (including myself).

Testing Data Access, Services and Security Layers can be easy, since it really does not require a special context to run on. As for asp.net UI and presentation layer, it's been quite difficult, until the ASP.NET MVC framework preview comes in, with the support for testing the UI layer.

Unit Test layer as a conception layer:
In fact, when we start unit testing, we need to know that testing is at the beginning, during and at the end of development phases. Besides, the unit tests framework is by definition a framework, now, how we use it is totally up to us.
To create a concept for our development of a data access layer, we can use our unit testing framework to

create methods that show us :
                                        1. how we will use the data access layer (opens a conception discussion)
                                        2. It provides us a framework for testing thoroughly our data access layer
                                        3. It also provides us with a controlled manner of building up the solution

Unit Test layer as a documentation layer:
Finally, one of the great benefits (by experience) we went through, is the fact that the unit tests layer can serve as a documentation layer (perfect fit).
In one of the project we worked on, we used the Unit tests layer to train any new team members, to get up and running quickly with using the layer He/she is concerned with. It was a great way to document using code and comments, we could also create a CHM file for it, and also, since it's a collection of unit tests, it works.

A unit tests layer, a CHM documentation file, and making sure tests work, can really be a great way to have a major part of the documentation to train new team members

In conclusion:
As much as the unit tests layer can cost in terms of resources and time, It builds up a great layer for the long term use of applications. Unit tests should be used for many layers, if not, then at least for one or two layers in the application, in order to minimize maintenance cost later on.
Good team work is necessary to the success of good unit tests layer.

Useful links :

ASP.NET MVC , ASP.NET MVC Quick Starts,

Comments are more than welcome

Monday, March 03, 2008

C#: using Timespan to caluculate the total of hours worked based on start and end times, as well as a pause

recently, I came acroos the need to get the total hours in sql. by using the CLR user defined functions freature, I was able to use this function into SQL server. The nice thing, is that I unit tested the function seperately, then used it. example :

SQL:

select dbo.GetTotalHours(TableName.StartTime, TableName.EndTime, TableName.Pause) as TotalHoursWorked;



The function is listed below with comments. also, below is a function for splitting the hour string.




example : GetTotalHours("20:00", "06:00", 120); Important: the pause is expressed in minutes (in this case, it's 120 minutes pause, or 2 hours of pause).







This function, inside an sql server project is created with the sql server attribute of [SqlFunction]. This tells sql server to execute this function
as an extension to sql server, which will make it run as any other sql function.







[Microsoft.SqlServer.Server.SqlFunction]
public
string GetTotalHours(string StartTime, string EndTime, int Pause)
{

if (StartTime == EndTime)
{
if ((StartTime == null) || (EndTime == null) || (StartTime == "00:00") || (EndTime == "00:00"))
{
StartTime = "0:0";
EndTime = "0:0";


return "00:00";
}
}
//this split gives the following example StartTime 10:30 Start[0] gives 10 and start[1] gives 30
string[] Start = this.SplitTimeString(StartTime);
string[] End = this.SplitTimeString(StartTime);


//timespan for the taking into consideration intervals similar to :
//starttime: 20:00 and endtime 01:30
TimeSpan tsTwentyFourHours = new TimeSpan(24, 0, 0);
//start time
TimeSpan tsStart = new TimeSpan(int.Parse(Start[0]), int.Parse(Start[1]), 0);
//end time
TimeSpan tsEnd = new TimeSpan(int.Parse(End[0]), int.Parse(End[1]), 0);

TimeSpan tsPause = new TimeSpan(0, Pause, 0);
TimeSpan tsHoursWorked;

if (int.Parse(End[0]) < int.Parse(Start[0]))
{
//Case when the it's overnight work like 20:00 to 01:30

//we substract start time from twenty for hours, then add the end time.
//this gives us the correct measures
tsHoursWorked = tsTwentyFourHours.Subtract(tsStart).Add(tsEnd);
//substratct the pause
tsHoursWorked = tsHoursWorked.Subtract(tsPause);
}
else
{
//normal hours example: 08:00 to 20:00
//we simply substract the start time plus the pause from the end time
tsHoursWorked = tsEnd.Subtract(tsStart.Add(tsPause));
}


return tsHoursWorked.TotalHours.ToString() + ":" + tsHoursWorked.TotalMinutes.ToString();
}





This function is used in the function above.







public string[] SplitTimeString(string Time)
{
char[] ch = new char[] { ':', '.' };
return Time.Split(ch);
}




in the next article, will see how this function can be used upgraded to be used in sql server 2008, taking advantage of its Time datatype, using Visual Studio 2010 beta version.

Comments are welcome !