C#, Microsoft, Technical, Visual Studio

Visual Studio 2010 unable to resolve project reference

I created a new console application in VS 2010 and imported an existing class library project from the disc into the solution. When I tried to build the project VS was complaining that it was unable to resolve the reference to the project. I did ensure that the imported project was correctly referenced in the console app.

It was due to the fact that the console app was targeting the .net 4.0 client framework and the class library was targeting the full .net 4.0 framework. I changed the console app to target the full framework and voila I was able to build the solution successfully!

CSS, Technical

Background Image for Input Button with CSS (works in both Firefox and IE)

I was having trouble with setting a background image for an input button in IE 6. The background-image property that worked in Firefox 2.0 just did not have any effect on IE6. After a bit of googling, I realized that the background-image property will not work on IE and that we need to use the background property. Here’s a snippet that works on both browsers:

<input type=”button” class=”button” title=”Background Image” />

.button{
      background: white url(‘images/imagebutton.gif’) no-repeat top;
      width: 150px;
      height: 40px;
      color:white;
}

C#, Microsoft, Technical, Tips, XML

Case insensitive XPath query

I wanted to do a case insensitive xpath lookup in my C# .Net application. There was no direct way that i could find to get the job done, but fortunately the workaround ain’t that difficult. Lets consider the following example:

<xml>
<books>
<book id=”1″ name=”Book1″ type=”fiction” />
<book id=”2″ name=”Book2″ type=”nonfiction” />
<book id=”1″ name=”Book1″ type=”FICTION” />
</books>

To request for all fiction books here is the xpath query:

“books/book[translate(@type, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘abcdefghijklmnopqrstuvwxyz’) =’fiction'”

Microsoft, SQL, SQL Server, Technical

Query for Powers of 2 in SQL Server 2005

I had a table with two columns: ID and Description. The task was to display the description of all elements whose ID is a power of two. Here’s the query that let me accomplish that:

SELECT ID, Desc FROM tblBits WHERE ID > 0 AND ID & (ID-1) = 0

How does this work?

Every power of two when converted to binary has only one bit set.
For example: 4=100, 8=1000, 16=10000
And one digit less in binary looks like:
3 = 011, 7=0111, 15=01111

So an AND operation between {4,3}, {8,7}, {16,15} and so on will always yield a ‘0’

.net, Ajax, C#, Code, Microsoft, Technical

‘Sys’ is undefined – ASP.Net Ajax error

I was using ASP.NET 2.0 along with Ajax Extensions 1.0. I received this error ‘Sys’ is undefined error message every time I used an Ajax control. I was pulling my hair out on it and decided to give the Control Toolkit’s web.config a glance. The following lines were not available in my web.config, so i copied them over:

<httpHandlers>
            <remove verb=”*” path=”*.asmx”/>
            <add verb=”*” path=”*.asmx” validate=”false” type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/>
            <add verb=”*” path=”*_AppService.axd” validate=”false” type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/>
            <add verb=”GET,HEAD” path=”ScriptResource.axd” type=”System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ validate=”false”/>
</httpHandlers>
<httpModules>
            <add name=”ScriptModule” type=”System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/>
</httpModules>

P.S: These lines go inside the System.Web section of your web.config file

Bingo.. the error vanished and all my controls started to function as they should 🙂

 

Technorati Tags: ,,
.net, C#, Code, Microsoft, Technical

Using StreamReader without locking the file in C#

I was using the StreamReader class to read the contents of a text file. I was under the assumption (well I know its a bad

thing)  that because I am only reading the contents of the file, the file would be available for other applications to read or write. Guess what, i was wrong. The StreamReader class does maintain a lock on the file until you call the Close() method. So to not have the lock, we have to explicitly set up the sharing mode.

FileStream fs = new FileStream(@”c:\test.txt”, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
txtContents.Text = sr.ReadToEnd();
sr.Close();

Technorati Tags: ,,