Sunday, 29 May 2016

C# Program to Calculate Size of File using LINQ

using System;
using System.Linq;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
        string[] MyFiles= Directory.GetFiles("c:\\xxx\\");
        var avg = MyFiles.Select(file =>new FileInfo(file).Length).Average();
        avg = Math.Round(avg / 10, 1);
        Console.WriteLine("The Average file size is {0} MB",avg);
        Console.ReadLine();
    }
}
---------------------------------------
OUTPUT:
The Average file size is 11.3 MB
-------------------------------------------

Sunday, 1 May 2016

How To Display Data in GridView using MVC

Controller:-
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using MVCApp.Models;
using System.IO;
using System.Text;

namespace MVCApp.Controllers
{
    public class EmployeeDetailController : Controller
    {
        // GET: EmployeeDetail
        [HttpGet]
        public ActionResult EmployeeDetail()
        {
            string strcon = System.Configuration.ConfigurationManager.AppSettings["conStr"].ToString();
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcon.Open();
           string qierr = "select name,addresss,mobile from EmpData";
           SqlCommand sqlcmd = new SqlCommand(qierr, sqlcon);
            SqlDataAdapter sqld = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqld.Fill(ds);
            sqlcon.Close();
            List<Employee> lst = new List<Employee>();
            foreach(DataRow dr in ds.Tables[0].Rows)
            {

                lst.Add(new Employee { name = dr["name"].ToString(), addresss = dr["addresss"].ToString(), mobile = dr["mobile"].ToString() });
            }


            return View(lst);
        }
        [HttpPost]
        public ActionResult EmployeeDetail(FormCollection frm)
        {
            string strcon = System.Configuration.ConfigurationManager.AppSettings["conStr"].ToString();
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand("insertEmployee", sqlcon);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.AddWithValue("@name", frm[0]);
            sqlcmd.Parameters.AddWithValue("@addresss", frm[1]);
            sqlcmd.Parameters.AddWithValue("@mobile", frm[2]);
            int insertStatus=sqlcmd.ExecuteNonQuery();
            if (insertStatus > 0)
            {
                ViewData["status"] = 1;
            }
            else
            {

                ViewData["status"] = 0;
            }
            sqlcon.Close();
            return View();
        }
    }
}
------------------------------------------------
Store Procedure:

Creat procedure insertEmployee --'sunil','pg','8554777'
(
@name varchar(50)='',
@addresss varchar(50)='',
@mobile varchar(50)=''
)
As begin
insert into EmpData(name, addresss, mobile, isActive)
values(@name,@addresss,@mobile,1)

End
----------------------------------
VIEW:
------------------
@model IEnumerable<MVCApp.Models.Employee>
@{
    ViewBag.Title = "EmployeeDetail";
    WebGrid grid = new WebGrid(Model);
}

@if (Convert.ToInt32(ViewData["status"])==1)
{
    <script type="text/javascript">
        alert('Data Inserted');
    </script>
}
else
{

    <script type="text/javascript">
        alert('Data not Inserted');
    </script>
}

@using (Html.BeginForm("EmployeeDetail","EmployeeDetail",FormMethod.Post,new{id="frmEmpDetails"}))
{
<fieldset>
    <legend>Employee Entry Form</legend>
    <table>
        <tr>
            <td>Name:</td>
            <td>@Html.TextBox("txtName", "", new { id = "txtName" })</td>
        </tr>
        <tr>
            <td>Address:</td>
            <td>@Html.TextBox("txtaddress", "", new { id = "txtaddress" })</td>
        </tr>
        <tr>
            <td>Mobile:</td>
            <td>@Html.TextBox("txtmobiles", "", new { id = "txtmobiles" })</td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr><td colspan="2" align="center"><input type="submit" name="Submit" id="btnSubmit" value="Submit"/>&nbsp;&nbsp;<input type="reset" name="Reset" id="btnReset" value="Reset" /></td></tr>
    </table>
    <table>
        <tr>
            <td>
             
                <h2>Employee Detail</h2>
                @grid.GetHtml(columns: grid.Columns(
grid.Column("name", header: "Name"),
       grid.Column("addresss", header: "Adress"),
        grid.Column("mobile", header: "Mobile")
                 ));

        </td>
        </tr>
    </table>
</fieldset>  

-----------------------
OUTPUT: