All posts in ASP.NET

Creating custom exceptions in asp.net

There is lots of exception or error handling in .NET. But if you want to create a custom error exception for your particular code then you can create your own error handling. We need to create a custom exception in our business layer so that the customer can’t use the same code in other domain. He can only use that code in particular domain or activation key. You can build you own login to make this work with you activation key. I just show how can you restricted them to you other domain.

So we restricted the user not to use our application other then www.learneveryday.net or containing learneveryday

at first i create a custom exception class DomainValidationException and inherit the System.ApplicationException
Continue reading →

‘HttpUtility’ does not exist in the current context

Last couple days working I developed a social networking share control for asp.net

http://www.smartwebsource.info/smartshare/Default.aspx

Where i use BitlyApi to shorten my url to send twitter.
There i use BitlyApi class in my App_Code folder . As it is web project so HttpUtility is found from System.Web.dll.
I try to make my SmartSocialShare class library to a dll so i make a new class library .
And paste all my Class file from my App_Code folder in the new Class library project . When i try to build the dll it show this error.

‘HttpUtility’ does not exist in the current context

Then i add System.Web.dll as a reference in my class library then it make my dll without error.
Hopes this will help you to solve this error.

Send email with attachment in asp.net

This tutorial will show you how to send a simple email message with an attachment using ASP.NET 2.0

Sending a email with an attachment using ASP.NET 2.0 is actually very simple.

First, you will need to use the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient class

 
using System.Net.Mail;

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/smart-social-share/160097" title="Smart Social Share (Asp.net control)" description="Smart Social share is a asp.net control . Which helps you to give user to share your content with social book mark site." ref="marifdu"]

Bellow is the form of our html

 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="qform">
        
            <p>
                <label>
                    Name <b>*</b></label><br />
                <asp:TextBox ID="_txtName" runat="server" CssClass='qtxt required' />
            </p>
            <p>
                <label>
                    Email Address <b>*</b></label><br />
                <asp:TextBox ID="_txtEmail" runat="server" CssClass='qtxt required email' />
            </p>
            <p>
                <label>
                    Subject <b>*</b></label><br />
                <asp:TextBox ID="_txtSubject" runat="server" CssClass='qtxt required' />
            </p>
            <p>
                <label>
                    Description of Question</label><br />
                <asp:TextBox ID="_txtQuestion" runat="server" TextMode="MultiLine" Columns="51" Rows="6" />
            </p>
         <p>
                <label>
                    Attachment </label><br />
           
            <asp:FileUpload ID="fuAttachment" runat="server" />
            </p>
            <p>
                <asp:Button runat="server" ID="sendq" Text="Send" OnClick="sendq_Click" />
            </p>
            <p><asp:Label runat="server" ID="lblMessage"></asp:Label></p>        </div>
    </div>
    </form>
</body>
</html>

Code behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void sendq_Click(object sender, EventArgs e)
    {
   

        string filename = "";
     
        String rootPath = "";
        if (fuAttachment.HasFile)
        {
            string dirUrl = Path.Combine(Request.PhysicalApplicationPath, "attachment/");



            filename = fuAttachment.PostedFile.FileName;
            string fullUploadPath = Path.Combine(dirUrl, fuAttachment.PostedFile.FileName);

            fuAttachment.PostedFile.SaveAs(fullUploadPath);

           
            rootPath = Server.MapPath("~/attachment/" + filename);

        }


        
        var subject = _txtSubject.Text;
        var from = _txtEmail.Text;
     
        var to = "m_arifdu@yahoo.com";
        var questoin = _txtQuestion.Text;
        var body = string.Format("A new file send with attachment");


        Mailer.Send(from, to, subject, body, true, rootPath);

        lblMessage.Text = "Mail send with attachment";

       
    }
}
 

In our web.config file we con fig our mail credential. As we send mail with gmail so we put here gmail settings.
To send mail with gmail please enable ssl from our mailer class. As google need ssl to send mail. Other wise you will get error.

  <system.net>
    <mailSettings>
      <smtp>
        <network port="587" host="smtp.gmail.com" userName="username" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>

 

We make a mailer class to send mail here is Our Mailer class

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;


    public class Mailer
    {
        /// <summary>
        /// Send Email without attachment
        /// </summary>
        /// <param name="messagefrom">Email from address</param>
        /// <param name="toaddress">Email to address</param>
        /// <param name="subject">Email subject</param>
        /// <param name="body">Email body</param>
        /// <param name="ishtml">True if the body contains HTML</param>
        /// <param name="fileattachment">attachment</param>
        /// <returns></returns>
    
        public static bool Send(string messagefrom, string toaddress, string subject, string body, bool ishtml, string fileattachment)
        {
            MailMessage message;
            //MailAddress mfrom, mto;
            SmtpClient sc = new SmtpClient();

            sc.EnableSsl = true;  //  this need for some domail need ssl // to send mail with gmail you need to true this value
            try
            {

                message = new MailMessage();
                message.To.Add(toaddress);
                message.From = new MailAddress(messagefrom);

                message.Subject = subject;
                message.Body = body;

                message.IsBodyHtml = ishtml;

                if (fileattachment != "")
                {

                    Attachment attachment = new Attachment(fileattachment);

                    message.Attachments.Add(attachment);

                }
                sc.Send(message);

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }


 

Get URL of ASP.Net Page

Some time we need to get the current page location . Today i will show how to do that.

This function can be used to retrieve the current page’s name, i.e default.aspx, hello.aspx or whatever.

 
public string GetCurrentPagename()
{

      string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);    
string sRet = oInfo.Name;  
return  sRet ;
}

If you want to need other things to retrive from url like bellow image then just call that.

Insert Html Tags Inside XML Node

How to insert html tag inside xml ?

Today i face a problem to add html tag inside my xml file.
If i add html tag inside my xml then when i try to load xml data by dataset and want to load that data in datagrid view then it shows error that it didn’t know my tag in which i add html tag.

So to solve this problem just add before your html text and add ]]>
After your text Like

     <![CDATA[
        <p>You can add your all desctioption by SeoPage.aspx and call
      SmartMetaGenerator.SeoSmartMetaGenerator("Faq.aspx",this); function in your page load event .<br/> And you will see it afect in you page.
      We add our  Faq.aspx Title <b>"This Faq Page  Title ! Frequently asked question page.</b> and Description <b>
        This Contact Title for faq page. </b>  <br/> KeyWord <b>Faq, qustion, asked smart seo pack</b> Then we run our faq.aspx page in browser
        and see our page source view and title which show the bellow image.<p>
        ]]>