Tuesday, June 1, 2010

Validating whether email id was already used using jquery for ajax & c# at backend

we have seen many times when we are filling up any online form as soon as we entered our email id many sites at that times only prompts us whether that email id id already used or not using ajax.
Doing such task is very very easy through jquery

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: yoururl+methodName,
data: "{'emailid':'"+emailidtoCheck+"'}",
dataType: "json",
async: false,
success: function(res)
{ alert('email is free');return true}
error: function(obj)
{ alert('email is already used');return false}
});

if method is wriiten on the same c# page then u only have to pass the page name and method name no need to write FQDN

At backend make one [webmethod]
grab the parameters check ur database,return true or false depending upon whether email id exists or not.

For this u also need to add jquery as a script


:)

Monday, May 31, 2010

XML,XSLT and Jquery

Parsing an xml with xslt is an tedious job.If u want to parse xml with xslt on HTML page u need to do a really hard work.
But it can be easy also using Jquery Plugins.Using Jquery Plugins u can call xml and xslt dynamically using ajax and parse them.
You can use this example for your help http://www.jongma.org/webtools/jquery/xslt/

Wednesday, April 8, 2009

How to handle events like paging,editing,update,cancel in gridview,datagrid?

In the case of Edit event
DatagridName.EditItemIndex = e.Item.ItemIndex;
datagrid.DataSource="Tablename";
datagrid.DataBind();

In case of Cancel event

Datagridname.EditItemIndex = -1;
datagrid.DataSource="Tablename";
datagrid.DataBind();

In case of paging

Datagridname.CurrentPageIndex = e.NewPageIndex;
datagrid.DataSource="Tablename";
datagrid.DataBind();


In case of updating
Datagridname.EditItemIndex = -1;
datagrid.DataSource="Tablename";
datagrid.DataBind();

Monday, March 30, 2009

how to Cache an object in asp.net using c#?

Suppose you want to cache an object say like datatable,you can easily do so by

Cache.Insert("DtIndustry", DtIndustry, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);

The above command cache the datatable called DtIndustry from 10 min.

Next if u want that object first check the cache first if cache is null then fill that datatable and again add that object into cache.
if (Cache["DtIndustry"] == null)
{
DtIndustry = objDll.retieve_DtIndustry();
Cache.Insert("DtIndustry", DtIndustry, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration); ;
}
else
{
DtIndustry = (DataTable)Cache["DtIndustry"];
}

Tuesday, March 24, 2009

Error while opening connection with sql as connection is already open using asp.net and c#?

There may e scenario where you are getting error while establishing connection with the database as connection is already open.

it can be easily solved using
if (mysqlcmd.Connection.State == ConnectionState.Closed)
{
mysqlcmd.Connection.Open();
}

//same for closing the connection
if (mysqlcmd.Connection.State == ConnectionState.Open)
{
mysqlcmd.Connection.Close();
}

Friday, March 20, 2009

How to add new column in data table in asp.net c#?

Requirment may arise when you want to add column in data table at runtime.It is a really simple task to do.

dt.Columns.Add(new DataColumn("Column1", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Column2", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Column3", System.Type.GetType("System.String")));

Tuesday, March 17, 2009

How to send email using asp.net and c#?

Code for sending email:

using System.Web.Mail;
private void Button1_Click(object sender, System.EventArgs e)

{

MailMessage mail = new MailMessage();

mail.To = txtTo.Text;

mail.From = txtFrom.Text;

mail.Subject = txtSubject.Text;

mail.Body = txtBody.Text;

SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);
}


You can also send email using
System.Net.Mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.IsBodyHtml = true;
mail.Headers.Add("Precedence", "bulk");//If u want to send mail in bulk

mail.From = new System.Net.Mail.MailAddress("EmailId", "Sender Name");
mail.ReplyTo = new System.Net.Mail.MailAddress("EmailId", "Name");
mail.Subject = " Subject";
System.Net.Mail.Attachment MailAttach = new System.Net.Mail.Attachment("path");
mail.Attachments.Add(MailAttach);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp server name", "port no");
smtp.Credentials = new System.Net.NetworkCredential("User Id for that email", "Password");
smtp.EnableSsl = true;
mail.To.Add("Reciever Email Id ");
mail.Body = "Body Text";
smtp.Send(mail);