October 24, 2007

Uploading a file or image in C#.net and storing in a directory or a folder

Your ASPX Code will look like this:

In Ur aspx.cs just write the code on the button click

protected void Submit1_ServerClick(object sender, EventArgs e)
if (imageup.PostedFile != null)//Checking for valid file
//Since the postedfile.FileNameFileName gives the entire path we use substring functio to rip of the filename alone.
string StrImageName = imageup.PostedFile.FileName.Substring(imageup.PostedFile.FileName.LastIndexOf("\\") + 1);
string StrImageType = imageup.PostedFile.ContentType;
int IntImageSize = imageup.PostedFile.ContentLength;
//Checking for the length of the file.If Length is 0 then the file is not uploaded.
if (IntImageSize <= 0)
Response.Write("Uploading of Image" + StrImageName + "failed");
Response.Write("Your File" +StrImageName + "of type" + StrImageType +" and size"+IntImageSize.ToString() +"bytes was uploaded sucessfully");
img.ImageUrl = ("upload" +"/"+ StrImageName);

October 12, 2007

Selecting Checkboxes inside the GridView Control:

How to select the checkboxes inside the Gridviewcontrol

After seraching many forums and websites i have developed my project by using this code.
Now in the button click event write this code which i have attached in blog as image:

// StringBuilder object

StringBuilder str = new StringBuilder();

// Select the checkboxes from the GridView control

for (int i = 0; i < GridView1.Rows.Count; i++)


GridViewRow row = GridView1.Rows[i];

bool isChecked = ((CheckBox) row.FindControl("chkBox")).Checked;

if (isChecked)


// Column 2 is the name column




// prints out the result


Note:1.If u want to have StringBuilder then u have to include the namespace Using.System.Text
2.If u want to delete the datas after getting the Id then u should rewrite the code as
so that u will get the output.For ex:1,2,(inorder to delete from database)
other wise if u use
u will get the output as 12.

October 10, 2007

Exporting Gridveiw data int o excel file

hi guys exporting files from gridview is quite simple

u have to just put this code inside buttononclick
protected void btnExcel_Click(object sender, EventArgs e)
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=My Report_" + System.DateTime.Today.Date.ToShortDateString().Replace("/", "_") + ".xls");
Response.Charset = "";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


if u compile this code i wont work finely.

All you need to do is to override the VerifyRenderingInServerForum event as I have shown below.

public override void VerifyRenderingInServerForm(Control control)

// Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.


After that if it shows the error

Control 'Gridview1' of type 'GridView' must be placed inside a form tag with runat=server

Then open user .aspx file in that u just add the attribute EnableEventValidation="false" like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="accounting.aspx.cs" Inherits="accounting" EnableEventValidation="false" %>

Now ur code will works fine

October 6, 2007

Connection strings in asp.net

Before writing connection string to ur C#.net applications
First include System.Data.SqlClient Namespace

The System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server.

The .NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space. Using the SqlDataAdapter, you can fill a memory-resident DataSet, which you can use to query and update the database.

For more details about click System.Data.SqlClient Namespace

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection("server=SRISHTI-THILLAI;uid=sa;pwd=1234;database=Teeth");
here SRISHTI_THILLAI is the server name
1234 is the password of your server
Teeth is the database name

October 5, 2007

How to make Apache run ASP.NET / ASP.NET 2.0

Don’t ask me why… but i’ve been asked to make Apache run ASP.NET.

IT Worked !

Even worked with ASP.NET 2.0 Site !

Following are the instruction to make Asp.Net work under apache:

– Install Apache 2.0.54

– Install Mod_AspDotNet

– Add at the end of C:\Program Files\Apache Group\Apache2\conf\httpd.conf the following lines

LoadModule aspdotnet_module "modules/mod_aspdotnet.so"

AddHandler asp.net asax ascx ashx asmx aspx axd config cs csproj licx rem resources resx soap vb vbproj vsdisco webinfo

# Mount the ASP.NET /asp application
AspNetMount /SampleASP "c:/SampleASP"
#/SampleASP is the alias name for asp.net to execute
#"c:/SampleASP" is the actual execution of files/folders in that location

# Map all requests for /asp to the application files
Alias /SampleASP "c:/SampleASP"
#maps /SampleASP request to "c:/SampleASP"
#now to get to the /SampleASP type http://localhost/SampleASP
#It'll redirect http://localhost/SampleASP to "c:/SampleASP"

# Allow asp.net scripts to be executed in the /SampleASP example

Options FollowSymlinks ExecCGI
Order allow,deny
Allow from all
DirectoryIndex index.htm index.aspx
#default the index page to .htm and .aspx

# For all virtual ASP.NET webs, we need the aspnet_client files
# to serve the client-side helper scripts.
AliasMatch /aspnet_client/system_web/(\d+)_(\d+)_(\d+)_(\d+)/(.*) "C:/Windows/Microsoft.NET/Framework/v$1.$2.$3/ASP.NETClientFiles/$4"

Options FollowSymlinks
Order allow,deny
Allow from all


– Create a directory c:\SampleASP and insert in it the index.aspx

– Restart apache server :
Start-> Apache HTTP Server 2.0.54 ->
Control Apache Server -> Restart

– Open Explorer and navigate to http://localhost/SampleASP/index.aspx

If everything worked fine you should get a nice asp.net page working.