Technological Guide.

ASP.Net FileUpload control inside Ajax UpdatePanel

Wednesday, May 18th, 2011

I’m posting this article with reference to the request from my friends and for all other world wide asp.net lovers and especially for programmers who are in need of a solution for asp.net FileUpload control inside Ajax UpdatePanel. We often face problem with FileUpload control when its placed inside Ajax UpdatePanel. There is a simple trick for it to make it upload without any issues :).

ASP.Net FileUpload control needs a full Page PostBack to upload file and proceed further. The simplest solution to use UpdatePanel is by adding PostBackTrigger to the updatepanel as shown below.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        Please upload your file: <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />&nbsp;&nbsp;
        <asp:Button runat="server" ID="btnSubmit" Text="Upload"
            onclick="btnSubmit_Click" />
        <br />
        <asp:Label runat="server" ID="lblFileDetails" Text=""></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnSubmit" />
    </Triggers>
</asp:UpdatePanel>

I’m presenting you a simple example with both HTML source code and C# code behind files which is coded for Microsoft .Net Framework 3.5 (Visual Studio 2008).

You can download from: FileUploadInsideUpdatePanel

ASP.Net connect to database using HttpHandler ASHX file

Wednesday, July 1st, 2009
 
Another alternative way to connect to database in ASP.Net is using ASHX file. The example we use is the same Customer information application given in my Previous Post  which connects to the Northwind database and shows the available customers in dropdownlist of an aspx web page. Upon selecting each customer the Customer details will be displayed. Inorder to make AJAX calls, the latest version of the jQuery library should be referenced which can be downloaded from here.
The design code of Customers.aspx

The codebehind in which data is retrieved from customers table of Northwind database and bound to a dropdownlist showing company name and having CustomerID as value field.

 
ASHX files are convenient ways to deliver partial content to a web page. They are actually HttpHandlers and responsible for processing incoming HTTP requests and providing the appropriate response. They are often used to deliver binary content such as images or files that are stored in database or outside of the web application file system. For delivering small amounts of html to be plugged into a particular position on a web page, they are extremely useful. Add New Item and choose Generic Handler file type and give the file name as “FetchCustomer.ashx”. A template for a class that inherits from IHttpHandler will be opened which contain one method-ProcessRequest( ) and a property-IsReusable( ). The entire FetchCustomer.aspx which generates a response to the Ajax call and creates output onto the caller page itself using context.Response object from the data retrieved to DataReader object which is from Customer table related to the particular CustomerID passed as Request.QueryString Parameter.

 
Now to call the HttpHandler of ASHX file, use the JQuery load function with QueryString parameter (exactly same as $.load( ) approach which was written in Previous Post) as shown below.

ASP.Net connect to database via AJAX using JQUERY

Tuesday, June 30th, 2009
One of the ways to connect to database in ASP.Net is via AJAX using JQUERY by giving an example below which connects to the Northwind database and shows the available customers in dropdownlist of an aspx web page. Upon selecting each customer the Customer details will be displayed. Inorder to make AJAX calls, the latest version of the jQuery library should be referenced which can be downloaded from here.
The design code of Customers.aspx

The codebehind in which data is retrieved from customers table of Northwind database and bound to a dropdownlist showing company name and having CustomerID as value field.

Now create another file “FetchCustomer.aspx” with code behind option deselected. The entire FetchCustomer.aspx which generates a response to the Ajax call and creates output onto the caller page itself using Response object from the data retrieved to DataReader object which is from Customer table related to the particular CustomerID passed as Request.QueryString Parameter.

There are a number of ways jQuery can request the FetchCustomer.aspx page and handle the response. The first way we are going to illustrate is the load() function, which loads html from FetchCustomer.aspx The below code will be added in the head section of the Customers.aspx which will reference the lattest version of the JQuery library and as the page loads, $(document).ready( ) function will be applied to onchange event of the dropdownlist control. This functions gets the id of the div “CustomerDetails” of the page (”Customers.aspx”) within which it resides to load the html returned from the FetchCustomer.aspx page and passes the currently selected dropdownlist value in the querystring.

Another way alternative to $.load( ) funtion is the $.get( ) function which does exactly the same except the callback argument specifies what is to be done with the response from the Ajax request. Place this script in the head section of Customers.aspx. In $.get( ) function the query string value is passed along with the querystring name in { } brackets along with the query string name & value seperated by a colon(:). jQuery takes these values and constructs a querystring as part of the HTTP request, so that the page called is FetchCustomer.aspx?CustomerID=strCustomerID.

The other alternative method is $.ajax(). This is a more feature rich method in that it allows a range of options to be applied to manage different types of call, and error handling. Just place the below script in the head section of the FetchCustomer.aspx page.

How to create Pdf file in ASP.Net

Wednesday, June 24th, 2009

Creating Pdf files in Asp.Net may not be a requirement but many of us being developers always wish to create pdf files to deliver some information instead of always going for crystal reports or others. The Microsoft .Net Framework didn’t provide any native way to work with PDF files, to serve our purpose we need to rely on third party components that are available. The best open-source free component available is iTextSharp. iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly. You can download iTextSharp dll from here.

 

Here we’ll see a simple web application to make use of iTextsharp to create pdf file with our own text. Create a web application and Add reference to the iTextShar.dll file which is extracted from the zip file downloaded from the above link. Create a folder inside your application called “PDF” to store our created pdf files. Design the Default.aspx with a multilined TextBox (to let user or we to enter our preferred text in paragraphs to appear in the pdf file) and a Button to trigger the code for pdf file creation as shown in the picture.

In code behind file we’ve to add 2 namespaces inorder to use iTextSharp Classes and Methods.

 

Code Behind File

Run the application, enter the information in multilined Textbox which must be displayed in pdf and click the button Create Pdf. An alert message will prompt you saying Pdf file has been successfully created. Now close the application and check the PDF folder inside the application which will contain a Pdf file created whcih looks as follows.

Date Formatting in ASP.Net

Tuesday, June 16th, 2009

We need to format date values in our application often. We can format date value as shown below.

The output for the above two methods will be shown as follows.
16-Jun-2009
16-Jun-2009
We can even format a date value retrieved from database in the above method.

We can format the datetime value either module specific or page specific or application specific method using Globalization.

The output for the above 3 methods are as follows…
16-06-2009
16 June 2009
16-06-2009 15:24:42
We can apply a specific culture for globalization in module level as shown above, or in page level in page declaration or in web.config file as shown below in

Fetch the incremented AutoNumber value from MS-Access

Wednesday, June 10th, 2009

The Microsoft Access Database provides AutoNumber fieldtype which is generated automatically when a record is added in such tables which contains AutoNumber. We can fetch the auto incremented value from MS-Access database to our applications using built in function @@IDENTITY( ).

The Jet 4.0 provider supports @@Identity( ) to obtain new Identity value from Access .mdb file which returns the value of an autoincrement column that is generated on the same connection. That means the Connection object used for the Insert query must be re-used without closing it and opening it up again to fetch the Identity value as shown in the below example.

The above built-in function @@IDENTITY( ) can be used to retrieve Identity value from SQL Server too with the same above coding.

Error: The Operation must use an updatable query

Tuesday, June 9th, 2009

When we work with an ASP.Net application with MS-Access as backend database, we often face the error “The operation must use an updatable query”. Even though our code compiles and builds without any errors, when we run the application, when the page tries to INSERT or UPDATE records the above error will be raised. We’ll see the details of the error root cause and the easy solution.

When working with MS-Access database in ASP.Net web application, the MS-Access database.mdb file will be placed in the App_Data folder. When we develop ASP.Net application in multi-user environment(like Windows XP, Windows 2003, Windows 2008), a.ldb file will be created whenever the database is opened. This particular .ldb file contains details of the user who opened the file, and prevents writing the records which are opened by another user.

All ASP.Net applications use the “ASPNET” machine account in Windows XP and “NETWORK SERVICE” account in Windows Server 2003, 2008 and Vista machines to access the file system of Operating System. Inorder to create, write and delete the required .ldb file, the relevant user “ASPNET” or “NETWORK SERVICE” account must needs MODIFY / WRITE permissions on the folder where the .mdb file resides which is App_Data folder in our ASP.Net Application.

To enable MODIFY / WRITE permissions on the App_Data folder, open the ASP.Net web application project folder in Windows Explorer, right click on the App_Data folder and select properties. When we reach properties we can see several tabs, among the tabs, go to Security tab which contains User Names section as shown below.

click the Edit button which will show UserNames window as shown below.

and then click Add which shows as follows

Type the account and click “Check Names” to verify the account name typed and click “OK” to add the user account. Upon clicking add, we’ll reach to Security Permission Edit Window, then click “Modify” for the account we added and click ok and Run the ASP.Net web application which can insert & update in the Microsoft Access Database.

The above solution can even fix the errors like The Microsoft Jet database engine cannot open the file (unknown), It is already opened exclusively by another user, or you need permission to view its data.

Highlight ASP.Net Gridview Row on Mouse Hover

Monday, June 8th, 2009

It’ll be very attractive & meaningful UI to highlight a GridViewRow when mouse pointer hovers which can be achieved with the following code.

Adding CheckBox Control to ASP.Net GridView

Monday, June 8th, 2009

While working with inventory screens or multiple items of any category, we often need to face situation of representing the items in ASP.Net GridView on a webpage providing CheckBox to select single or multiple items. CheckBox control can’t be placed in a column of GridView directly, in which case it must be embedded in an ItemTemplate column of the ASP.Net Gridview.

Design Source

Coding