TechnicalArchitectureWorx

The (Unofficial) ITWorx Technical Architecture Blog

Archive for the ‘.NET 2.0’ Category

Resolution for (Exception from HRESULT: 0x800736B1)

Posted by archworx on October 17, 2007

I got this error when I tried to use a C++ DLL, compiled by Visual Studio 2005 SP1, in a web application (ASP.Net 2.0):

“This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)”

This happens due to a lot of reasons, you can check them at: http://blogs.msdn.com/eldar/archive/2006/06/07/621501.aspx

The reasons in my case were:

1- The target C++ DLL depends on 3 DLLs: msvcm80.dll, msvcp80.dll, and msvcr80.dll version 8.0.50727.762. This is because the SP1 is installed on the development machine, while the production machine has these files but version 8.0.50727.42, thus, I installed Microsoft Visual C++ 2005 SP1 Redistributable Package (x86) on the production machine. You can get it from: http://www.microsoft.com/downloads/details.aspx?familyid=200B2FD9-AE1A-4A14-984D-389C36F85647&displaylang=en

2- My bad! I forgot to change the DLL’s active configuration to release, so you always have to check that before you deploy your application.

Posted in .NET 2.0, Configuration Management, El-Geish | 29 Comments »

Developing Reusable .NET Components

Posted by archworx on June 10, 2007

Hello All,

I have been developing Custom ASP.NET Server Controls for a while now and I think it is worthwhile to write a series of blog posts about how to develop them. So here we go with the first introductory blog. First, let us think why developing .NET components is important. Well, first of all Microsoft has developed a decent set of components and controls for the .NET framework, but surely these preexisting components don’t fit every scenario that you might need when you are developing your application, so this is why creating new components (or extending existing ones) is needed. More importantly it is the concept of reusability that particularly makes developing such components useful. Identifying certain recurring scenarios and common functionalities and developing a certain component that can be reused by yourself and others without having to re-write the same code again is very valuable. A friend of mine has recently told me that “A good developer only writes code once”. This proverb, albeit an oversimplification, captures the essence of reusability. Great, so what implications does this have on you as a developer? Well, being able to capture repetitive scenarios into reusable code is very important. So next time you write code try to think if you have written this code before (or if you are going to write it again in the future) and if so then you can try to encapsulate it into a component that can be reused (even if this means creating a simple code snippet). This doesn’t only apply to code that you write, but if you are smart enough you can identify recurring patterns inside applications that your organization is developing and hence suggest how reusable components can be used within the company to improve the development process. So, with this introduction over we will turn to talking about Custom Controls in .NET and how they differ from User Controls. But alas, this will have to wait till the next blog.

Posted in .NET 2.0, AFathalla, Programming, Reusable Components | Leave a Comment »

Generating Documentation for .NET assemblies using SandCastle

Posted by archworx on June 4, 2007

I was developing a Control Library for .NET 2.0 and wanted to generate neat looking documentation from XML tags that I had written in code. So I googled around for a bit until I found that you can use a tool called SandCastle to generate HTML documentation that could be transformed into CHM and HxS formats. Here are the steps you have to do:

1- Inside VS2005 right click your project in the Solution Explorer and choose ”Properties”.

2- Click “Build” tab, under “Ouput” make sure that you check the “XML documentation file” checkbox. Once you do so, you can fill in a textbox that specifies the path of where the XML file (that contains the tags you put in code) will be generated.

3- Download and Install HTML Help Workshop .

3- Download and Install SandCastle March 2007 CTP .

4- Download and Install SandCastle Help File Builder (A really cool project on CodePlex that provides a UI that utilizes Sandcastle to generate Help Files)

5- Use SandCastle Help file Builder to generate your documentation .

Voila, you’ve got a cool professional documentation for your .NET project

Posted in .NET 2.0, AFathalla, Documentation, VS 2005 | 5 Comments »

Edit and Continue

Posted by archworx on May 23, 2007

Source: http://msdn2.microsoft.com/en-us/library/bcew296c(VS.80).aspx

Edit and Continue is a time-saving feature that enables you to make changes to your source code while your program is in break mode. When you resume execution of the program by choosing an execution command like Continue or Step, Edit and Continue automatically applies the code changes with some limitations. This allows you to make changes to your code during a debugging session, instead of having to stop, recompile your entire program, and restart the debugging session.

Posted in .NET 2.0, El-Geish, Programming, VS 2005 | Leave a Comment »

F#

Posted by archworx on May 23, 2007

Source: http://research.microsoft.com/fsharp

Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of .NET.

F# is a programming language that provides the much sought-after combination of type safety, performance and scripting, with all the advantages of running on a high-quality, well-supported modern runtime system. F# gives you a combination of

The only language to provide a combination like this is F# (pronounced FSharp) – a scripted/functional/imperative/object-oriented programming language that is a fantastic basis for many practical scientific, engineering and web-based programming tasks. 

F# is a pragmatically-oriented variant of ML that shares a core language with OCaml. F# programs run on top of the .NET Framework. Unlike other scripting languages it executes at or near the speed of C# and C++, making use of the performance that comes through strong typing. Unlike many statically-typed languages it also supports many dynamic language techniques, such as property discovery and reflection where needed. F# includes extensions for working across languages and for object-oriented programming, and it works seamlessly with other .NET programming languages and tools.

For further information, read about F# in more detail, download the F# distribution, read the getting started pages (a guide to installation and running your first program), go to the F# Manual, and learn about the growing F# Community. A short FAQ is also included on this site.

Some Starting points

Posted in .NET 2.0, El-Geish, Programming | 1 Comment »

Anonymous Methods in C#

Posted by archworx on May 23, 2007

In .Net v1.x, declaration of delegate required named methods. C# 2.0 introduced anonymous methods, as MSDN states, “Creating anonymous methods is essentially a way to pass a code block as a delegate parameter.

// Create a handler for a click event
btn.Click += delegate(System.Object o, System.EventArgs e) { MessageBox.Show("Click!"); };

This can be very useful in some cases such as a parameter to the Thread constructor:

new System.Threading.Thread(
  delegate()
  {
      System.Console.WriteLine("Hello");
      System.Console.WriteLine("World");
  }).Start();

Click here for more info.

Posted in .NET 2.0, El-Geish, Programming | 1 Comment »

‘My’ Namespace in VB.Net 2.0

Posted by archworx on May 23, 2007

For rapid development, Visual Basic .Net 2.0 introduced a namespace called My to access all the stuff that belongs to you like computer, network, printers, keyboard, forms, and others.

My Namespace 

For eaxmple, if you need to access the network, you just type: My.Computer.Network.

If My.Computer.Network.IsAvailable Then
    My.Computer.Network.DownloadFile("http://myserver.com/log.txt","c:\archives\serverlog.txt")
End If

Click here for more info.

Posted in .NET 2.0, El-Geish, Programming, VS 2005 | Leave a Comment »

When to use Page.IsValid ?

Posted by archworx on May 15, 2007

In his asp.net blog http://weblogs.asp.net/sibrahim/archive/2003/06/15/8723.aspx, Samer Ibrahim  points out a very important issue regarding Page.IsValid usage in asp.net pages. He quoted the following paragraph from Fritz Onion’s book  ‘Essential asp.net’ :

“As soon as you place a validation control on a page, it is imperative that you check the IsValid flag of the Page class before using any of the data posted by the client.  It is a common misconception that if validation fails on a page, the code for that page will not execute.  On the contrary, the only thing that happens when server-side validation fails is that the IsValid flag of the Page class is set to false, and each validation control that failed renters itself as a visible span so that the error indicator shows up when the page is redisplayed to the user.”

 So if a nice hacker bypasses the javascript validation, the server’s postback code will execute unless you check using safety condition: if(Page.IsValid){//run my code}

Posted in .NET 2.0, MZayet | 3 Comments »

Parsing Word Document in C#

Posted by archworx on May 10, 2007


1) Add a Microsoft Word Object Library (Interop.Word.dll) Reference to your project

2) Save the word document as Xml programmatically (Shown Below)

Word.Application WordApp = new Word.ApplicationClass();

object NullObject = System.Reflection.Missing.Value;

object FalseValue = false;object TrueValue = true;

//Document in Word format

object Format = (object)Word.WdSaveFormat.wdFormatXML;

Word.Document Document;Document = WordApp.Documents.Open(ref DocumentPath, ref NullObject, ref FalseValue, ref NullObject, ref NullObject, ref NullObject,ref TrueValue, ref NullObject, ref NullObject,ref NullObject, ref NullObject, ref FalseValue,ref NullObject, ref NullObject, ref NullObject, ref NullObject);  //Accept all the revisions done on the document first.

Document.AcceptAllRevisions();

//Save document in WordMl Format.

Document.SaveAs(ref XmlPath, ref Format, ref NullObject, ref NullObject, ref NullObject,ref NullObject, ref FalseValue, ref NullObject, ref NullObject, ref NullObject, ref NullObject, ref NullObject, ref NullObject, ref NullObject, ref NullObject, ref NullObject);

//Close Word DocumentDocument.Close(ref TrueValue , ref NullObject, ref NullObject);}

3) Parse the saved XML file using XSLT style sheets

Note that the When the Word Document is saved as Xml the output is in a WordML format.

This is a simple example of a style sheet that gets all the sentences in the document that are preceded by a certain tag, here [#]. This was used for parsing a Requirements Document.

<?xml version=1.0 encoding=UTF-8 standalone=yes ?><xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform xmlns:wx=http://schemas.microsoft.com/office/word/2003/wordml xmlns:w=http://schemas.microsoft.com/office/word/2003/wordml>

<xsl:template match=/><doc><xsl:for-each select=//w:p/w:r>

<xsl:if test=w:t!=”>

<xsl:if test=contains(w:t, ‘[#]’)>

<!– check if the text after the ~ is a requirement by checking that the text is preceeded by a number–>

<xsl:choose>

<xsl:when test=starts-with(translate(w:t/parent::node()/parent::node()/w:pPr/w:listPr/*/@*, ‘0123456789’, ‘9999999999’), ‘9’)> <requirment>

<xsl:choose>

<xsl:when test=not(string-length(translate(w:t,’ ‘,”))>3)>

<reqValue>

<xsl:value-of select=following-sibling::*[1]/>

</reqValue>

</xsl:when>

<xsl:otherwise>

<reqValue>

<xsl:value-of select=w:t/>

</ reqValue>

</xsl:otherwise>

</xsl:choose>

<reqNum>

<xsl:value-of select=parent::node()/w:pPr/w:listPr/*/@*>

</xsl:value-of>

</reqNum>

<!–</xsl:if>–>

<!–in case of a section number, the substring-before function returns an actual number, so it can be converted to a number–><!–whereas in case of text, the whole string returns and it cant be converted to a number, result is NaN–>

</requirment>

</xsl:when>

<xsl:otherwise>

<xsl:if test=starts-with(translate(parent::node()/parent::node()/parent::node()/preceding-sibling::*[1]/w:tc/w:p/w:pPr/w:listPr/*/@*, ‘0123456789’, ‘9999999999’), ‘9’)>

<requirment>

<xsl:choose>

< xsl:when test=not(string-length(translate(w:t,’ ‘,”))>3)>

<reqValue>

<xsl:value-of select=following-sibling::*[1]/>

</reqValue>

</xsl:when>

<xsl:otherwise>

<reqValue>

<xsl:value-of select=w:t/>

</reqValue>

</xsl:otherwise>

</xsl:choose>

<!–<xsl:if test=”starts-with(translate(parent::node()/w:pPr/w:listPr/*/@*, ‘0123456789’, ‘9999999999’), ‘9’)”>–>

<reqNum>

<xsl:value-of select=parent::node()/parent::node()/parent::node()/preceding-sibling::*[1]/w:tc/w:p/w:pPr/w:listPr/*/@*>

</xsl:value-of>

</reqNum>

<!–in case of a section number, the substring-before function returns an actual number, so it can be converted to a number–>

<!– whereas in case of text, the whole string returns and it cant be converted to a number, result is NaN–>

</requirment>

</xsl:if>

</xsl:otherwise>

</xsl:choose>

</xsl:if>

</xsl:if>

</ xsl:for-each></doc></xsl:template></xsl:stylesheet>

4) Link the XSLT style sheet to the XML file saved programmatically

  //The XmlPath is the path of the word document in the XMl formatXPathDocument

XpathDoc = new XPathDocument(XmlPath.ToString());

XslCompiledTransform TransformXml = new XslCompiledTransform();

try

{

DataSet Ds = new DataSet();

//load the style sheet (virtual path)

TransformXml.Load(StyleSheetPath);               

 //The ResultXmlPath is the path of the resultant XMl file after the trasformationXmlTextWriter Writer = new XmlTextWriter(ResultXmlPath, null);

//transform the xml file using xsl sheets:

TransformXml.Transform(XpathDoc, Writer);Writer.Close();

//Read the Resultant Xml file (after style sheet) into a dataset Ds.ReadXml(ResultXmlPath);

log.Debug(“Dataset Count: “ + Ds.Tables[0].Rows.Count);

}

 catch (Exception ex)

{

log.Error(“DocumentToXml : “ + ex.Message);

}

Posted in .NET 2.0, Radwa Nada | 5 Comments »

ASP.NET Black Belt Tips and tricks

Posted by archworx on February 7, 2007

I was fortunate enough to attend this great session in the MDC by the great Patrick Hynds.

Here are some great tips I got:

1. The Page.SetFocus method that is used to set focus on a control. (A cool feature and somehow unknown by many including me 🙂 ).

2. If you want to maintain the scroll position in a page there is the “MaintainScrollPositionOnPostback” property which is here to replace the dreaded “smart navigation” feature in ASP.NET 1.1.

And then there was a really cool part on membership providers, Here are my notes on that:

Membership Providers

 

Why Custom providers?

  • If you need a different schema for the db other than the default schema,
  • If data is in another database MS doesn’t support.(Oracle- Informix).
  • If you need a new feature not implemented in the default provider.

 

Here are a few more ideas:

  • Minimum implementation of Custom provider is Validate User.
  • Location tag in web.config to set zones for anonymous access, others for authenticated users.
  • When inplementing a custom membership provider you will need to implement a custim roles provider as well.

 

Posted in .NET 2.0, MSamy, VS 2005 | 2 Comments »