Personal Blog of Mustafa Basgun

Archive for the ‘C#’ Category

Getting Syntax Error in ASP.NET Routing Due to WebResource.axd

with 6 comments

If you implement routing in an ASP.NET 4.0 web application and use the validation controls in it, it is good to keep in mind that you need to add an exception for the .axd files in your Global.asax file. Otherwise, you will keep getting a syntax error because the routing picks up the .axd files and not properly loads the JavaScript files needed for the validation controls.

mustafa-basgun-blog-aspnet-routing-error-1

mustafa-basgun-blog-aspnet-routing-error-2

If same issue occurs in an ASP.NET 3.5 SP1 application, you can use the StopRoutingHandler class which implements the IRouteHandler interface.

Written by Mustafa Basgun

October 25, 2010 at 9:51 PM

Posted in AJAX, ASP.NET, C#, Web Development

Tagged with ,

Getting Connection String From Web.Config Inside A DLL Code

leave a comment »

I have been working on creating some .NET dlls at work which would commonly be utilized by our web applications. And, one of the requirements was getting the connection string information from web.config file. Simply making the call by using the ConfigurationManager class wouldn’t work since it was giving some kind of compile time error when you try to build the dll. So, I needed to find a workaround to retrieve the connection string from either connectionStrings or appSettings elements. Here is the solution that I have figured out so far.

Necessary namespaces:

mustafa-basgun-blog-aspnet-connstring-webcofig-1

Generic function that grabs the information based on your connection string name:

mustafa-basgun-blog-aspnet-connstring-webcofig-2

Sample usage:

mustafa-basgun-blog-aspnet-connstring-webcofig-3

To understand the ConnectionStringSettingsCollection and NameValueCollection classes, followings links would be helpful:

http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringsettingscollection.aspx
http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx

Written by Mustafa Basgun

March 22, 2009 at 7:20 AM

Update Title and Meta Tags Dynamically in ASP.NET

with 3 comments

If you are using master pages, updating your title or meta tags (such as “description” and “keywords”) may become an important subject because of the SEO purposes. Since there are several ways to accomplish this, here is my way in C# to do so.

Written by Mustafa Basgun

April 30, 2008 at 8:00 AM

Posted in ASP.NET, C#, SEO, Web Development

Rotate Array by N Position – Via Programming Pearls

with 2 comments

Lately, I have been trying to finish reading the Programming Pearls written by John Bentley. The chapters related with designing and developing algorithms are really entertaining. One of the questions that was discussed in the early chapters of the book was about an algorithm of rotating a one-dimensional vector, and I want to share my solution written in VB.NET and C# by utilizing a simple string array.

Question (with the terms of an array)

Rotate a one-dimensional array of K elements from left by N positions.
For instance, with K=7 and N=3, the array {a, b, c, d, e, f, g} is rotated to {d, e, f, g, a, b, c}.

Solution

As suggested in the book, I will be using the Array.Reverse method. I will also write the function that rotates an array from right as well in order to carry out the discussion further.

In VB.NET – Rotate By N From Left

In C# – Rotate By N From Left

In VB.NET – Rotate By N From Right

In C# – Rotate By N From Right

Page_Load of an ASP.NET environment would be a good place to test and visualize the results in this case.

In VB.NET

In C#

Finally, results on the browser should be like:

Written by Mustafa Basgun

April 11, 2008 at 6:00 AM

Posted in C#, VB.NET

Utilize Read-Only TextBoxes in FileUpload Control of ASP.NET

with 9 comments

Nowadays, I am spending most of my work time on developing a “document depot” type of web application which has a heavy usage of FileUpload control. And, it was one of the requirements that users should not be able to modify the associated textbox after browsing the document from their hard drives. I believe it is a very good user-experience practice and it avoids some possible security issues as well. After walking around between some search results, I have found the following solution by assuming that FileUpload1 is the ID of the control:

VB.NET:

C#:

As a result, users would be able to select a document only by using the Browse button.

I have tested the sample code with the latest versions of IE and Firefox, and everything seemed to be working fine with both browsers.

Written by Mustafa Basgun

January 28, 2008 at 5:00 PM

Manipulating ColSpan Attribute of Footer Row in GridView

with 8 comments

There may be some cases that the Footer of a GridView is desired to be displayed as one cell, which means setting the ColumnSpan property of the Footer’s first cell to the number of columns of the GridView. As there are some other possible workarounds to accomplish this, here is my way by adding a few lines to the OnRowCreated method.

First, make sure that ShowFooter property of the GridView is set to True:

01202008_1.gif

VB.NET:

01202008_2.gif

C#:

01202008_3.gif

Written by Mustafa Basgun

January 20, 2008 at 2:00 PM