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);

Monday, March 16, 2009

How to check that the file which user is uploading should be only word file using asp.net RegularExpressionValidator?

ControlToValidate="FileUpload" Display="Dynamic" ErrorMessage="Upload a valid file"
ValidationExpression="^.+(.DOC|.DOCX|.doc|.docx)$">

Thursday, March 12, 2009

how to Post data from html using asp.net as a backend?

Some times you want to post your data from html page..
do this was a really simple task

<form method="post" action="URL" id="frmAddmissionEnquiry">
<input type="text" id="txtname" />
<input type="text" id="txtEmail" />
</form>


Capturing the same in asp page.
Request.Form["txtname"].ToString()


Returing back to the same form
Response.Redirect(Request.UrlReferrer.ToString());