Archive

Archive for June, 2012

Magic !! Access aspx page without aspx page

June 26, 2012 Leave a comment

Suppose you have a Users folder in your web application

there is Default.aspx page inside Users folder. if you access a page like http://yoursite.com/users/anyname.aspx will show error.

Using HTTP handler we can handle the request coming to any aspx page

Steps

1. Create a website in Visual studio

2. Create a folder as “users”

3. Create a an aspx page Default.aspx inside “users” folder

4. Right Button solution -> Add new item -> Class -> enter class name as Handler

Code inside this handler look like this

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

///<summary>

///Summary description for Handler

///</summary>

public class Handler : IHttpHandler

{

public Handler()

{

//

// TODO: Add constructor logic here

//

}

public bool IsReusable

{

get { return false; }

}

public void ProcessRequest(HttpContext context)

{

string vPath = context.Request.RawUrl;

int vIndexOfFolder = vPath.IndexOf(“/users”,StringComparison.OrdinalIgnoreCase);

if (vIndexOfFolder > 0)

{

string vUserName = vPath.Substring(vIndexOfFolder + 7);

//remove aspx extension

vUserName = vUserName.Substring(0, vUserName.Length – 5);

context.Response.Write(“Welcome “+vUserName);

}

}

}

5. Edit web.config with handler settings

<httpHandlers>

<add verb=”*” path=”users/*.aspx” type=”Handler”/>

</httpHandlers>

Access http://yoursite.com/users/anyname.aspx  !!

you can check the name is available in database and go for further processing.

Categories: ASP.NET

SQLite in .Net

June 25, 2012 Leave a comment

SQLite with .Net

Steps

1. Download and Install ADO.NET 2.0 Provider for SQLite from here http://sourceforge.net/projects/sqlite-dotnet2/

2. Create a website using Visual studio 2010

3. Add a reference to dll file C:\Program Files (x86)\SQLite.NET\bin\System.Data.SQLite.dll

4. Use Server Explorer to connect a SQLite database. Click Connect to database from Server Explorer. Select SQLite Database file -> Continue -> New -> type database filename -> Save -> OK

5. Check the connection string of newly created SQLite database from Server explorer

6.  Sample code

using System.Data.SQLite;

SQLiteConnection  conn = newSQLiteConnection();

conn.ConnectionString =@”data source=C:\Users\uerid\Documents\testdb.db”;

conn.Open();

SQLiteCommand command = conn.CreateCommand();

command.CommandText =“CREATE TABLE Test (id integer primary key AUTOINCREMENT, text varchar(100))”;

command.ExecuteNonQuery();

command = conn.CreateCommand();

command.CommandText =“INSERT INTO Test (text) VALUES (@1)”;

SQLiteParameter parameter = command.CreateParameter();

parameter.ParameterName =“@1”;

parameter.Value =“some text”;

command.Parameters.Add(parameter);

command.ExecuteNonQuery();

Categories: .NET Tags: