Monday, November 14, 2011

ASP.Net: Redirecting to custom 401 page when Access denied occurs within an ASP.NET application with Windows authentication

Hi,

This was the problem happening when I was developing an ASP.Net application with Windows Authentication. Well I googled it a bit. Then I found the following article in Code Project. This was exactly what I needed. Wonderful Article and clearly solves my problem.

http://www.codeproject.com/KB/aspnet/Custon401Page.aspx

I hope it helps you too :)

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Friday, November 11, 2011

Javascript with ASP.Net : Use Javascript to iterate through a gridview and find checkbox in each row

I was looking for a mechanism to iterate over a gridview using Javascript. I had to get the checkbox in each row and update that checkbox's checked status according to the corresponding header columns checkbox's status.

Just after googling a little bit , I found exactly what I was looking for. The following link has done the same thing. Have a look at this:

http://www.codeproject.com/KB/grid/GridCheckBox.aspx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Monday, August 29, 2011

ASP.NET: Automatically convert http urls into https url

Sometimes we need to navigate securely within the website because of some confidential information. The user might use just http but we need to change it into https. So the approach I like best is to do it in masterpage level. We can write a small code snippet in the page load section of the masterpage which checks if the page is http or not. If it is http then changes it to https. Following are the line of codes that we need to put in the master page's page load function. I did not come up with these code, I collected it from the internet. So I am not taking any credit for the code ;)

//Redirect to https if typed http.

string redirectUrl = null;
HttpContext context = HttpContext.Current;

// if we want HTTPS and it is currently HTTP
if (!context.Request.IsSecureConnection)
{
redirectUrl = context.Request.Url.ToString().Replace("http:", "https:");
}

// check if we need to redirect, and if so use redirectUrl to do the job
if (redirectUrl != null)
context.Response.Redirect(redirectUrl);

Hope it helps. :)

Thursday, August 25, 2011

ASP.NET: How to create a custom error page.

ASP.NET has this useful feature that you can set a custom error page. Whenever an error occurs in the system, you are automatically redirected to the custom error page. By using this feature you can avoid showing the ugly yellow page and the error details to the user. How to implement this? Well the following link has described it all in details. :)
http://www.asp.net/hosting/tutorials/displaying-a-custom-error-page-cs

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Wednesday, August 10, 2011

ASP.NET: How to save the result found from database query in an excel file and let the user download that file

I was exactly looking for this solution and found that msdn already has what I needed (as usual). Here is the link:

http://support.microsoft.com/default.aspx?scid=kb;en-us;311194

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Wednesday, August 3, 2011

ASP.NET: Adding check/uncheck all button with a gridview.

It is a common question for many developers that
1) How to add a checkbox that appears with each row of a gridview.
2) How to implement check/uncheck all functionality.

I found a useful tutorial on the internet which pretty much covers both of the above questions.
Take a look at this here:

http://wiki.asp.net/page.aspx/281/check-uncheck-checkboxes-in-gridview-using-javascript/

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Monday, July 18, 2011

ASP.NET : How to access/read value from any control within a repeater.

We often need to read values from a databound control inside a repeater. How to read the control?

This is very simple. The following code sample has all in it. We assume that the repeater variable is repeaterObj and the control we want to access has the id "controlId". The control resides under itemtemplate. Then the code will be:

repeaterObj.ItemTemplate.Items[0].findControl("controlId");

To read the value from a label use :

string labelText = repeaterObj.ItemTemplate.Items[0].findControl("controlId").Text;

Monday, June 6, 2011

ASP.NET: Check is a string is in numeric format

Well, this is a simple problem, but C# made me learn new things when I wanted to know if a string is in a numeric format or not.

There is one function named tryparse() which does this work for us. Here is the reference to msdn page:

http://msdn.microsoft.com/en-us/library/f02979c7.aspx


** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Thursday, May 19, 2011

ASP.NET: Use sitemap to control menu in a page

While creating a web site we might have several pages and those pages might have several links in the menu. ASP.NET provides an easy way to manage this which is called sitemap. If you are looking for how to use sitemap in your application, this msdn tutorial has everything for you.

http://msdn.microsoft.com/en-us/library/ms178418.aspx

http://msdn.microsoft.com/en-us/library/ms178428.aspx


http://msdn.microsoft.com/en-us/library/aa581781.aspx

Have a look at it :)


** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Tuesday, May 17, 2011

ASP.NET : Creating a Global Class

If you are wanting to create a global class in your ASP.NET application, then this is how it is done, check the following link:

http://www.dotnetperls.com/global-variables-aspnet

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Thursday, May 12, 2011

ASP.NET Gridview : Get the row number from the linkbutton in a grid cell

Hi All,

Suppose you have a link button. When you click on that, you want to navigate to a different page. Before that you want to detect which row's linkbutton has been clicked. This is how it is done:

http://forums.asp.net/t/1250848.aspx/1

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Thursday, April 28, 2011

ASP.NET: Change the default session timeout for an application.

According to MSDN, the default timeout for server session is 20 minutes. You might need t ochange it for tha sake of your application. Just make the following change in the applications root level web.config file.

<system.web>
<sessionstate timeout="30">
</system.web>

ASP.NET: Automatic redirect to login page after session expires

If you want to automatically redirect to a page as the session expires, this is how you can do it:

Just check the session's existance. If it is no more in the server , then redirect to the desired page. The code snippet is as follows:

if (Session["session_name"] == null)
{
Response.Redirect("the_page_you_want_to_redirect_to.aspx");
}

ASP.NET: Sending an email from application.

Hi!

If you are looking to send an email from your application, ASP.NET has a very easy way to do that. The details can be found under this link:

http://msdn.microsoft.com/en-us/library/5k0ddab0.aspx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Monday, March 7, 2011

.NETand Java: Dictionary Object in both platforms.

While coding, we often feel the need for getting a value depending upon a key value. This can be accomplished through dictionary object. Thankfully C# came with dictionary object. A small example on how to use dictionary object can be found here:

http://www.dotnetperls.com/dictionary-keys


I searched through Java. They had this dictionary object before, but they have deprecated it for some reason. They have recommended using MAP object which works pretty much the same way a dictionary object works. A quick code snippet to know how Java Map object works can be found here:

http://www.easywayserver.com/blog/java-map-example/


** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

ASP.Net : Load XML from String

If you are interested to know how to load XML from a string rather than from a file, then this tutorial says it all:

http://www.xmlplease.com/create-xml-parse#s2.

The first line is very important, don't miss it.

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Friday, March 4, 2011

ASP.Net: Printing raw xml .

I was trying to print a raw xml string. Browser was omitting the xml tags at the time of display. Then I searched through the internet and found that ASP.Net has a pretty cool widget named Literal. Using this widget, it is very simple to print xml string without the xml tags omitted by the browser. Here is an example code snippet:

1) Add the literal object.

2) Then just use this literal object like a label:

Literal1.Text = XMLString;

Is not it cool!!!

Thursday, March 3, 2011

ASP.net : Post data to another page programmatically without a form submit button.

I was working on a project where I had to pass some value securely to a php script from an ASP.NET page. I found that .NET web request and web response class is there to do this task. Here is the MSDN provided tutorial on how to use these two classes to sent the data through post method and get the response.

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

In the receiving page, I need to write the data through Response.write (for ASP.net) or echo(for php). The response is automatically sent to the requesting page.

Is not it cool!!

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Friday, February 18, 2011

Finding HTML controls from ASP.NET

Here is the article that says ins and outs of this issue.

http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Friday, February 11, 2011

ASP.NET: Check a file for its MIME type.

If you want to check a file file for its MIME type, this is the link that tells you how to do it. I used this link when I checked MIME type of a file with ASP.NET file uploader tool.

http://tiredblogger.wordpress.com/2008/04/07/verifying-extensions-and-mime-types-of-fileupload/

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Thursday, February 10, 2011

ASP.NET : How to use customvalidator with a file upload tool for client side validation

We really do not want to validate in the server side because it makes the server busy.
So a good approach is to validate in the client side using javascript. Here is a good tutorial that does it:

http://www.beansoftware.com/ASP.NET-FAQ/FileUpload-Validation.aspx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Javascript: split method can be used to split based on string or regular expression.

If you want to split a string based on another string, this how you do it:
str.split(" ");

If you want to split a string based on a reges, this how you do it:
str.split(/\s/);

\s is a reges which is used for any space character like space,\n,\t or \r.

Wednesday, February 9, 2011

MySQL: How to search a value in the whole table.

If you want to search a value in the whole table:

1) You have to enable full text indexing on the columns.
2) Column types have to text/char/varchar.
3) Search by the MATCH() AGAINST() functions.
4) You cannot match text having length less than 4. There is a work around to it. I'll write about that later.

Details can be found here:

http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

Friday, January 28, 2011

ASP.net: XMLDataSource can only bind with DropDownList when the values are used as attributes.

If you want to bind a DropDownList with an XMLDataSource and your xml has the following structure:

<destination>

<location><>assets_UploadedFile1</location>

<location>assets_UploadedFile2</location>

<location>assets_UploadedFile3</location>

</destination>


Actually, you can't bind. You have to modify your xml file as to conform to the following format:

<destination>

<location name="assets_UploadedFile1">

<location name="assets_UploadedFile2">

<location name="assets_UploadedFile3">

</location>


Now you can easily bind the DropDownList to the XMLData Source by selection xPath="destination/location" .

ASP.NET and AJAX: File Upload in UpdatePanel, Does not work.

It does not work as expected because of some security issues. Details look at here:

http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx

How to upload a file using ASP.net

Two easy ways to do that are:

1) Using ASP.net FileUpload Tool:
http://msdn.microsoft.com/en-us/library/aa479405.aspx

2) Using AJAX control Toolkit- AsyncFileUploader Tool:
http://www.asp.net/ajaxlibrary/HOW%20TO%20Use%20the%20AsyncFileUpload%20Control.ashx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Tuesday, January 18, 2011

Get data from SQL Server Stored Procedure using LINQ.

http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Get data from SQL Server using LINQ.

This link has a wonderful video tutorial. That is good enough to get a starting point.

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

Starting with .Net Language Integrated Query (LINQ)

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**

http://msdn.microsoft.com/en-us/library/bb308959.aspx