Muhanad YOUNIS

Parent – Child in recursive data table with LINQ

If you have a data table which looks like this one below and holds child,parent rows at the same table;

ID ParentID Name
guid1 null parent 1
guid2 guid1 child for parent 1
so on so on so on

and you wont to retrieve all records from the table in a table looks like below;

Parent Childs
parent 1 child 1 for parent 1
child 2 for parent 1
child 3 for parent 1
….
Parent 2 child 1 for parent 2
child 2 for parent 2
….

This means that i have to make a recursive query in Sql to retrieve it this way. but with LINQ its more easy to be done, see the query below;

   1:  var q=  from p in TypedDataTable
   2:      where p.ParentID == null  // well get all parents
   3:      select new 
   4:       {
   5:             ParentID = p.ParentID,
   6:            child =  from c in TypedDataTable 
   7:                      where c.ParentID == p.ID select
                           new {ChildID=c.ID,
   8:                          ParentID = c.ParentID}
   9:      };
  10:          

and by the query above you’ll get this result;

linqParentChild 

 NOTICE : this query will load one level at a time

hope this helps

Posted: Nov 07 2009, 04:19 PM by mohi88 | with 5 comment(s)
Filed under: ,
Using 2 Tables Joined with LINQ as data source without anonymous cast error in databond method

Yesterday one of our project team member faced a challenge of using an anonymous data that is returned from joining 2 typed data tables with LINQ. The problem is not how to use the data, the problem was how to be able to cast and use the data in Repeater ItemDataBond method without having “<>f__AnonymousType0….” cast error. below is the join query (tables used are typed) :-

   1:PagedDataSource objPDS = new PagedDataSource();
   2:objPDS.AllowPaging = true;
   3:objPDS.PageSize = 10;
   4:   
   5:objPDS.DataSource = (from p in Table1
   6:                join  d in Table2 on p.ID equals d.ID
   7:                               select new
   8:                               {   
   9:                                   p,
  10:                                   F1= d.f1,
  11:                                   F2= d.f2,
  12:                                   F3= d.f3,
  13:                                   F4= d.f4,
  14:                                 }).ToList();

The code above will return rows with anonymous type that will include fields wanted for table2 and all table1 fields!.
NOTACE : to bind this data to repeater use <%# Eval("F1”) %> for Table2 fields and <%# Eval("p.FieldName”) %> for Table1 Fields.

Now what will happen if we want to use the datarow data bonded to repeater row! the code below will show you that you can not cast that datasource in ItemDataBond method as DataRowView :-

   1:  var bindedRow= e.Item.DataItem as DataRowView; 
   2:  //bindedRow return null value

So how to use a value inside the bonded row! after some researches I found out that reflection must be used to take that value from e.Item.DataItem with anonymous data. Reflection can be done by using DataBind.Eval which will evaluate data at run time. A label added to repeater and we want to bond some data at itemdatabond time, the code below will demonstrate that :- (Sorry for using multiline in code)

   1:  label1.Text = Table3.
   2:  Where(cid => cid.ID == 
           (Guid)DataBinder.
Eval(e.Item.DataItem, "p.FieldName")).
   3:  First().ToString();

DataBind.Eval Done the trick for you and retrieve wonted data from the anonymous type and casting it to needed type (here its guid).

Hope this Helps

Posted: Nov 04 2009, 10:19 AM by mohi88 | with no comments
Filed under: , , ,
Where with dynamic parameter (linq)

Yesterday i faced a satiation that i need to make a search in a datatable with dynamic parameter ( i mean parameters that may change - not the type of the parameter –). So i had this table below

ID                   appID               condition
---------           ---------             ------------
1                       A                          25
2                       A                          35
3                       D                          35
4                       C                          25
5                       D                          45
6                       A                          15

 

and i was looking to do this
for example : i would like to know the applications that have conditions 15,25 and 35 so that will be A and C
or 45 , 25 and that will be C and D.

and after some code refactoring i found the best way to code this is as below;

var appIDKeys= new List<Guid>();  
                   listOfConditions.ForEach(a =>  
                        {  
                            var q = (from p in MainTable  
                                     where p.ConditionKey== new Guid(a)  
                                     select p.AppID).ToList();  
                               if (q.Count > 0)  
                                     {  
                                        if (appIDKeys.Count == 0)  
                                            {  
                                              appIDKeys= q;  
                                            }  
                                 appIDKeys= appIDKeys.Intersect(q).ToList<Guid>();  
                            }  
                  else
                            { 
                               throw new ApplicationException("No Match");  
                            }  
                      });

 

Hope This helps

Posted: Oct 20 2009, 11:03 AM by mohi88 | with 2 comment(s) |
Filed under: ,
TDD Masterclass By Roy Osherove

Roy Osherove is giving an hands-on TDD Masterclass in the UK, September 21-25. Roy is author of "The Art of Unit Testing" (http://www.artofunittesting.com/), a leading tdd & unit testing book; he maintains a blog at http://iserializable.com (which amoung other things has critiqued tests written by Microsoft for asp.net MVC - check out the testreviews category) and has recently been on the Scott Hanselman podcast (http://bit.ly/psgYO) where he educated Scott on best practices in Unit Testing techniques. For a further insight into Roy's style, be sure to also check out Roy's talk at the recent Norwegian Developer's Conference (http://bit.ly/NuJVa). 

Full Details here: http://bbits.co.uk/tddmasterclass

bbits are holding a raffle for a free ticket for the event. To be eligible to win the ticket (worth £2395!) you MUST paste this text, including all links, into your blog and email Ian@bbits.co.uk with the url to the blog entry.  The draw will be made on September 1st and the winner informed by email and on bbits.co.uk/blog

Posted: Aug 25 2009, 08:36 AM by mohi88 | with no comments
Filed under: ,
Nobody Hates Software More Than Software Developers

I know that i did not write a tiny word since a long time!. all that because we have a new member on our family and i have to give her more attention. During my standby time i had some time to read some books and developers blogs.I red a very nice post by Jeff Atwood and want to share it with you Nobody Hates Software More Than Software Developers.

Posted: Jul 23 2009, 09:11 AM by mohi88 | with no comments
Filed under:
“What’s the biggest challenge with task estimates?”

Read this article from Eric Brechner about task estimation

I would estimate 

I believe it will help ;)

Global Timer (Background Timer)

Updated on 01.19.2009

Why do you need a Global Timer ?
There are many reason that you might want to use a global timer for, like pulling data from you database on time periods to prevent user from displaying empty data. I used the global timer to get signed up  online users data ( like online user count, cities and assigned rules).

there are 3 types of timer class in ASP.NET;

Here i will not discus the deference between the above timer class (you may check the links and read msdn about them)but I'll show how to use System.Threading.Timer to create a background timer.

the time that I’ll create in this article will be created once and will keep working as long as my web application keep working. If the application stopped for any reason the timer will start again with the application restart.

First of all create a class that will star Threading.Timer, make sure to create the timer starter method as Static method because we’ll call that method from Global.asax applicathin start method. Make the class as below;

Add using System.Threading; as reference;

   1:  public class TimerStarter
   2:  {
   3:      private static Timer threadingTimer;
   4:   
   5:      public static void StartTimer()
   6:      {
   7:          if (null == threadingTimer)
   8:          {
   9:              threadingTimer = new Timer(new TimerCallback(CheckData),
                                           HttpContext.Current, 0, 600000);
  10:          }
  11:      }
  12:      private static void CheckData(object sender)
  13:      {
  14:          //ToDo check Data
  15:      }
  16:  }

Now Let me explain the code above; I declared a threadingTimer but did not initialize it. The main method here is StartTimer; Its a static method. The method checks if the threadingTimer Object is null or not. If its the first time that the method is called the threadingTimer will be null and will be created. line 9 creates and sets the timer object. TimerCallback is the delegate that will be called within timer intervals, here I call CheckData Method. CheckData method must be overloaded to match TimerCallback delegate. HttpContext.Curent is the object that will be used by the callback method; if you do not need any extra information for your method you can pass it as null. the 0 (zero) force the threadingtimer to call CheckData method as soon as the timer is created. You may set any milliseconds you want the timer to wait and call CheckData method. 600000 tells the timer to call CheckData method every 10 min. Call your timer from Global.asax as below;

   1:      void Application_Start(object sender, EventArgs e) 
   2:      {
   3:          TimerStarter.StartTimer();
   4:      }

 

UPDATE : When your background thread throw and exception the working process will shout down! and IIS will recycle and start again so be careful.

 

That is all!

You may use System.Timers.Timer in a deferent way to achieve the same goal. Check MSDN for more details.

Hope this Helps

ScriptManager and MasterPage PageMethods !

I know that this subject has been asked many times therefore i would like to summarize it and give a small tutorial about how to do it.

I had some PageMethods on a default page which calls some web methods on code behind of the page. Before 1 week we decided to change the old structure of the project UI to use MasterPage. MaterPage triggered many problems with it, one of these problems that MasterPage does not support JS PageMethods! because MasterPage does not inherit from Web.UI.Page therefore you can not call PageMethods (its not a page!) – you can not call pagemethods on usercontrols too – so handle this problem and call your methods you can try this tutorial;

  • Create a MasterPage and add a ScriptManager on page.
  • On ScriptManager add the folowings
       1:  <asp:ScriptManager ID="ScriptManager" runat="server"
                   EnableScriptGlobalization="true"
       2:          LoadScriptsBeforeUI="true" 
    EnableScriptLocalization="true"
    EnablePageMethods="true">
       3:         <Scripts>
       4:              <asp:ScriptReference 
    Path="~/Javascript/MasterPageWSJS.js" />
       5:          </Scripts>
       6:          <Services>
       7:              <asp:ServiceReference 
                           Path="~/WebServices/MasterPageWS.asmx" />
       8:          </Services>
       9:      </asp:ScriptManager>

        Here we have 2 important sections
                   - Scritps which includes our JS file location
                   - Sevices which includes our Webservices location
       Here to be mentioned that EnablePageMethods attribute means nothing on MasterPages!.

  • Add a javascript file to the project ( here its  MasterPageWSJS.js)
       1:  function CallService() {
       2:  //CallFromMasterJS() is the name of the service method
       3:            MasterPageWS.CallFromMasterJS();
       4:    }

  • Add a Webservice file to the project (here it is MasterPageWS.asmx)
       1:  <%@ WebService Language="C#" Class="MasterPageWS" %>
       2:   
       3:  using System;
       4:  using System.Web;
       5:  using System.Web.Services;
       6:  using System.Web.Services.Protocols;
       7:  using System.Web.Script.Services;
       8:   
       9:  [WebService(Namespace = "http://tempuri.org/")]
      10:  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      11:  [ScriptService]
      12:  public class MasterPageWS : System.Web.Services.WebService
      13:  {
      14:   
      15:      [WebMethod(EnableSession = true)]
      16:      public void CallFromMasterJS()
      17:      {
      18:   // todo: write the needed codes
      19:      }
      20:  }

      

Here some important notes about the service:

- decorate the service class with [ScriptService]

- Decorate your methods with [WebMethod] add if you want to use

session variables with it decorate it with

[WebMethod(EnableSession = true)] because webservices are

stateless by default

  • at the end add this code lines to your MasterPage codebehind:
       1:  if(!IsPostBack)
       2:  {
       3:  // masterBody is the ID of the masterpage body html tag       
            HtmlGenericControl body =
          (HtmlGenericControl)Page.Master.FindControl("masterBody");
       4:          body.Attributes.Add("onunload", "CallService();");
       5:  }

That is all !. when you start your page and refresh it the master page will unloaded and that will fire the event onunload on the page body which will call the JS and from there the web service will be called.

Hope this helps

this tutorial based on Calling Web Services from Client Script in ASP.NET AJAX


Publish Server Performance Monitors with MsChart

In this article I'll try to show you how to to use MsChart Controls with ajax update panel to publish your server performance counter variables.

Be sure to install these packages before your start;

MsChart Controls are free and new, I used to use owc 11 and later versions to achieve needed works on projects.

After you install these packages goon and create a new website in your visual studio. On vs2008 toolbox you’ll find some new controls which installed with MsChart package. You’ll find chart control under data tab. First add a ajax script manager and an update panel to your page. Inside the update panel drop a timer and set its interval to 3000 milliseconds (3 seconds). Drag and drop a chart control for toolbox to your update panel content section. You will have a chart section looks like below;

<asp:Chart ID="Chart2" runat="server">
        <Series>
            <asp:Series Name="Series1">
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </ChartAreas>
</asp:Chart>

Here you can see that the chart control have got to main section Series and ChartArea, I’ll not discus these two section widely; you may have more information about chart control and its section from Microsoft Chart Controls for .NET Framework Documentation. the series section is related with graph it self and the chartarea section is related with chart area that will hold hold the drawn graph. In my project I configured chart section as below;

<asp:Chart ID="Chart1" runat="server" Height="592px" Width="412px"
ImageUrl="~/TempImages/ChartPic_#SEQ(300,3)"

BackColor="#D3DFF0" BorderLineStyle="Solid"
BackGradientEndColor="White" BackGradientType="TopBottom"
BorderlineColor="DarkGray" EnableViewState="true"
BackImageTransparentColor="White"
BackSecondaryColor="White" BorderlineDashStyle="Solid">

 

Do not change ImageUrl location!.

for Series;

<Series>
     <asp:Series Name="MySeries" XValueType="Double" ChartType="Spline"
BorderColor="180, 26, 59, 105"
YValueType="Double" BorderWidth="2" 
ShadowColor="254, 0, 0, 0" 
ShadowOffset="1"
 ChartArea="MyChartArea">
                            <Points>
                                <asp:DataPoint XValue="0" YValues="0" />
                            </Points>
                        </asp:Series>
  <asp:Series XValueType="Double"
 Name="Exceps Thrown"
 ChartType="Spline" 
BorderColor="180, 26, 59, 105"
YValueType="Double" 
BorderWidth="2" 
ShadowColor="254, 0, 0, 0" ShadowOffset="1"
ChartArea="Default" IsVisibleInLegend="true">
    <Points>
          <asp:DataPoint XValue="0" YValues="0" />
    </Points>
  </asp:Series>
</Series>

Here I used 2 series which mean that I’ll have to graphs in my chart. You can see that I used two different  chartareas that is because I’ll use two chart areas on the same chart control.There many charttypes that you can select depending on your project demands, in my example it choose Spline. Beside that I added a Points section which will give the graph a starting position ( in static chart and charts with out code behind you can add many datapoints to draw the graph needed).

and for Chart Area;

 <ChartAreas>
    <asp:ChartArea Name="Default" BorderColor="64, 64, 64, 64" 
BorderDashStyle="Solid"
     BackSecondaryColor="White" BackColor="64, 165, 191, 228" 
ShadowColor="Transparent"
     BackGradientStyle="TopBottom" Area3DStyle-Enable3D="true" 
Area3DStyle-PointDepth="1000"
      Area3DStyle-PointGapDepth="200" AlignmentOrientation="Horizontal">
          <Position Y="5" Height="50" Width="94" X="0"></Position>
                <AxisY LineColor="64, 64, 64, 64">
                     <MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>

<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold">
</
LabelStyle>

                            </AxisY>

<AxisX LineColor="64, 64, 64, 64"
IsMarginVisible="False" Maximum="100" Minimum="0"

                                IsStartedFromZero="False">
                                <MajorGrid LineColor="64, 64, 64, 64">
</
MajorGrid>
                                <LabelStyle Font="Trebuchet MS, 8.25pt, 
style=Bold"
></LabelStyle>
                            </AxisX>
                        </asp:ChartArea>
   <asp:ChartArea Name="MyChartArea" BorderColor="64, 64, 64, 64" 
BorderDashStyle="Solid"
       BackSecondaryColor="White" BackColor="64, 165, 191, 228" 
ShadowColor
="Transparent"
       BackGradientStyle="TopBottom" Area3DStyle-Enable3D="true" 
Area3DStyle-PointDepth="1000"
       Area3DStyle-PointGapDepth="200" AlignmentOrientation="Horizontal">
              <Position Y="50" Height="45" Width="94" X="0"></Position>
                 <Area3DStyle Enable3D="true" PointDepth="1000"
PointGapDepth="200" />
                     <AxisY LineColor="64, 64, 64, 64">
                        <MajorGrid LineColor="64, 64, 64, 64"></MajorGrid>
                                <LabelStyle Font="Trebuchet MS, 8.25pt, 
style=Bold"
></LabelStyle>
                            </AxisY>
                            <AxisX LineColor="64, 64, 64, 64" 
IsMarginVisible="False" Maximum="100" Minimum="0"
                                IsStartedFromZero="False">
                                <MajorGrid LineColor="64, 64, 64, 64">
</
MajorGrid>
                                <LabelStyle Font="Trebuchet MS, 8.25pt, 
style=Bold"
></LabelStyle>
                            </AxisX>
                        </asp:ChartArea>
                    </ChartAreas>

 

Points of intrest in ChartAreas are Position, AxisX and AxisY section. In position section you give the locations of your chartarea over the chart control it self. Since I have two chartareas I replaced them under each other. AxisX and AxisY are used to configure your graph axes; even if you don’t configure this section you can still use your chart but you’ll have some graphical problems until you data is sent to chart and display start. after all that configuration the chart control will look as below ( you may download the project from the link in the bottom of the article )

   

Now let us go to code behind and see how to send our performance monitors values to chart.

on Timer tick event I replaced 2 methods; the first one will set the performance monitors and the other will fill the chart as below;
(Do not forget to add using System.Web.UI.DataVisualization as a reference)

   1:  protected void Unnamed1_Tick(object sender, EventArgs e)
   2:          {
   3:              StartMonitor();
   4:              ChartCondfig();
   5:           }

for StartMonitor method;

 private void StartMonitor()
        {
 
   pc1 = new PerformanceCounter(".NET CLR Exceptions", "# of Exceps Thrown", 
"_Global_");
  // get the current value
  float dResult1 = pc1.NextValue();
 
  pc2 = new PerformanceCounter("ASP.NET", "Application Restarts", "");
   float dResult2 = pc2.NextValue();
 
  pc3 = new PerformanceCounter("ASP.NET", "Requests Current", "");
 float dResult3 = pc3.NextValue();
 
   pc4 = new PerformanceCounter("ASP.NET", "Requests Queued", "");
   float dResult4 = pc4.NextValue();
 
   pc5 = new PerformanceCounter("ASP.NET", "Worker Process Restarts", "");
   float dResult5 = pc5.NextValue();
 
  pc6 = new PerformanceCounter("ASP.NET", "Request Execution Time", "");
  float dResult6 = pc6.NextValue();
 
   pc10 = new PerformanceCounter("Memory", "Available MBytes", "");
  float dResult10 = pc10.NextValue();
 
        }
 

Here pc1 – 10 are declared on page load. and you can add much performance counter as you want. In this example I selected pc1 and 10 to be shown in the graph.

For ChartCondfig method; (sorry the code is not well formatted)

 
 
   1:  private void ChartCondfig()
   2:          {
   3:              double lastXValue =
   4:           this.Chart1.Series[0].Points[this.Chart1.Series[0]
.Points.Count - 1].XValue + 1;
   5:              this.Chart1.Series[0].Points.AddXY(lastXValue++, 
                  Convert.ToDouble(pc1.NextValue()));
   6:   
   7:              double lastXValue1 = this.Chart1.Series["MySeries"]
       .Points[this.Chart1.Series["MySeries"].Points.Count - 1].XValue + 1;
   8:              this.Chart1.Series[1].Points.AddXY(lastXValue1++, 
Convert.ToDouble(pc10.NextValue()));
   9:   
  10:              // Adjust categorical scale
  11:              double axisMinimum = this.Chart1.Series[0]
                 .Points[0].XValue;
  12:              this.Chart1.ChartAreas[0].AxisX.Minimum = axisMinimum;
  13:              this.Chart1.ChartAreas[0].AxisX.Maximum =
                                         axisMinimum + 100;
  14:   
  15:              this.Chart1.ChartAreas[1].AxisX.Minimum = axisMinimum;
  16:              this.Chart1.ChartAreas[1].AxisX.Maximum =
axisMinimum + 100;
  17:   
  18:              // Remove points from the left chart side if number of 
points exceeds 100.
  19:              while (this.Chart1.Series[0].Points.Count > 100)
  20:              {
  21:                  // Remove series points
  22:                  foreach (Series series in this.Chart1.Series)
  23:                  {
  24:                      series.Points.RemoveAt(0);
  25:                  }
  26:              }
  27:          }

on line 3 a double value named lastXvalue is declared this variable will hold the last point position over the X axes. on line 5 our first series on the chart will add a new point on X axes and Y axes.For X axes ,increasing  lastXvalue variable will be enough and for Y axes point performance monitor current value is given.

   5:  this.Chart1.Series[0].Points.AddXY(lastXValue++, 
       Convert.ToDouble(pc1.NextValue()));

 

Adjusting the categorical scale of the chartarea is important; otherwise win your lastXValue reaches the end of the scale you’ll have a problem, there for on line 16 we tell the chart to extend the scale. From line 19 to 26 I removed the points from series after the left side of the chart exceeds 100.

If you are debugging your project you well receive no exception but on runtime a small exception will rise; your application will look for a folder named TempImageFiles; goon and create this folder on the location shown in the exception and your application will run.

When you run the project you’ll have your chart as below;

To make with example work on your server and other computers you need to install the Download the free Microsoft Chart Controls package.

That's all. In this article I tried to explain how to use MsChart to publish you server performance monitoring values. You can change and play around with charttypes and do many other nice works with that.

Download the sample project from here

For more information about MsChart you can check these links;

This article is based on Microsoft Chart Controls Web Samples project ajax section.

MIX09 10K Smart Coding Challenge

For Smart Coding and Smart Coders !

http://2009.visitmix.com/MIXtify/TenKGallery.aspx

More Posts Next page »