Wednesday, 30 October 2013

Make CSS3 Creatures

Something really amazing with css3 is one of here. You can see below.




So here you go for Html page code

 
Style for it
 




For reference link
http://bennettfeely.com/csscreatures/

Tuesday, 29 October 2013

Which command using Query Analyzer will give you the version of SQL server and operating system?

This one is very important question for interview.

Here you go,

SELECT SERVERPROPERTY ('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition').

Saturday, 19 October 2013

MVC demo

This example for add and search record using entity framework in MVC3 using JSON.

Entity frame work database design is given below:


http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/creating-model-classes-with-the-entity-framework-cs

HomeController Page
namespace MvcAppVehicleSystem.Controllers

{
    public class HomeController : Controller
    {
        VehicleSysHireEntities2 _db;
        public HomeController()
        {
            _db = new VehicleSysHireEntities2();
        }

        public ActionResult Index()
        {
            var query = _db.VehicleRentTypes.ToList();

            var query2 = _db.VehicleCategories.ToList();

            var model = new Models.SelectViewModel
            {
                CategoryList = query2,
                RentTypeList = query
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(VehicleMasterModel role)
        {
            VehicleMaster currentVehicle = new VehicleMaster();
            currentVehicle.Brand = role.Brand;
            currentVehicle.Category = role.Category;
            currentVehicle.Model = role.Model;
            currentVehicle.Color = role.Color;
            currentVehicle.NoOfYear = role.NoOfYear;
            currentVehicle.RentType = role.RentType;
            _db.AddToVehicleMasters(currentVehicle);
            _db.SaveChanges();

            if (currentVehicle.ID > 0)
            {
                VehicleFacility currentfacility = new VehicleFacility();
                currentfacility.VehicleMasterID = currentVehicle.ID;
                currentfacility.HasABS = role.HasABS;
                currentfacility.HasAirCon = role.HasAirCon;
                currentfacility.HasCTX = role.HasCTX;
                _db.AddToVehicleFacilities(currentfacility);
                _db.SaveChanges();
            }
            return Json(true);
        }

        public ActionResult About()
        {
            var query = _db.VehicleRentTypes.ToList();
            var query2 = _db.VehicleCategories.ToList();
            var model = new Models.SelectViewModel
            {
                CategoryList = query2,
                RentTypeList = query
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult About(VehicleMaster role)
        {
            List objList = (from m in _db.VehicleMasters select m).ToList();
            return Json(objList);
        }

        [HttpPost]
        public ActionResult AboutResult(VehicleMasterModel role)
        {
            var objList = (from m in _db.VehicleMasters
                           where m.RentType == role.RentType
                           select new VehicleMasterModel()
                           {
                               Brand = m.Brand,
                               Model = m.Model
                           }).ToList();
            return Json(objList, JsonRequestBehavior.AllowGet);
        }
    }
}

Model-> SelectViewModel
 public class SelectViewModel

    {

        public List CategoryList { get; set; }

        public List RentTypeList { get; set; }

        public VehicleFacility currentvehiclefacility { get; set; }

        public VehicleMaster currentvehiclemaster { get; set; }

    }

Model-> VehicleCategoryModel
public class VehicleCategoryModel

    {

        public int ID { get; set; }

        public string Category { get; set; }

        public List VehicleCategoryList { get; set; }

    }

Model-> VehicleFacilityModel
  public class VehicleFacilityModel

    {

        public List VehicleMasterList { get; set; }

        public int ID { get; set; }

        public bool HasAirCon { get; set; }

        public bool HasABS { get; set; }

        public bool HasCTX { get; set; }

        public int VehicleMasterID { get; set; }

    }
 
Model-> VehicleMasterModel
 public class VehicleMasterModel

    {

        public int ID { get; set; }

        public int Category { get; set; }

        public string Brand { get; set; }

        public string Model { get; set; }

        public string Color { get; set; }

        public int NoOfYear { get; set; }

        public int RentType { get; set; }

        public bool HasAirCon { get; set; }

        public bool HasABS { get; set; }

        public bool HasCTX { get; set; }

        public int VehicleMasterID { get; set; }

    }

Model-> VehicleRentTypeModel
 public class VehicleRentTypeModel

    {

        public int ID { get; set; }

        public string RentType { get; set; }

        public List  VehicleRetTypeList { get; set; }

    }

Home->Index
@{
    ViewBag.Title = "Home Page";
}

@ViewBag.Message

@model MvcAppVehicleSystem.Models.SelectViewModel
Type @Html.DropDownList("ddlType", new SelectList(Model.RentTypeList, "ID", "RentType"), "Select Type", new { required = "required" }) Category @Html.DropDownList("ddlCategory", new SelectList(Model.CategoryList, "ID", "Category"), "Select Category", new { required = "required" })
Model @Html.DropDownList("ddlModel", new[] { new SelectListItem() {Text = "Audi",Value = "Audi"}, new SelectListItem() {Text = "Maruti",Value = "Maruti" }, new SelectListItem() {Text = "Suzuki",Value = "Suzuki" } }, "Choose model", new { required = "required" }) No of year @Html.DropDownList("ddlNoofYear", new[] { new SelectListItem() {Text = "2010",Value = "2010"}, new SelectListItem() {Text = "2011",Value = "2 011" }, new SelectListItem() {Text = "2012",Value = "2012" }, new SelectListItem() {Text = "2013",Value = "2013" } }, "Choose year", new { required = "required" })
Brand Has AirCon Has ABS Has CTX
Colour

Home->About
@{
    ViewBag.Title = "Search";
}
@model MvcAppVehicleSystem.Models.SelectViewModel

Search

Type @Html.DropDownList("ddlType", new SelectList(Model.RentTypeList, "ID", "RentType"), "Select Type")
No of year @Html.DropDownList("ddlNoofYear", new[] { new SelectListItem() {Text = "2010",Value = "2010"}, new SelectListItem() {Text = "2011",Value = "2011" }, new SelectListItem() {Text = "2012",Value = "2012" }, new SelectListItem() {Text = "2013",Value = "2013" } }, "Choose year")




Thursday, 17 October 2013

For client side checkbox check all or uncheck all in girdview

 

Gridview header checkbox select and deselect all rows using client side JavaScript and server side C#

In this article, I will show you how to add checkbox in gridview header template then select and deselect all rows in gridview using clients side JavaScript and server side as well. 

Introduction


Adding checkbox in gridview header template and select/deselect all rows is a common requirement in most of the asp.net projects and frequently asked questions also. Here I will explain how to add checkbox in header template of gridview and select/deselect all rows using client side javascript and server side code.

Add Checkbox in Gridview header template


Create the asp.net project and drag the gridview control then format as likes below.

                
                
                
                
                
                    
                    
                    
                    
                        
                            
                        
                        
                        
                            
                        
                    
                
            

Using HeaderTemplate of gridview I had added checkbox and also added in Itemtemplate of same column.

Select and deselect using Client side


I’m loading the few sample employee records in gridview to select/deselect all rows. Below javascript CheckAllEmp function will do select and deselect when check and uncheck in header checkbox. Call this function in gridview headertemplate, checkbox onclick event as shown above.


Above javascript code will get gridview client id and loop the each row and get the checkbox id which is available in itemTemplate and make it select/deselect rows. This is the one of the way using client side script.

Select and deselect using Server side

Same functionality we can able to do with server side also. To do this make the changes in HeaderTemplate as like below.

                        
                            
                        
                        
                        
                            
                        
                    

Make it autopostback as true and create OnCheckedChanged event in checkbox and add the below code in chkboxSelectAll_CheckedChanged event in code behind part.
protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)GridVwHeaderChckboxSrvr.HeaderRow.FindControl("chkboxSelectAll");
            foreach (GridViewRow row in GridVwHeaderChckboxSrvr.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }
        }

Above checked changed event will get the header checkbox id and if header checkbox is checked then find all rows checkbox id and make it all select else deselect all rows

For Jquery select and deselect
 function CheckAll(obj) {
   $('#<%=grdData.ClientID %> tbody tr td input:checkbox').attr('checked', obj.checked);
}

  function childclick() {
            if ($("#<%=grdData.ClientID %> input[name$='chkuser']").length == $("#<%=grdData.ClientID %> input[name$='chkuser']:checked").length) {
                $("#<%=grdData.ClientID %> input[name$='chkheaduser']").attr('checked', true);
            } else {
                $("#<%=grdData.ClientID %> input[name$='chkheaduser']").attr('checked', false);
            }
        }

For that grid have this one
  
                
                    
                
                
                    
                
 

All grid action server side events

Here i am going to show you simple asp grid all inline editing with sorting. Like CRUD operation in MVP structure.


.cs Page code


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex; BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = GridView1.Rows[e.RowIndex];
            CurrentUserData = new Entity.UserData();
            CurrentUserData.UserId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Value);
            _presenterData.GetCurrentData();

            CurrentUserData.FirstName = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text;
            CurrentUserData.LastName = ((TextBox)(row.Cells[2].FindControl("TextBox2"))).Text;
            CurrentUserData.CompanyName = ((TextBox)(row.Cells[3].FindControl("TextBox3"))).Text;
            CurrentUserData.ContactNo = Convert.ToDecimal(((TextBox)(row.Cells[4].FindControl("TextBox4"))).Text);
            CurrentUserData.Address = ((TextBox)(row.Cells[5].FindControl("TextBox5"))).Text;
            CurrentUserData.Country = ((TextBox)(row.Cells[6].FindControl("TextBox6"))).Text;
            CurrentUserData.DOB = Convert.ToDateTime(((TextBox)(row.Cells[7].FindControl("TextBox7"))).Text);

            FileUpload fp = ((FileUpload)row.Cells[7].FindControl("fup"));
            if (fp.HasFile)
            {
                fp.SaveAs(Server.MapPath("Temp/" + fp.FileName)); CurrentUserData.ImgUrl = fp.FileName;
            }

            _presenterData.Update();
            GridView1.EditIndex = -1;
            BindData();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1; BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CurrentUserData = new Entity.UserData();
            CurrentUserData.UserId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Value);
            _presenterData.Delete(); BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                #region Old code

                _presenterData.GetAllData();

                if (lastSortExpression.Value == e.SortExpression.ToString())
                {
                    if (lastSortDirection.Value == SortDirection.Ascending.ToString())
                    {
                        e.SortDirection = SortDirection.Descending;
                    }
                    else
                    {
                        e.SortDirection = SortDirection.Ascending;
                    }
                    lastSortDirection.Value = e.SortDirection.ToString();
                    lastSortExpression.Value = e.SortExpression;
                }
                else
                {
                    lastSortExpression.Value = e.SortExpression;
                    e.SortDirection = SortDirection.Ascending;
                    lastSortDirection.Value = e.SortDirection.ToString();
                }


                switch (e.SortExpression)
                {
                    case "FirstName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.FirstName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.FirstName).ToList();
                            GridView1.DataBind();
                        }
                        break;

                    case "LastName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.LastName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.LastName).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "CompanyName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.CompanyName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.CompanyName).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "ContactNo":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.ContactNo).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.ContactNo).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "Address":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.Address).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.Address).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "Country":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.Country).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.Country).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "DOB":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.DOB).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.DOB).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    default:
                        break;
                }

                #endregion

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

       

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentUserData = new Entity.UserData();
                CurrentUserData.FirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
                CurrentUserData.LastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
                CurrentUserData.CompanyName = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;

                CurrentUserData.ContactNo = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtContact")).Text);
                CurrentUserData.Address = ((TextBox)GridView1.FooterRow.FindControl("txtAddress")).Text;
                CurrentUserData.Country = ((TextBox)GridView1.FooterRow.FindControl("txtCountry")).Text;
                CurrentUserData.DOB = Convert.ToDateTime(((TextBox)GridView1.FooterRow.FindControl("txtDOB")).Text);

                FileUpload fup = ((FileUpload)GridView1.FooterRow.FindControl("fupImg"));
                if (fup.HasFile)
                {
                    fup.SaveAs(Server.MapPath("Temp/" + fup.FileName)); CurrentUserData.ImgUrl = fup.FileName;
                }
                _presenterData.Add();

                BindData();

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

.aspx page
    Select
Name : <%# Eval("FirstName") %>   <%# Eval("LastName") %>
Company : <%# Eval("CompanyName") %>
 

Style
  .tooltip
        {
            position: relative; /* color: Black;*/
        }
        
        .tooltip span td
        {
            text-align: left;
        }
        .tooltip span
        {
            display: none;
        }
        .tooltip:hover span
        {
            display: block;
            position: absolute;
            color: Black;
            width: 200px;
            background-color: rgba(251,251,251,0.9);
            border: 1px solid #a2a2a2;
            z-index: 100;
            border-radius: 7px;
            box-shadow: 2px 1px 3px 2px #a2a2a2;
        }
        input[type="text"]
        {
            width: 100px;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 3px;
        }
        
        td
        {
            text-align: center;
        }


For custom paging in grid view
http://www.codeproject.com/Articles/16238/GridView-Custom-Paging
http://www.dotnetcurry.com/ShowArticle.aspx?ID=345 

Tuesday, 15 October 2013

Count words and reverse operation in Char Array

In interview sometimes asked logical question. One of is here.
  • Get the count of word in given char array in c#. So here you go..
char[] arr = "My name is ABC XYZ.".ToCharArray();
               
 string str = "";
 ArrayList lst = new ArrayList();
 foreach (var item in arr)
  {
  if (item.ToString() == " " || item.ToString() == ".")
   {
       lst.Add(str);
       str = "";
   }
  else
  {
   str += item.ToString();
  }

 }
 Response.Write(lst.Count);

Output :  5
  • Get the reverse order of char array
 //For reverse array
 lst.Reverse();
 var mystr = "";
 foreach (var item in lst)
 {
  mystr += item + " ";
 }
 Response.Write("Count : " + lst.Count + " Reverse : " + mystr);

Output : XYZ ABC is name My
  • Get the reverse order of char array with single string
 //Reverse string
 char[] abc = "HiralSavaria".ToCharArray();
 Array.Reverse(abc);
 Response.Write("
" + new string(abc));

Output : airavaSlariH