Upload Multiple Files in Asp.Net

Upload Multiple Files in Asp.Net

In this tutorial i will let you know that how can you upload multiple files in asp.net at once, Here i am using FileUpload control for this no other functionality is required.

Design View:-

<%@ 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>
    <style type="text/css">
        .style1
        {
            width: 900px;
            float: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center" class="style1">
            <tr>
                <td>
                    File1
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    File 2
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload2" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    File 3
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload3" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    File 4
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload4" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload All Files" />
                    <br />
                    <br />
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code:-

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 Button1_Click(object sender, EventArgs e)
    {
    HttpFileCollection Files = Request.Files;
    for (int fileCount = 0; fileCount < Files.Count; fileCount++)
    {
    HttpPostedFile uploadedFile = Files[fileCount];
    string file = Path.GetFileName(uploadedFile.FileName); 
    uploadedFile.SaveAs(Server.MapPath("~/Files/") + file);
    Label1.Text += file + "<b>  Following File Has Been Saved</b> <br>";
    }
    }
}

Enjoy…

Posted in Asp.net | Tagged , , , | Leave a comment

GridView Auto Refresh

GridView Auto Refresh

In this tutorial i will let you know how to auto refresh or update GridView using AJAX Timer and UpdatePanel in asp.net. In this it is described that how a GridView is update after a specific time set in timer control.

Design View:-

<%@ 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">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Timer ID="Timer1" Interval="10000" runat="server" ontick="Timer1_Tick">
        </asp:Timer>
     <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
        <asp:Label ID="update" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
    </asp:UpdatePanel>
   
    </form>
</body>
</html>

Code:-

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        databind();
        update.Text = " Gridview Was Last Updated at " + DateTime.Now;
    }

    public void databind()
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        SqlConnection conn = new SqlConnection(strcon);
        conn.Open();
        string str = "select * from dept";
        SqlCommand cmd = new SqlCommand(str, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();


    }
}

Posted in Asp.net | Tagged , , , , | Leave a comment

Send Email with Attachment in Asp.Net

send email with attachment in asp.net

In this tutorial i will let you know how to Send Email with Attachment in Asp.Net.I am saving this file into memory stream rather saving it on server.

Here i am using Gmail smtp to send mail from Asp.net code.For this first we have to add the following namespaces.

Design your page according to your specifications. Design view of page crated by me.

Namespaces:-

using System.Net.Mail;
using System.Net.Security;

Design:-

<%@ 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>
    <style type="text/css">
        .style1
        {
            width: 600px;
            float: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center" class="style1">
            <tr>
                <td>
                    To:-</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Subject:-</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Cc:-</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Body:-</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Height="115px" TextMode="MultiLine" 
                        Width="265px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Attachment:-</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
                    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
              </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code:-

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using System.Net.Security;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("mailsend@gmail.com");
        mail.To.Add(TextBox1.Text);
        mail.CC.Add(TextBox3.Text);
        mail.Subject = TextBox2.Text;
        mail.IsBodyHtml = true;
        mail.Body = TextBox4.Text;
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new System.Net.NetworkCredential("mailsend@gmail.com", "demo@123");
        smtp.EnableSsl = true;
        smtp.Send(mail);
        Label1.Text = "Email sent to " + TextBox1.Text + " Successfully !";
    }
}

Posted in Asp.net | Tagged , , , | Leave a comment