Thursday, November 26, 2009

Advantages of storing images in database over file system

  • If the images binary data is stored in a database table, we have all the data required to make the image any size we want, and it will always look like the orginal image

  • If the images binary data is stored in a database table, when we back the database up, we have also backed up all the users images

  • We can store meta data corresponding to the images that we store

Tuesday, November 17, 2009

How to Add & Retrieve Images from a SQL database using LINQ in c#

Today I received an email from a friend, asking how to save a binary file in to the database using LINQ to SQL. This was the second time a person had asked me the same question, So I thought of having a blog entry about it.

Assume that we need to upload a file and save its name, and the file in the database table. so lets see how we do it.

This is my Table

CREATE TABLE [dbo].[Files2](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[FileSource] [image] NOT NULL,
)
GO
And The Following Stored Procedure is used to Insert the Value:
 
CREATE PROCEDURE [dbo].[ADDFILE]
(
@FILENAME VARCHAR(50),
@FILESOURCE image
)
AS
SET NOCOUNT ON

INSERT INTO Files([FileName],[FileSource])
VALUES(@FILENAME,@FILESOURCE)


1.Storing Images

The following code is used to upload and save the file using web form with a text box for file name, and a file upload control to upload the file. on the click event of a button this code is called.

protected void Button1_Click(object sender, EventArgs e)
{

if (FileUploader.HasFile && FileUploader.PostedFile.ContentLength > 0)
{
string filename = txtFileName.Text;
//Read the file in to a byte Array.
byte[] filebyte = FileUploader.FileBytes;

System.Data.Linq.Binary fileBinary = new System.Data.Linq.Binary(filebyte);
MyDataDataContext MyData = new MyDataDataContext();

MyData.ADDFILE(filename, fileBinary);
lblStatus.Text = "File Uploaded Successfully!";

}
}

The Main thing to remember here is that we need to pass the FileUploader.FileBytes to the constructor of System.Data.Linq.Binary.

2.Rerieving Images

context.Response.ContentType = "image/jpeg";

MyDataDataContext ctx = new MyDataDataContext();

//This part os for to getting the image file from data base
Img img = ctx.Imgs.SingleOrDefault(c => c.id == 2);
byte[] ret = img.image.ToArray();

context.Response.BinaryWrite(ret);



Monday, November 16, 2009

How to Add FreeTextBox - Rich Text Editor - in C#

This tutorials explains how we can add custom controls to our project, and how to programmtically access the control's properties and methods. C# version.

ASP.NET's built-in controls are very useful, but unfortunately do not provide everything we want. Thankfully, ASP.NET allows us to create custom controls. This tutuorial will focus on how we can implement these custom controls for use in our projects.

We start by adding a Bin folder to our project. To do this, right-click the main project in the Solution Explorer and choose Add ASP.NET Folder > Bin. Once the Bin folder appears in the Solution Explorer, we can right-click this and then choose Add Existing Item. We can then simply browse to the location of the assembly file (.dll or .pdb). In this example, we are using the FreeTextBox control which can be downloaded from www.freetextbox.com and is the no.1 choice for a free HTML editor in ASP.NET Custom Controls. Once we have added the FreeTextBox.dll to our project, we can use it.

We can also add this to our toolbox for dragging & dropping like other controls, by right-clicking a blank area of the toolbox and choosing Choose Items. If FreeTextBox is not in the list, we can click on the Browse button to locate it. Once in the list, make sure it is selected (ticked).

Once you see the icon in the toolbar, drag it onto the page you want it to be displayed. Doing this will add the following to the top of your page:

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

And you will also see the following where we want the FreeTextBox custom control to be placed:

<"FTB:FreeTextBox ID="FreeTextBox1" runat="server"> '<'/FTB:FreeTextBox'>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

This is all that is needed for us to use the control within our page. However, if we want to interact with the control programmatically, we will have to add the assembly reference to the code-behind:

using FreeTextBoxControls;

The above code will allow us to reference the FreeTextBox in our code, and also allow us to access its properties and methods. However, if we are using a naming container such as a MasterPage, we will need to use the FindControl method as well. This will be done by writing code similar to this:

FreeTextBox myFreeTextBox = (FreeTextBox)Master.FindControl("FreeTextBox1");


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeTextBoxControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FreeTextBox1.Height = 150;
FreeTextBox1.Text = "We are using the <:b>Custom Control FreeTextBox<:/b> and changing its Text attribute programmatically.";
}
}


We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Or if we had to use FindControl, it would look something like this:



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeTextBoxControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FreeTextBox myFreeTextBox = (FreeTextBox)Master.FindControl("FreeTextBox1");
myFreeTextBox.Height = 150;
myFreeTextBox.Text = "We are using the <:b>Custom Control FreeTextBox<:/b> and changing its Text attribute programmatically.";
}
}

How To Add a Rich Text Box To Your ASP.net - C# -Web Site

You can use FCKEditor for your purpose.

I found a good link & like to share with.

http://www.codedigest.com/Articles/ASPNET/208_Integrating_FCKeditor_in_ASPNet_Websites_%E2%80%93_Part_1.aspx

http://www.codedigest.com/Articles/ASPNET/217_Integrating_FCKeditor_in_ASPNet_Websites_%E2%80%93_Part_2.aspx

Thursday, November 12, 2009

Exporting GridView to Excel Without installing Microsoft Office On The Server

In this code snippet, I will show how you can export data from a GridView control to a Microsoft Excel spreadsheet.

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}

private void BindData()
{
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
GridView1.DataSource = ds;
GridView1.DataBind();
}

private string ConnectionString
{
get { return @"Server=localhost;Database=NorthWind;Trusted_Connection=true"; }

}
protected void BtnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}


In the above listing, the GridView control will be populated with the data from the Categories table of the Northwind database. You will have to give the appropriate Server name or IP instead of localhost as server name in the above connection string.

If you use this code and try to export the GridView control, you will see an error message saying that the GridView control must be placed inside the form tags with the runat = server attribute.

This is pretty confusing, since your GridView is already inside the form tags and also contains the runat = server attribute. You can easily resolve this error by adding the following lines.

Listing 3: Overiding VerifyRenderingInServerForm Method

public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */

}

Yup, that’s it. Now, when you click the button, the GridView control will be exported correctly. It will prompt you either to open the file as it is or to save it elsewhere.

Get further details & download the sample here

Wednesday, November 11, 2009

Visual Studio 2008 code snippet for adding a new property

Now if you type prop and hit "tab" twice, this will bring the Automatic property not the conventional property declaration.

Thursday, November 5, 2009

Configure Search On sharepoint

The following procedures step you through the process of configuring Office SharePoint Server 2007 search services, creating a Web application for the SSP, creating the SSP, and configuring indexing settings.



Start and configure the Search service

1.On the SharePoint Central Administration home page, click the Operations tab on the top navigation bar.

2.On the Operations page, in Topology and Services, click Servers in farm.

3.On the Servers in Farm page, click the server on which you want to configure the search service.

4.Click Start next to Office SharePoint Server Search.

5.On the Office SharePoint Server Search Settings page, in the Query and Indexing section, make sure that the Use this server for indexing content and Use this server for serving search queries check boxes are selected.

6.In the Default Catalog Location section, type a path to a physical folder to store the index files, or use the default location that is specified.

7.In the Contact E-Mail Address section, specify a valid e-mail address.

8.In the Service Account section, click Configurable, and in User name and Password, type the user name and password for the user account under which you want the Search service to run. The user account must be a member of the Administrators group on the computer that is running the Search service. If you want to use the principle of least privilege and select a unique user account that does not have administrative rights on your front-end servers or on your back-end database servers, see the Known Issues/Readme for Office SharePoint Server 2007 Beta 2. The user name must be in the format DOMAIN\username.

9.In the Web Front End And Crawling section, do one of the following:
If you are configuring the search service on a server that provides Web services and renders Web content, click No dedicated Web front-end computer for crawling
If you are configuring the search service on a server that is a standalone search server that does not provide Web services and render Web content, click Use a dedicated web front end computer for crawling, and then, in Select a web front end computer, click the computer you want to use for crawling.

10.Click Start.

Start the Windows SharePoint Services Web Application service

You must start the Windows SharePoint Services Web Application service on every computer that you want to act as a Web server and was set up using the Complete option during Setup. This service is started by default on servers that were set up using the Web Front End option. To enhance security, you can leave this service turned off on application servers that do not provide Web content to client computers. Also, you do not need to turn this service on to use SharePoint Central Administration on a server.

1.On the SharePoint Central Administration home page, click the Operations tab on the
top navigation bar.

2.On the Operations page, in Topology and Services, click Servers in farm.

3.On the Servers in Farm page, click the server on which you want to start the Windows SharePoint Services Web Application service.

4.Click Start next to Window SharePoint Services Web Application.


Create the Shared Services Provider

1.On the SharePoint Central Administration home page, click the Application Management tab on the top navigation bar.

2.On the Application Management page, in the Office SharePoint Server Shared Services section, click Create or configure this farm's shared services.

3.On the Manage this Farm's Shared Services page, click New SSP. Important: If you have not created a Web application for the SSP administration site, you need to create one before you create the SSP. If you have already created a Web application for the SSP administration site, skip to step 14.

4.On the New Shared Services Provider page, click Create a new Web application.

5.On the Create New Web Application page, in the IIS Web Site section, click Create a new IIS web site, and do not modify the default settings in this section.

6.In the Security Configuration section, under Authentication provider, select the appropriate option for your environment, and do not modify the default settings in the remainder of this section.

7.In the Load Balanced URL section, do not modify the default settings.

8.In the Application Pool section, click Create new application pool.

9.In Application pool name, enter the name of your application pool or use the default name.

10.Click Configurable, and in User name and Password, type the user name and password for the user account under which you want the application pool to run. The user account does not have to be a member of any particular security group. It is recommended that you use the principle of least privilege and select a unique user account that does not have administrative rights on your front-end servers or on your back-end database servers. You can use the user account that you specified as the Office SharePoint Server 2007 service account; however, if that user account is a member of a security group that has administrative rights on your front-end servers or your back-end database servers, you will not be following the principle of least privilege. The user name must be in the format DOMAIN\username.

11.In the Database Name and Authentication section, verify the database information and make sure that Windows Authentication (recommended)is selected.

12.In the Search Server section, do not modify the default settings.

13.Click OK. Upon successful creation of the Web application, the New Shared Services Provider page appears.

14.In the SSP Name section, in Web Application, select the Web application that you created for the SSP, and do not modify any of the default settings in this section.

15.In the My Site Location section, do not modify any of the default settings.

16.In the SSP Service Credentials section, in User name and Password, type the user name and password for the user account under which you want the SSP to run. The user account does not have to be a member of any particular security group. It is recommended that you use the principle of least privilege and select a unique user account that does not have administrative rights on your front-end servers or on your back-end database servers. You can use the user account that you specified as the Office SharePoint Server 2007 service account; however, if that user account is a member of a security group that has administrative rights on your front-end servers or your back-end database servers, you will not be following the principle of least privilege. The user name must be in the format DOMAIN\username.

17.In the SSP Database section, you can either accept the default settings (recommended), or specify your own settings for the database server, the database name, or the SQL authentication credentials.

18.In the Search Database section, you can either accept the default settings (recommended), or specify your own settings for the search database server, the database name, or the SQL Server authentication credentials.

19.In the Index Server section, in Index Server, click the server on which you configured the Search service. Note: If there is no index server listed in the Index Server section, then no server in your farm has been assigned the index server role. To assign the index server role to a server in your farm, follow the instructions in the "Configure the Search service" section earlier in this topic.

20.In the SSL for Web Services section, click No.

21.Click OK. Upon successful creation of the SSP, the Success page appears.

22.On the Success page, click OK to return to the Manage this Farm's Core Services page.

Configure indexing settings

1.On the SharePoint Central Administration home page, click the Application Management tab on the navigation bar.

2.On the Application Management page, in the Office SharePoint Server Shared Services section, click Create or configure this farm's shared services.

3.On the Manage this Farm's Shared Services page, click SharedServices1.

4.On the Shared Services Administration page, in Search, click Search Settings.

5.On the Configure Search Settings page, in the Crawl Settings section, click Default content access account.

6.In the Default content access account section, in Account, Password, and Confirm Password, type the user name and password for the user account that you want to use to crawl content on your sites. This account must be a domain user account. It is recommended that you use the principle of least privilege and select a unique user account that cannot modify content and does not have administrative rights on your front-end servers or on your back-end database servers. You can use the user account that you specified as the Office SharePoint Server 2007 service account; however, if that user account is a member of a security group that has administrative rights on your front-end servers or your back-end database servers, you will not be following the principle of least privilege. The user account that you specify will be added to the Web application Full Read policy for your farm. The user name must be in the format DOMAIN\username.

7.Click OK.

8.In the Crawl Settings section, click Content sources.

9.On the Manage Content Sources page, click Local Office SharePoint Server sites.

10.On the Edit Content Source page, in the Crawl Schedules section, under Full Crawl, click Create schedule.

11.In the Manage Schedules dialog box, configure schedule settings for full crawls of your content, and then click OK.

12.In the Crawl Schedules section, under Incremental Crawl, click Create schedule.

13.In the Manage Schedules dialog box, configure schedule settings for incremental crawls of your content, and then click OK.

14.In the Start Full Crawl section, select the Start full crawl of this content source check box, and then click OK.


You are done!