Wednesday, 30 August 2017

mvc checkboxlist example


Using the ckeckboxlist to display the records. It is having the items in the form of list box and having the checkbox in list.

It will be appear as like below. Here  I have implemented the checkbox list which will dynamically binding based on dropdown. mvc checkboxlist example
Its like cascaded checkboxlist, mvc checkboxlist multiselectlist.                       

1.       Take the reference of checkboxlist from nugget package as like below.  Search as MVCCheckboxlist as  given below and install this nugget package.



2.       Create the classes which will help to bind it to the dropdown  and  checkboxlist.

  public class EmployeeDetailModel
    {
        public int EmpID { get; set; }
        public int EmpName { get; set; }
        public string Dist { get; set; }
        public List<District> DistList { get; set; }
        public string City { get; set; }
        public List<City> CityList { get; set; }
        public List<City> SelectedCityList { get; set; }


    }

    public class District
    {
        public int DistrictID { get; set; }
        public string DistrictName { get; set; }
    }

    public class City
    {
        public int CityID { get; set; }
        public int DistrictID { get; set; }
        public string CityName { get; set; }
    }

3.       Create the collection of district and City where District will bind to the dropdown and   City will bind to the list.

List<District> disct;
        List<City> lstCity;
        public HomeController()
        {
            disct = new List<District> {
                new District { DistrictID = 1, DistrictName = "Bilaspur" },
                new District { DistrictID = 2, DistrictName = "Korba" },
                new District { DistrictID = 3, DistrictName = "Durg" }
            };

            lstCity =  new List<City> {
                new City { CityID = 1, DistrictID=1, CityName = "Masturi" },
                new City { CityID = 2,  DistrictID=1, CityName = "Masturi1" },
                new City { CityID = 3,  DistrictID=1,CityName = "Masturi2" },
                new City { CityID = 4,  DistrictID=2,CityName = "Masturi3" },
                new City { CityID = 5,  DistrictID=2,CityName = "Masturi4" },
                new City { CityID = 6,  DistrictID=2, CityName= "Masturi5" },
                new City { CityID = 7,  DistrictID=2,CityName = "Masturi6" },
                new City { CityID = 8,  DistrictID=2,CityName = "Masturi7" },
                new City { CityID = 9,  DistrictID=4,CityName = "Masturi8" },
                new City { CityID = 10,  DistrictID=4,CityName = "Masturi9" },
                new City { CityID = 11,  DistrictID=4,CityName = "Masturi10" },
                new City { CityID = 12,  DistrictID=4,CityName = "Masturi11" },
                new City { CityID = 13,  DistrictID=4,CityName = "Masturi12" },

            };
        }


4.       User Interface for application which consist of textboxfor, Dropdown and  Checkboxlistfor.( mvc checkboxlist multiselectlist.)

@using MvcCheckBoxList
@model WebApplication1.Controllers.EmployeeDetailModel
@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Employee Detail</p>

<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <label for="exampleInputPassword1">Emp Name</label>
            @Html.TextBoxFor(m => m.EmpName);
        </div>
    </div>
    <div class="col-md-4">
        <label for="exampleInputPassword1">Dist</label>
        @Html.DropDownListFor(m => m.Dist, new SelectList(Model.DistList, "DistrictID", "DistrictName"));
    </div>
    <div class="col-md-4">
        <label for="exampleInputPassword1">City</label>
        <div style="height: 100px; overflow-y:auto;border: 1px solid lightgrey;padding:4px 4px 4px 2px;" id="chkCityList">
            @Html.CheckBoxListFor(m => m.SelectedCityList, m => m.CityList, e => e.CityID, e => e.CityName, m => m.SelectedCityList, MvcCheckBoxList.Model.Position.Vertical);
        </div>

    </div>
</div>

5.       Defining the controller for  View and pass the model to bind the dropdown and checkboxlistfor.

public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            EmployeeDetailModel oEmployee = new Controllers.EmployeeDetailModel();


            oEmployee.DistList = disct;

            oEmployee.CityList = lstCity;

            return View(oEmployee);
        }

6.       Binding the checkboxlistfor based  selection of dropdown  list it will behave cascaded dropdown with list.

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $('#Dist').change(function () {
            debugger;
            var city = "";
            $.ajax({
                type: "post",
                url: "/Home/GetCity",
                data: { DistID: $('#Dist').val() },
                datatype: "json",
                traditional: true,
                success: function (data) {
                    for (var i = 0; i < data.length; i++)
                    {
                        city = city + "<input id='SelectedCityList" + i + "' name='SelectedCityList' type='checkbox' value='" + data[i].CityID + "'><label for='SelectedCityList0'>" + data[i].CityName + " </label><br />"
                    }
                    $('#chkCityList').html(city);
                }
            });
        });
    });
</script>



Now while selecting Bilaspur you can see it will bind only three records.

Sunday, 6 August 2017

@html.grid mvc 5


Step by step process to bind MVC.Grid in MVc application
Grid will be display like below

Thursday, 3 August 2017

Generate Property and Methods Tool

You can automatically generate the property and method and insert query which help you to build the backed code faster.


Create an window application like below ;


Use below code,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CreateProperty
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=REETPC;Initial Catalog=propertyHelper;Persist Security Info=True;User ID=sa;Password=password1");
        SqlDataReader rd;
        SqlCommand cm = new SqlCommand();
        string fieldName, dataType;
        public Form1()
        {
            InitializeComponent();
        }

        #region Private method for helping to create data
        /// <summary>
        /// Change the string type as the first latter as small lattre
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string getFirstSmall(string str)
        {
            string getStr;
            getStr = str.Substring(0, 1).ToLower();
            getStr = getStr + str.Substring(1, str.Length - 1);
            return getStr;
        }
        /// <summary>
        /// Open the connection
        /// </summary>
        ///
        private void GetConnection()
        {
            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
                rd.Close();
            }

            cn.Open();
            cm.Connection = cn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = "select * from properties";

            rd = cm.ExecuteReader();
        }

        /// <summary>
        /// close the connection
        /// </summary>
        private void CloseConnection()
        {
            cn.Close();
            rd.Close();
        }

        # endregion
        private void btnProperty_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;

            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
                rd.Close();
            }

            cn.Open();
            cm.Connection = cn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = "select * from properties";

            rd = cm.ExecuteReader();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    strProp += "\r\n";

                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        // strProp += "public string " + fieldName + "\n {  get { return _" + getFirstSmall(fieldName) + ";} \n set {  _" + getFirstSmall(fieldName) + " = value; }  }\n";
                        strProp += "public string " + fieldName + "{ get; set; };\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "public string " + fieldName + "{ get; set; };\n";
                        //strProp += "public string " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; } \n set {  _" + getFirstSmall(fieldName) + " = value; }  }\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "public int " + fieldName + "{ get; set; };\n";
                        //  strProp += "public int " + fieldName + " \n{  get { return _" + getFirstSmall(fieldName) + ";} \n set {  _" + getFirstSmall(fieldName) + " = value; }  \n}\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "public bool " + fieldName + "{ get; set; };\n";
                        //  strProp += "public bool " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; } \n set {  _" + getFirstSmall(fieldName) + " = value; }  \n}\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "public DateTime " + fieldName + "{ get; set; };\n";
                        // strProp += "public DateTime " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; }\n set {  _" + getFirstSmall(fieldName) + " = value; \n } \n }";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "public decimal " + fieldName + "{ get; set; };\n";
                        // strProp += "public decimal " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; }\n set {  _" + getFirstSmall(fieldName) + " = value; \n } \n }";
                    }

                }
            }
            catch { }

            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnAttib_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += "\n string _" + getFirstSmall(fieldName) + " = string.Empty;";

                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "\n string _" + getFirstSmall(fieldName) + " = string.Empty;";

                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "\n int _" + getFirstSmall(fieldName) + " = 0;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "\n bool _" + getFirstSmall(fieldName) + " = false;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "\n DateTime _" + getFirstSmall(fieldName) + " = DateTime.Now;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "\n decimal _" + getFirstSmall(fieldName) + " = 0.0M;";
                    }

                }
            }
            catch { }
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnConstruct_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            SqlDataReader clonerd = null;
            strProp = "public ClassName(";
            GetConnection();
            clonerd = rd;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += ",string " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += ",int " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += ",bool " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += ",DateTime " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += ",decimal " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += ",string " + fieldName;
                    }

                }
            }
            catch { }
            strProp += ")";
            strProp += getInnerBodyConstructor();
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnReader_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            strProp = "private void _loadInternalData(SqlDataReader oDataReader,bool bAlreadyPresent) \n {";
            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsString(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsString(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsInt32(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsBool(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsDateTime(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsDecimal(oDataReader[\"" + fieldName + "\"]);";
                    }

                }
            }
            catch { }
            strProp += "\n }";
            CloseConnection();
            txtGetValues.Text = strProp;
        }
        /// <summary>
        /// Call the inner body of constructor
        /// </summary>
        /// <param name="rd"></param>
        /// <returns></returns>
        private string getInnerBodyConstructor()
        {
            CloseConnection();
            GetConnection();

            string strProp = string.Empty;
            strProp += "{";
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    strProp += "_" + getFirstSmall(fieldName) + "=" + fieldName + ";\n";
                }
            }
            catch { }
            return strProp;
        }
        private void BTNdal_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            strProp += "internal static  Int16 MethodName(ClassName  objname)";
            strProp += "\n{";
            strProp += " \n Int16 ret = 0;\n DataSet dts = new DataSet(); \n try \n{";
            strProp += "\n SpParamCollection  spParams = new SpParamCollection();";

            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    strProp += "\n spParams.Add(new SpParam(\"@" + fieldName + "\", objname." + fieldName + "));";
                }
            }
            catch { }
            strProp += "\n ret = ExecProcNonQuery(\" Spname \", spParams);";
            strProp += "\n }";
            strProp += "\n catch (Exception ex)\n { \n  ex.Message.ToString(); \n } \n return ret; \n }";
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnBAL_Click(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            SqlDataReader clonerd = null;
            GetConnection();
            clonerd = rd;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    strProp += "@" + fieldName + " " + dataType + ",\n";


                }
            }
            catch { }
            strProp += "BEGIN \n\nINSERT INTO TableName \n( ";

            strProp += GetFiledOfTable();
            strProp += ")\n Values \n (";

            strProp += GetValuesOfTable();
            strProp += ") \n\nEND";

            txtGetValues.Text = strProp;
        }
        #region To craete insert storeprocedure
        private string GetFiledOfTable()
        {
            CloseConnection();
            GetConnection();

            string strProp = string.Empty;
            while (rd.Read())
            {
                fieldName = rd["fieldName"].ToString();
                dataType = rd["dataType"].ToString();
                strProp += fieldName + ",\n";
            }
            return strProp;
        }
        private string GetValuesOfTable()
        {
            CloseConnection();
            GetConnection();
            string strProp = string.Empty;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    strProp += "@" + fieldName + ",\n";
                }
            }
            catch { }
            return strProp;
        }
        #endregion
    }
}

Database coding.

Need to create table

USE [propertyHelper]
GO

/****** Object:  Table [dbo].[properties]    Script Date: 08/03/2017 14:01:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[properties](
      [fieldName] [varchar](100) NULL,
      [dataType] [varchar](100) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO





Others

You will like to read it