The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase’


While I was working with the Open Office XML SDK 2.0, after I added the reference to the DocumentFormat.OpenXml assembly and tried to build the application I ran into this error message.

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’

Turns out, all you need to do to fix it is to just add another reference to WindowsBase assembly that is found in the .NET Tab.

Could not execute CVTRES.EXE


Today I started woking on a windows application at home and I was very annoyed by the error I was getting whenever I tried to debug/build the application that said: “Could not execute CVTRES.EXE

Tried lots of things like giving full rights to “everyone”  on the debug folder and some other silly things, untill I found out that there was something in ZoneAlarm that was causing the problem. I had to uninstall and re-install ZoneAlarm. Turning it off was not sufficient. I never understood what was causing the problem at that time and to be honest I’m not too concerned with that either. May be there was some setting that needed a reset or something. But anyway like they say: “All is well that ends well”!

Hope some of you might find this useful someday.

How to use OrderBy() for multiple columns in EntitySpaces?


You can pass multiple columns in like this:-

collection.Query.OrderBy(collection.Query.ProductID.Descending, collection.Query.CategoryID.Ascending)

How to manipulate Session in Http Handlers


There is a common issue that you can’t manipulate session objects in http handlers, the files with ASHX extension 🙂 Well normally you can read the contents in the session via context.Session but you can add/remove/modify that in there. So how do you do this:

The answer is simple: Just implement the IRequiresSessionState interface. It doesnt have any method to implement so you dont have to worry about any details, this just notifies the runtime that Session can be manipulated in this context. So your class declaration would look something like this:-

public class SetLabels : IHttpHandler, IRequiresSessionState
{

How to get the file name from a full file path?


If you have a full file path like D:\My Documents\myimg.jpg and you want to get just the file name myimg.jpg, you can get it with the help of the Path class in this fashion:-

In C#

string filename = Path.GetFileName(“D:\My Documents\myimg.jpg“);
In VB .NET

Dim fileName as String = Path.GetFIleName(“D:\My Documents\myimg.jpg”)

A lost bet — Response.BinaryWrite Spoils the day!


Two days ago I lost a bet. My friend Jubair was generating a bar code image and he said how can I get it to display on the page without saving it as a file and giving its source to the image tag. I said you can’t do it because I thought it would be possible in desktop applications only. So we made a deal that if I lose I will write a blog entry for him. So here it is. Anyways, he did it in a manner similar to this one.

dim omemorystream as new memorystream()
img1.save(omemorystream,imagetype)
Response.binarywrite(omemorystream.Buffer)

How to generate Random numbers in .NET


Actually these are not exactly ‘true’ random numbers. These are actually pseudo-random numbers, which means that they give you an illusion that they’re being generated randomly.

It’s very simple: There’s a class called System.Random. and the rest is as simple as this:-

Random r = new Random(0);
int num = r.Next(0,999999);

in VB .NET

dim r as Random = new Random(0)
dim num as Int = r.Next(0,999999)

How to Add Shaded Rows to ListView’s Details View


Sometimes it can be challenging to read the Details view in a ListView, especially if the rows are long. This article shows how to add shading to every second row to make a ListView easier to read.

As you may know, you can alter the appearance of individual ListViewItem’s such as the Font and BackColor. But these values are ignored unless you set the item’s UseItemStyleForSubItems property to true.

The following code demonstrates how to shade every other row in a ListView:

ListView listView = this.ListView_Products;

listView.View = View.Details;

int i = 0;

Color shaded = Color.FromArgb( 240, 240, 240 );

foreach (Product product in products)

{

ListViewItem item = new ListViewItem( product.Name );

item.SubItems.Add( product.Version );

item.SubItems.Add( product.Description );

item.SubItems.Add( product.Status );

if (i++ % 2 == 1)

 {

item.BackColor = shaded;

item.UseItemStyleForSubItems = true;

}

listView.Items.Add( item );

}


VB .NET Code:-

Dim listView As ListView = Me.ListView_Products

listView.View = View.Details

Dim i As Integer = 0

Dim shaded As Color = Color.FromArgb(240, 240, 240)

For Each product As Product In products

    Dim item As New ListViewItem(product.Name)

    item.SubItems.Add(product.Version)

    item.SubItems.Add(product.Description)

    item.SubItems.Add(product.Status)

    If System.Math.Max(System.Threading.Interlocked.Increment(i),i – 1) Mod 2 = 1 Then

        item.BackColor = shaded

        item.UseItemStyleForSubItems = True

    End If

    listView.Items.Add(item)

Next

Properties in .NET


Property in C#:

Property ? it is a special method that can return a current object?s state or set it. Simple syntax of properties can see in the following example:

public int Old 
{
   get {return m_old;}
   set {m_old = value;}
 } 
public string Name 
{
   get {return m_name;} 
}

Here are two types of properties. A first one can set or get field of class named m_old, and the second is read only. That?s mean it can only get current object?s state.

The significance of these properties is its usability. These properties need not be called with any function names like objectname.get or objectname.set etc., But they can be directly assigned the values or retrieve the values.

The VB .NET Syntax of declaring a property is as follows:-

Public Property X() As Integer
Get
     Return
x
End
Get
Set(ByVal Value As Integer
)
     x = value
End
Set
End Property

How to render a page’s HTML?


I’ve been trying to get the Html of a gridview and in the process I found the way to get the Html of an aspx page @ Microsoft .NET Framework Group at Google. I’ve tested the code and now I’m trying to modify it to get the HTML of a gridview. Wish me luck!

 

<%@ Page Language=”VB”  %>

<script runat=”server”>

    Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
        Const OUTPUT_FILENAME As String = “renderedpage.html”
        Dim renderedOutput As StringBuilder = Nothing
        Dim strWriter As IO.StringWriter = Nothing
        Dim tWriter As HtmlTextWriter = Nothing
        Dim outputStream As IO.FileStream = Nothing
        Dim sWriter As IO.StreamWriter = Nothing
        Dim filename As String
        Dim nextPage As String

        Try
            ‘create a HtmlTextWriter to use for rendering the page
            renderedOutput = New StringBuilder
            strWriter = New IO.StringWriter(renderedOutput)
            tWriter = New HtmlTextWriter(strWriter)

            MyBase.Render(tWriter)

            ‘save the rendered output to a file
            filename = Server.MapPath(“.”) & “\” & OUTPUT_FILENAME
            outputStream = New IO.FileStream(filename, _
                                          IO.FileMode.Create)
            sWriter = New IO.StreamWriter(outputStream)
            sWriter.Write(renderedOutput.ToString())
            sWriter.Flush()

            ‘ redirect to another page
            ‘  NOTE: Continuing with the display of this page will result in the
            ‘       page being rendered a second time which will cause an exception
            ‘        to be thrown
            nextPage = “DisplayMessage.aspx?” & _
                       “PageHeader=Information” & “&” & _
                       “Message1=HTML Output Saved To ” & OUTPUT_FILENAME
             ‘Response.Redirect(nextPage)
            ‘Response.Write(renderedOutput.ToString())

            writer.Write(renderedOutput.ToString())
        Finally

            ‘clean up
            If (Not IsNothing(outputStream)) Then
                outputStream.Close()
            End If

            If (Not IsNothing(tWriter)) Then
                tWriter.Close()
            End If

            If (Not IsNothing(strWriter)) Then
                strWriter.Close()
            End If
        End Try
    End Sub

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1″ runat=”server”>
    <title>Capture Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:dropdownlist id=”DropDownList1″ runat=”server”>
            <asp:listitem>red</asp:listitem>
            <asp:listitem>blue</asp:listitem>
            <asp:listitem>green</asp:listitem>
        </asp:dropdownlist></div>
    </form>
</body>
</html>

 

———————————————————————————-

C# Lovers! dont be disapointed, here’s the code in C# too:-

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    const string OUTPUT_FILENAME = “renderedpage.html”;
    StringBuilder renderedOutput = null;
    IO.StringWriter strWriter = null;
    HtmlTextWriter tWriter = null;
    IO.FileStream outputStream = null;
    IO.StreamWriter sWriter = null;
    string filename;
    string nextPage;
   
   
    try {
        //create a HtmlTextWriter to use for rendering the page
        renderedOutput = new StringBuilder();
        strWriter = new IO.StringWriter(renderedOutput);
        tWriter = new HtmlTextWriter(strWriter);
       
       
        base.Render(tWriter);
       
       
        //save the rendered output to a file
        filename = Server.MapPath(“.”) + “\\” + OUTPUT_FILENAME;
        outputStream = new IO.FileStream(filename, IO.FileMode.Create);
        sWriter = new IO.StreamWriter(outputStream);
        sWriter.Write(renderedOutput.ToString());
        sWriter.Flush();
       
       
        // redirect to another page
        // NOTE: Continuing with the display of this page will result in the
        // page being rendered a second time which will cause an Exception
        // to be thrown
        nextPage = “DisplayMessage.aspx?” + “PageHeader=Information” + “&” + “Message1=HTML Output Saved To ” + OUTPUT_FILENAME;
        //Response.Redirect(nextPage)
        //Response.Write(renderedOutput.ToString())
       
       
        writer.Write(renderedOutput.ToString());
    }
    finally {
       
       
        //clean up
        if (((outputStream != null))) {
            outputStream.Close();
        }
       
       
        if (((tWriter != null))) {
            tWriter.Close();
        }
       
       
        if (((strWriter != null))) {
            strWriter.Close();
        }
    }
}