.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: ,,
Advertisements
.net, C#, Code, Microsoft, WPF

Hide a window instead of closing it in WPF

I wanted to give WPF a try and was creating a timer application which will pop up a window once a while. But what I wanted was for the application to keep running in the tray even when its closed. The following code does NOT work in WPF for some reason:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            Window.Visibility = !Window.Visibility;
        }

 

In order to hide a closing window in WPF, this is what needs to be done:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            //Do some stuff here 

            //Hide Window
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback)delegate(object o)
            {
                Hide();
                return null;
            }, null);

            //Do not close application
            e.Cancel = true;

        }

.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: ,,
Blog, Blog Help, BlogML, Wordpress, XML

BlogML importer for WordPress 2.5

I had a blog server which I felt compelled to migrate to WordPress 2.5. The existing blog server can only emit the data in the BlogML format. So I set out to find an BlogML importer for WordPress 2.5. I stumbled upon Aaron’s post on importing to wordpress. So here is what needs to be done:

  • Downloaded WordPress BlogML import module from Aarons site (blogml_wp_2.3.zip)
  • Upload a copy of the BlogML.php file to your wp-admin/import folder
  • Edit the file and change the PATH_TO_CURRENT_DIRECTORY to point to the folder where import.php exists.
  • Download the PHP XPath library 3.5 from Sourceforge
  • Upload a copy of the XPath.class.php file to your wp-admin/import folder

We are all set now. Just go to your wordpress admin page and navigate to Manage -> Import. If all is well, then you should see an entry for BlogML. Just click on the link and follow directions to import the BlogML file into your WordPress blog.

PS:  Once you click on the BlogML link on the import page if you are unable to view the “Upload file and import” button then don’t worry, just open up the BlogML.php file and modify the following lines:

// Instantiate and register the importer
include_once(‘import.php’);
if(function_exists(‘register_importer’)) {
    $blogml_import = new BlogML_Import();
    register_importer(‘blogml’, ‘BlogML’, (‘Import posts, comments, users, and categories from a BlogML file’), array ($blogml_import, ‘dispatch’));
}

to

// Instantiate and register the importer
//include_once(‘import.php’);
//if(function_exists(‘register_importer’)) {
    $blogml_import = new BlogML_Import();
    register_importer(‘blogml’, ‘BlogML’, (‘Import posts, comments, users, and categories from a BlogML file’), array ($blogml_import, ‘dispatch’));
//}

 

Many thanks to Aaron for the plugin.

Microsoft, Technical, Tools

Sysinternals Live

If you are a Sysinternals fan then this one is for you. Microsoft has announced the launch of Sysinternals Live. Ever since Microsoft took over Sysinterals the one thing that i have detested is the need to search for a particular utility and scrolling to the bottom of the page just to download it. But Sysinternals live changes all of that. If you frequent Regmon, Filemon, Process Explorer, Whois, autoruns, Bginfo amongst the myriad of tools offered by Mark and his team then check this out. Just go to Start -> Run and type in

\\live.sysinternals.com\tools

This should give you the list of all the tools at on place. This should save us some time as well as avoid the need to go to Google every time we need the link to a specific Sysinternal utility.

.net, C#, Microsoft, Technical

.Net CLR Windows Service and the ConfigurationManager class

I created a new windows service and attached a App.Config file to it. Then I went back to the code and tried this:

System.Configuration.ConfigurationManager.GetSection(“Extras”);

For some reason the compiler was not able to figure out the class and kept throwing an error. This does not happen with your standard EXE projects as well as ASP.NET projects. I also did not want to use the
ConfigurationSettings.GetConfig() method as it was obsolete. I fiddled around for a while and eventually ended up manually adding a reference to the
System.Configuration DLL. For those who don’t know, just right click on the References folder in your project, click on Add Reference. Select System.Configuration in the .NET tab and click OK. This should allow you to access the ConfigurationManager class. Back to work now..

I will some day write a post on how to implement custom configuration sections in the App.Config file.

Code, Microsoft, Performance, SQL, SQL Server, T-SQL, Technical

Computed Columns in SQL Server 2005

“Computed Columns” as the name suggests allows you to set the value of a column based on other columns data. Here’s how you could specify the formula in T-SQL:

--Create Table
CREATE TABLE T1 (
    a INT, 
    b INT, 
    operator CHAR,
    c AS CASE operator
        WHEN '+' THEN a+b
        WHEN '-' THEN a-b
        ELSE a*b
    END
    PERSISTED
) ;

--Insert dummy data into it
INSERT INTO T1 VALUES(1,2,'+')
INSERT INTO T1 VALUES(5,3,'-')
INSERT INTO T1 VALUES(4,4,'')

--View the results
SELECT * FROM T1

Notice the keyword PERSISTED. This will enable the computed column value to be stored physically on to the disk so that it need not be created every time you ask for it. Creating an index on this computed column will improve performance by doing a seek rather than a table scan.

If you want to specify the above computed column in SQL Server management studio directly you can do this:

Modify the table. Go to the computed column specification for the specific column. Expand it and type the following in the “Formula” field:

(CASE [operator] WHEN '+' THEN [a]+[b] WHEN '-' THEN [a]-[b]

ELSE [a]*[b] END)

Enter without line breaks. If you want the column values to be persisted in the storage medium, just change the value of “Is Persisted” to “Yes”