Florence Blogspot about asp.net tutorials and web design and web development

Monday, July 6, 2009

Asp.Net CAPTCHA and Asp.Net AJAX CAPTCHA

Asp.Net CAPTCHA and Asp.Net AJAX CAPTCHA

I am using a great Asp.Net CAPTCHA by BrainJar in a number of web sites with and without Asp.Net AJAX. It’s a simple and really easy to use Asp.Net CAPTCHA. The actual source code is in C#, but you can use it with both C# and VB.Net by simply wrapping the functionality in a class library.

In Asp.Net forums and in many other user communities I have seen lot of people asking for VB.Net CAPTCHA. So I thought to write a blog post and create some sample implementations. The zip file contains C# CAPTCHA and VB.Net CAPTCHA. I have included the samples for Asp.Net AJAX CAPTCHA also.

You can download the samples and implementation from here

Implementing Asp.Net AJAX CAPTCHA is really simple. Just wrap the main Asp.Net CAPTCHA with Asp.Net AJAX update panel and put a random query string at the end of CAPTCHA image src. The random query string will avoid showing the old CAPTCHA from browser cache.

STEPS TO ADD ASP.NET CAPTCHA IN YOUR WEBSITE
  1. Refer the assembly CaptchaDLL.dll in your project
  2. Copy JpegImage_CS.aspx or JpegImage_VB.aspx (according to the language of choice) to your website.
  3. Open the above file and make changes in Colors and Font if needed. (read the inline comments to know more)
  4. Now user the sample codes from Default.aspx or Ajax.aspx pages. The code is straight forward. You make a session and generate an image with the string in the Session. Now when you submit you have to check the session value and textbox value to see whether the entered CAPTCHA is correct.
DOWNLOAD THE SOURCE AND SAMPLES FROM HERE

If you have any questions please put as a comment.

Wednesday, June 3, 2009

Date Formatting in C#

Date Formatting in C#

Cheat sheet

<%= String.Format("{specifier}", DateTime.Now) %>


Specifier Description Output
d Short Date 08/04/2007
D Long Date 08 April 2007
t Short Time 21:08
T Long Time 21:08:59
f Full date and time 08 April 2007 21:08
F Full date and time (long) 08 April 2007 21:08:59
g Default date and time 08/04/2007 21:08
G Default date and time (long) 08/04/2007 21:08:59
M Day / Month 08 April
r RFC1123 date Sun, 08 Apr 2007 21:08:59 GMT
s Sortable date/time 2007-04-08T21:08:59
u Universal time, local timezone 2007-04-08 21:08:59Z
Y Month / Year April 2007
dd Day 08
ddd Short Day Name Sun
dddd Full Day Name Sunday
hh 2 digit hour 09
HH 2 digit hour (24 hour) 21
mm 2 digit minute 08
MM Month 04
MMM Short Month name Apr
MMMM Month name April
ss seconds 59
tt AM/PM PM
yy 2 digit year 07
yyyy 4 digit year 2007
: seperator, e.g. {0:hh:mm:ss} 09:08:59
/ seperator, e.g. {0:dd/MM/yyyy} 08/04/2007

Friday, May 29, 2009

Read the emailID from txt file

Read the emailID from txt file:
_____________________________________

Using Regular Expression, we can do this.

the textfile format may be in any format like

sdfsd

danasegarane@test.com,

test@test.com,

me@me.com

sdf

sdfs

dfsd


First Method:

string pattern = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);

//Read file
string sFileContents =System.IO.File.ReadAllText(Server.MapPath("Email.txt"));

System.Text.RegularExpressions.MatchCollection mc = reg.Matches(sFileContents);

//string array for stroing
System.Collections.Generic.List str = new System.Collections.Generic.List();foreach (System.Text.RegularExpressions.Match m in mc)

{

str.Add(m.Value);

}

OR

Second Method:

using System.Text.RegularExpressions;
using System.IO;

try
{
//the file is in the root - you may need to change it
string filePath = MapPath("~") + "/EmailText.txt";

using (StreamReader sr = new StreamReader( filePath) )
{
string content = sr.ReadToEnd();
if (content.Length > 0)
{
//this pattern is taken from Asp.Net regular expression validators library
string pattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
MatchCollection mc = Regex.Matches(content, pattern);
for (int i = 0; i <>

Monday, May 25, 2009

Export Data To Excel using ADO.Net

Excel Workbook is just like database with sheets corresponding to tables. See the mapping below.



Database <—————> Excel Workbook


Sheet <—————-> Table



Connection String for Excel 97-2003 Format (.XLS)


For Excel 97-2003 format Microsoft Jet OLEDB Driver 4.0 is used. A sample connection string as follows.


"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties='Excel 8.0;HDR=Yes'"




Connection String for Excel 2007 Format (.XLSX)


For Excel 2007 format the new Microsoft Ace OLEDB Driver 12.0 is used. A sample connection string as follows.


"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Book1.xlsx;Extended Properties='Excel 8.0;HDR=Yes'"



Rest everything is same for both versions. One thing to note Microsoft Ace OLEDB Driver 12.0 works for both Excel 2003 and Excel 2007 format.



You can specify whether your Excel file has Headers or not using the HDR property.


When HDR is set to Yes the First Row is considered as the Header of the Excel file.





Establish a Connection



String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;"


+ "Data Source=Book1.xls;"


+ "Extended Properties='Excel 8.0;HDR=Yes'";



OleDbConnection connExcel = new OleDbConnection(strExcelConn);


OleDbCommand cmdExcel = new OleDbCommand();


cmdExcel.Connection = connExcel;





Accessing Sheets



connExcel.Open();


DataTable dtExcelSchema;


dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);


connExcel.Close();



The dtExcelSchema contains all the Sheets present in your Excel Workbook


You access them in the following way


string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"];



This will give the name of the first sheet. i.e. Sheet1$



Create a new sheet


cmdExcel.CommandText = "CREATE TABLE [tblData]" +


"(ID varchar(10), Name varchar(50));";


connExcel.Open();


cmdExcel.ExecuteNonQuery();


connExcel.Close();



The above code creates a new Sheet in the Excel Workbook with the name tblData with two columns ID and Name.


Insert Record into Sheet


connExcel.Open();


cmdExcel.CommandText = "INSERT INTO [tblData] (ID, Name)" +


" values ('1', 'MAK')";


cmdExcel.ExecuteNonQuery();


connExcel.Close();





Update existing Record into Sheet


connExcel.Open();


cmdExcel.CommandText = "UPDATE [tblData] " +


"SET Name ='John' WHERE ID = '1'";


cmdExcel.ExecuteNonQuery();


connExcel.Close();

Search