How to restrict the number of files and restrict size of file for a single upload using Asp.Net C#

Restrict the number of files and restrict size of file for a single upload 

Image or Files upload  multiple file for single click.Rrestrict the number of files and restrict the size of file using in asp.net c#.

                                   DEMO 

                            

                             Download
                       
                        HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Multiple image upload and Restrict file size using Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <br />
        <asp:FileUpload ID="FileUpload1" AllowMultiple="True" runat="server" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

                                                              C# CODING

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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        HttpFileCollection hfcc = Request.Files;

        if (hfcc.Count != 0)   //
        {
            // Allow Only 2 Images
            if (hfcc.Count <= 2)
            {
                if (FileUpload1.PostedFile.ContentLength < 1048576)            // Allow Size upto 1MB
                {
                    HttpFileCollection hfc = Request.Files;
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            hpf.SaveAs(Server.MapPath("Image") + "\\" + Path.GetFileName(hpf.FileName));
                          
   SqlCommand cmd = new SqlCommand("insert into Reg(imagename,imgpath) values ('" +hpf.FileName+ "','" +"Image/"+hpf.FileName  + "')", con);
                            cmd.ExecuteNonQuery();
                        }
                       
                    }
                    con.Close();
                    Response.Write("<script>alert('Data Registered')</script>");                   
                }
                else
                {
                    Response.Write("<script>alert('Maximum File Size 1MB')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Allowed only 2 Images')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Minimum One Images Upload')</script>");
        }

    }
}

                               Web.Config


<?xml version="1.0"?>


<configuration>

    <connectionStrings>
    
        <add name="dbcon" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
 
          
    <system.web>

      <httpRuntime maxRequestLength="1048576"/>

    </system.web>

</configuration>


Add New Webform -  Add FileUpload Control from Toolbox - Add AllowMultiple=True





Next - Add Namespace System.IO  and get  requested length from upload file then check by if condition







Next - Add Web.Config maxRequestLength = 1 MB







Stylish Gridview Using Bootstrap Css in Asp.Net

Stylish Gridview Using Bootstrap Css

Gridview Display Responsive using this Bootstrap Css and Javascript Using Asp.net.

HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Stylish Gridview Using Bootstrap Css in Asp.Net</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" />

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>

</head>
<body>
    <form id="form1" runat="server">
        <br />
                <div style="width: 90%; margin-right: 5%; margin-left: 5%; text-align: center">

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="username" HeaderText="Username" SortExpression="username" />
                <asp:BoundField DataField="sal" HeaderText="Salary" SortExpression="sal" />
            </Columns>

        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Reg]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Without Using Bootstrap Css shown below like this 



After Adding Bootstrap Css 




How to Set Default Page Using Web.Config in Asp.net

Set Default Page Using Web.Config


if Login.aspx form set to default form use to web.config file . add below <system.webServer> Using Asp.Net.

Web.Config


<system.webServer>
              <httpErrors errorMode="Detailed" />
              <asp scriptErrorSentToBrowser="true"/>

    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    </handlers>

<defaultDocument enabled="true">
         <files>
            <add value="Login.aspx" />
         </files>
      </defaultDocument


       </system.webServer>






How to Use Jquery DatePicker within Ajax UpdatePanel Using in Asp.Net

Use Jquery DatePicker within Ajax UpdatePanel

Jquery Date Picker use to inside of ajax updatepanel because of  Avoid postback use to pageload using  Jquery in Asp.Net.

                                DEMO

                      

                                 Download

                           HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <title>How to Use Jquery DatePicker within Ajax UpdatePanel Using in Asp.Net</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"/>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
    <script type="text/javascript" lang="javascript">
        function pageLoad() {
            $(function () {
                $("#<%=txtDate.ClientID%>").datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: "dd/mm/yy"
                });
            });
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">   
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
           Select Date <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>          
        </ContentTemplate>
    </asp:UpdatePanel></div>
    </form>
</body>
</html>



Add - New Web form - Jquery Datepicker  add inside of Ajax Updatepanel use to pageload