Sunday, June 14, 2009

Storing Files In Database Using ASP.NET

In this example I am storing files like word document, text file and excel files into database and retrieving back from database and saving back into a folder.

So lets start creating a Table in SQL Server database in which we are going store our uploaded files, in this table I am creating following columns.

  1. DocumentID int primary key Identity yes
  2. DocumentName varchar(100) name of the document with extension
  3. Document varbinary(MAX) binary column for saving file in Binary format.

Open SQL Server Management Studio and open Northwind database and run following script for creating Table

image

Now we are ready with our Database creation so lets create a web application

Open Visual Studio and create a Web Application and in Default.aspx add following controls.

  1. One upload control
  2. One Button for uploading
  3. One DropDownList for displaying List of files which are in database already
  4. One Button for opening selected document.

Please refer following screen shot for design.

image

In Page_Load event we have to retrieve all documents which are already stored in database before so I have written a method called FillDropDown to fill the DropDownList

image

Once user selected a file to upload and click on Upload button we have to store that file in database in Binary format so I am using InputStream of uploaded file and reading into byte array and connection to database and storing the data.

image

Once user selects one uploaded file click on open I am retrieving the binary data of that file from database and using FileStream object I am saving the a folder, I am created a Folder called Documents in the solution itself and storing files in that folder, if a file already present in the folder it will be over written. please add following code in open button click event.

image 

Now we are ready with our code just build, run and test.

image 

Tags : Storing files into database, ASP.NET, C#, Web Application, FileStream, InputStream.

Saturday, June 13, 2009

Show Long Text Item in ComboBox

Many times in our windows form we need to show data in Combo Box and items in combo box are have same length, so we face a problem that some item in the combo box is not shown fully, and increasing width of then combo might not always the good idea.

To avoid above mentioned problem we have a solution, we can increase the height of the combo box item at run time based on the length so the item can be read fully, so lets start building the example.

Open Visual Studio and Create a Windows Form Application and Add One label and one combo box and and some items in the combo box as shown in the below screen shot.

image

Now we are ready with design part, so lets start writing some code to increase the height of the combo box, for this we need to make use of the MeasureItem event of Combo box, in this item we can find out what is the length of each item and based on the length we can set the height, once you we set the item length we need to add the item in Combo Box using DrawItem event.

Normally the combo box draw style is OwnerDrawFixed so we cannot change any item at run time if it is OwnerDrawFixed so we need to change Combo Box’s DrawStyle to OwnerDrawVariable so that run time changes are allowed.

Please add following code in Form_Load, in below code I am setting DrawStyle and assigning MeasureIteam and DrawItem events to Combo Box

image

We need add following event handler for handling MeasureItem and DrawItem events.

image

Now we are ready with our code so lets build, run and test.

image

Tags: Showing full item text in Combo Box, .NET, C#, Windows Application

Monday, June 8, 2009

Client Side AJAX in ASP.NET 4.0

AJAX is most used technology these days, this avoids page reloads and user will not get frustrated, however it still requires a post back to server so ASP.NET 4.0 introduced ClientSide AJAX which is achieved by using some jQuery and server side service

Here I am going to build a sample using Northwind database Categories and Products, on selecting a category all products under that category will be listed, to list the products I am using a HTML Table and Templates.

So lets start, open Visual Studio 2010 and create a Web Application and name it as ClientSideAjax. To retrive the data from database I am using 2 DataSources those are

1) SQLDataSource1 : Just drag and drop a SQLDataSource object from tool box and select connection and connect to Northwind database’s Categories table.

2) ProductsCategories.edmx: This datasource is EntityDataModel, just right click on project in Solution Explorer and add a new file select EntityDataModel and name it as ProductsCategories.edmx, just follow the steps and connect to Northwind Database and select Product and Categories tables and finish.

Now we have created required datasources for our project, so let start designing the web form, we need to add one Dropdownlist for listing all the categories and one HTML table for listing all the products under selected category, set the DataSource1 to DropDownlist and set DataTextField = CategoryName and DataValueField = CategoryID

image

We will talk about {{ ProductName }} template in details below.

So now lets write a service from where we will get the all the products under a category, for that I added a WebService page Northwind.asmx(create a folder called Services and Northwind service under that folder) and a web method to get products based on category, please find the asmx page in code below I am creating a public web method getProducts, in this method I am reading the data from ProductsCategories EntityDataModel and returning products.

image

Now we are ready with our server side code and let start write some client side JQuery to read the data from web service and display using JQuery templates. so we need add some Microsoft AJAX JavaScript files to our project just create a folder in our project called MicorsoftAjax and add following files

image  You can download these JavaScript files from http://aspnet.codeplex.com

Add reference in the Default.aspx page to all js file and Northwind service, also create a style temple which is used to hide the jQuery template initially

image

So now we will write a PageLoad method in JQuery to get the products, now we will add following method to Default.aspx

image

Now we are ready with our code lets build, run and test

image

Hope this helps.

Tags: Client Side AJAX in ASP.NET 4.0, jQuery, Visual Studio 2010, C# .NET

Thursday, June 4, 2009

QueryExtender Web Control : ASP.NET 4.0

Query extender is a new control introduced in ASP.NET 4.0, this control is used to filter and sort data by using different expressions, it has Search Expression, OrderByExpression, RangeExpressions etc.

When you have data in your Datasource and you want to filter at the application end so you always go for this control.

As of now this control is used with LINQDataSource other DataSource controls are not support with Query Extender in Visual Studio 2010 ASP.NET 4.0 beta 1.

I am building a example how we can filter data using QueryExtender, as it is part of ASP.NET 4.0 here I am using Visual Studio 2010 beta 1 so lets start.

Create a web application and Name it as QueryExtender, now we will add a LINQ To SQL Class file to our project by right clicking the project in Solution Explorer selecting Data and Select LINQ to SQL Class file and name it as LINQProducts.dbml, this is the LINQ DataSource for LINQDataSource.

AddLINQ

Once you add this file to our project you can see LINQProducts.dbml file and you will observer 2 parts in the file, now you need to drag and drop your table from Server explorer to right side part, here I am selecting Northwind database's Products table and dropping.

CreateLINQ

To effect the changes we need to build our project here once.

Now we have created LINQ source for LINQDataSource control, Now open the design view of Default.aspx page and Add LINQDataSource Control from the Toolbox.

Once you have added the LINQDataSource control select the control and click on '>' symbol and click on Configure DataSource, now you will see a wizard for configuring the datasource, select the 'Show only DataContext objects' check box and choose the LINQProductsDataContext from Context combo box, click on next.

LINQControl1

Now choose required fields to display, here I am selecting ProductId, ProductName, UnitPrice, UnitsInStock fields to display and click finish.

LINQControl2

Now we have creaetd LINQDataSource object and bind the data with Products table to display, now drag and drop GridView control from tool box and add to Default.aspx, and '>' and click on Configure DataSource and select LINQDataSouce1 as datasource and Save.

GridView

To check whether data is getting display or not just run the project and see the output .

OutPut1

By this point we have displayed the data using LINQDataSource control to filter the data displayed we will use QueryExtender control, before creating the QueryExtender we need to add a reference in Web.Config to make use of its Expressions, so please add following in Pages -> Control section in Web.Config

image

Note : In above line you need to replace the PublicKeyToken value with other controls PublicKeyToken.
Once you have done this we are now ready to add QueryExtender to our project just add following code in our Default.aspx page, before adding we need to add some control to our form for search purpose, so add one label and textbox and a button to Default.aspx as shown in the screen shot Design.jpg

Design

Now add QueryExtender control to Default.aspx

QueryExtender

In the above lines I am creating a QueryExtender and assigning its taget control as our LINQDataSource1, for filtering I am getting the value from a text box, you can also get the result in order by using OrderByExpression.


Now we are ready with our code so let build, run and test

OutPut2
Hope this helps.

Wednesday, June 3, 2009

EntityDataSource Control ASP.NET

ASP.NET 3.5 sp1 introduces a data source control called EntityDataSource using this control we can bind data to different Data Binding controls.

Now lets start building an example application which gets the data from Northwind database employee table.

Open Visual Studio 2008/2010 and create a Web Application.

Now right click on Solution explorer and add new item and select ADO.NET Entity Data Model and name it as Employees, we will fill EntityDataSource object using this model only.

image

Select the option generate from database and click next

image

Now click on the New Connection button and give required details and create a connection to Northwind Database and click next

image

Once you have connected to database it list all the table procedures etc, here I would like to display data of Employee Table so I selected Employee table and click on finish.

image

Now we are ready with our model once you have done this, in you project you will Employees.edmx file

image

to use our model we need to build our project and check whether there are any error or not, just build the project once.

Our model is ready now, so lets bind the data to a EntityDataSource control for that we need to select a EntityDataSource control from ToolBox and add to Default.aspx, and also add one GridView control to Default.aspx

To bind the model to our EntityDatasource control just select the control and click on > symbol and select configure.

Select Named Connection combo box and select our EntityModel, click on next

image

Select EntitySet name as employees, you will see only one employee table because while creating model we have selected only one table if select more those tables also will be listed, one you select employees all the columns of that table will be listed below select which are appropriate to you here I am selected EmpId, first name and last name and finsh

image

Now we are created EntityDataSource lets bind the data to grid view for that select the grid view and click on ‘>’ symbol and select EntityDataSource1 as data source.

image

Now we are ready with our code just build, run and test.

image

tags: Using EntityDataSource Control, EntityDataModel, ASP.NET 3.5, ADO.NET

Monday, June 1, 2009

Bing : A decision making engine

Bing is new search engine from Microsoft; actually they say it is not search engine but decision making engine. Microsoft tries to live up to Google search.

Bing is good in finding deals, so try it yourself www.bing.com.