MY bLOG

Saturday, 13 July 2013

How to upload images using file upload control in asp.net

Here how to upload images usig fileupload control

In aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1 align="center" style="color: #FF00FF">File Upload Demo</h1>
    <table align="center"><tr><td>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
       <td><asp:TextBox  ID="TXT_NAME" runat="server"></asp:TextBox></td></tr>
       <tr><td>Image:</td><td><asp:FileUpload ID="fileupload1" runat="server" /></td></tr>
       <tr><td>
           <asp:Image ID="Image1" runat="server" Height="182px" Width="172px" /></td></tr>
       <tr><td colspan="2" align="center">
           <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></td></tr>
        </table>
    </div>
    </form>
</body>
</html>

In aspx.cs page:

 protected void Button1_Click(object sender, EventArgs e)
    {
     
        if (fileupload1.HasFile)
        {
            if (fileupload1.PostedFile.ContentType == "image/jpeg" || fileupload1.PostedFile.ContentType == "image/gif" || fileupload1.PostedFile.ContentType == "image/x-png" && fileupload1.PostedFile.ContentLength == 1024)
            {
                string path = "~/Images/" + fileupload1.FileName;
                fileupload1.SaveAs(Server.MapPath(path));


                Image1.ImageUrl = "~/Images/" + fileupload1.FileName;
                Image1.Visible = true;
            }
Output:





Tuesday, 9 July 2013

How to send an Email using asp.net?


Sending mail from asp.net:

   When we are working with mail system we need to have SMTP server, SMTP server will request from web site and will deliver the mail request to other SMTP servers associated with other websites.


Here iis and smtp are servers.when ever any SMTP server will be seen any mail msg then it will send the request of that mail to the web site.
So at the time of sending a mail we need to set our web site as client to the SMTP server prepare a mail msg
and send to smtp server.

To perform this we use three namespaces in asp.net
                     1)using system.net;
                     2)using system.net.mail;
                     3)using system.web.mail;

System.Net:
                         This namespace contains an important class known as network credentials.

System.Net.Mail:
                  
                       classes available in the namespaces are
                               1. Attachment
                               2.Mail Address.  
                               3.SMTP client
                               4.SMTP Exception
                               5.SMTP failed recipient exception
                               6.SMTP permission
System.Web.Mail:
                             Classes available in the namespaces are             
                          Mail Attachment
                           Mail message
                           SMTP Mail.     

By using this we can write a code
Source Code:

<body>
    <form id="form1" runat="server">
    <div>
    <h1 align="center">Sending an Email</h1>
    <table align="center">
    <tr><td>To:</td><td><asp:TextBox ID="txt1_to" runat="server"></asp:TextBox></td></tr>
     
    <tr><td>subject:</td><td><asp:TextBox ID="txt_sub" runat="server"></asp:TextBox></td></tr>
     
      <tr><td>Body:</td><td><asp:TextBox ID="txt_body" runat="server"></asp:TextBox></td></tr>
     
      <tr><td>Attachment:</td><td><asp:FileUpload ID="fle1" runat="server" /></td></tr>
     
       <tr><td colspan="2" align="center"><asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /></td></tr>
        </table>
    </div>
    </form>
</body>
</html>
Code Behind:
in an button click event

 protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage("aswinialuri@gmail.com", txt1_to.Text);
       
        msg.IsBodyHtml = true;
        msg.Subject=txt_sub.Text.ToString();
        msg.Body=txt_body.Text.ToString();
        if(fle1.HasFile)
        {
            msg.Attachments.Add(new Attachment(fle1.PostedFile.InputStream, fle1.FileName));
        }
        NetworkCredential nc = new NetworkCredential("aswinialuri@gmail.com", "yourpwd@");
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = nc;
        smtp.Send(msg);
    }














Sunday, 7 July 2013

Features of DotNet Framework

Features of dotnet framework are
1. CLR(Common language Run time)
2. BCL or FCL(Base class library or framework class library)

   CLR has sub components
      
         1. CLS (Common Language Specification)
         2. CTS (Common Type System)
         3. GC( Garbage Collector)
         4. JIT(Just In Time Compiler) 

CLS:
             It is responsible to provide language interoperability
             It can be achieved by two ways managed and unmanaged code.
         
A code is generated by MSIL then it is called as managed code.
A code is generated with the help of  OS is called un managed code.
The process of execution of managed and unmanaged code is called language independency and language interoperability.

Language Specification:
Every programming language has its own language specification.
But CLR can understand all the programming language because of language specification.
Any language compiler should follow this language specification of CLR at the time of compilation and should generate MSIL,and CLR'S JIT compiler will generate native code from MSIL and CLR will execute it.

CTS:
      * Every language will have its own datatype system.
      *CLR supports the execution of most of the programming languages the CLR must understand the datatypes of the all the programming languages.
     *CLR contains its own data type system called as common type system.
     * All the data types presenting different languages will be converted to common type system before comming for the execution to the CLR.

EX:
        C#.Net               VB.Net
        int x ;                    Dim x as integer    
by using CTS it can accept all type of datatypes
system.int32

GC:
     Garbage collector is responsible to provide automatic memory management
By using automatic memory management
There do not be any problem of insufficient memory
it reduces the burden of programmer.
Garbage collector has its own engine known as optimize engine which runs when required and divide objects into two categories.
->Objects in use and
->idled objects.
Objects in use are kept in memory and idled objects are destroyed from the memory.

JIT Compiler:
converting MSIL code to machine code by the just in time compiler .

BCL:
It is also known as framework class library

C# library:collection of Assemblies
                           |
Collection of name spaces
                        |
collection of classes and interfaces