Sunday, 15 May 2016

Open page in new page and Print div - javascript

No comments


 //Print label

    function PrintLabel(inId) {
        var printLabelContent = "<html><head><title> &nbsp;</title></head><body><label style='font-family:\"Free 3 of 9\"' id='barcodeCode'>" + inId + "</label></br><label style='font-family:\"Arial\"' id='barcodeCodeId'>" + inId + "</label>" + '</body></html>';
        var thePopup = window.open('', "Customer Listing", "menubar=0,location=0,height=700,width=700");            
        thePopup.document.body.innerHTML = printLabelContent;
        var css = '@@page {size: auto;margin: 0; }',
        head = thePopup.document.head || thePopup.document.getElementsByTagName('head')[0],
        style = thePopup.document.createElement('style');
        style.type = 'text/css';
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
        head.appendChild(style);      
        thePopup.document.getElementById('barcodeCode').style.zoom = "10.0";      
        thePopup.print();
        return false;
    }
read more

Thursday, 12 May 2016

Execute SP in MVC with repository pattern

No comments
 var result = contactsRepository.ExecWithStoreProcedure("Search @CompanyName,@CountryName,@StateName,@CityName,@AddressType,@zip,@OrderId",
                new SqlParameter("CompanyName", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(companyName) ? string.Empty : companyName
                },
                new SqlParameter("CountryName", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(countryName) ? string.Empty : countryName
                },
                new SqlParameter("StateName", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(stateName) ? string.Empty : stateName
                },
                new SqlParameter("CityName", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(cityName) ? string.Empty : cityName
                },
                new SqlParameter("AddressType", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(hdfAddressType) ? string.Empty : hdfAddressType
                },
                new SqlParameter("zip", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(zip) ? string.Empty : zip
                },
                new SqlParameter("OrderId", System.Data.SqlDbType.VarChar)
                {
                    Value = string.IsNullOrEmpty(orderId) ? string.Empty : orderId
                }
                ).ToList();

read more

SP to search in form

No comments
CREATE PROCEDURE [dbo].[Search] (
@CompanyName VARCHAR(150) = NULL
,@CountryName VARCHAR(50) = NULL
,@StateName VARCHAR(50) = NULL
,@CityName VARCHAR(50) = NULL
,@AddressType VARCHAR(10) = NULL
,@zip VARCHAR(20) = NULL
,@OrderId VARCHAR(50) = NULL
)
AS
BEGIN
IF (LEN(@CountryName) = 0)
BEGIN
SET @CountryName = NULL
END
ELSE
BEGIN
SET @CountryName = '%' + @CountryName + '%'
END

IF (LEN(@OrderId) = 0)
BEGIN
SET @OrderId = NULL
END

IF (LEN(@StateName) = 0)
BEGIN
SET @StateName = NULL
END
ELSE
BEGIN
SET @StateName = '%' + @StateName + '%'
END

IF (LEN(@CityName) = 0)
BEGIN
SET @CityName = NULL
END
ELSE
BEGIN
SET @CityName = '%' + @CityName + '%'
END

IF (LEN(@CompanyName) = 0)
BEGIN
SET @CompanyName = NULL
END
ELSE
BEGIN
SET @CompanyName = '%' + @CompanyName + '%'
END

IF (LEN(@zip) = 0)
BEGIN
SET @zip = NULL
END

PRINT @AddressType
PRINT ISNULL(@AddressType, '@@@@AddressType is null')


SELECT Address1
,Address2
,City
,ZIP
,Phone
,Email
,AddressType
,cntc.CountryId
,CompanyName
,StateId
,OrderId
,CountryName
,ContactsId
,'Contact' AS TypeTable
FROM Contacts cntc WITH (NOLOCK)
INNER JOIN Country cntry WITH (NOLOCK) ON cntc.CountryId = cntry.CountryId

WHERE ISNULL(CompanyName, '') LIKE COALESCE(@CompanyName, CompanyName, '')
AND ISNULL(CountryName, '') LIKE COALESCE(@CountryName, CountryName, '')
AND ISNULL(StateId, '') LIKE COALESCE(@StateName, StateId, '')
AND ISNULL(City, '') LIKE COALESCE(@CityName, City, '')
AND ISNULL(ZIP, '') = COALESCE(@zip, ZIP, '')
 AND OrderId = coalesce(@OrderId ,OrderId,''')
END
read more

Thursday, 5 May 2016

Bind Dropdownlist to model in MVC

No comments
CSHTML - 

 @Html.DropDownListFor(m => m.drugid, Model.DrugList, "Select Drug", new { @class = "chosen-select" })


GenericSelectListItems class to bind data anywhere - 

    public class GenericSelectListItems
    {
        private static GenericSelectListItems _GenericSelectListItems = new GenericSelectListItems();
        UnitOfWork uom = new UnitOfWork();

        public GenericSelectListItems()
        {

        }

        public static GenericSelectListItems Instance
        {
            get
            {
                return _GenericSelectListItems;
            }
        }

        public List<SelectListItem> GetDrugs()
        {
            var drugRepository = uom.Repository<DrugRepository>();
            var drugDataList = drugRepository.GetAllDrugs();
            return drugDataList.Select(d => new SelectListItem
            {
                Value=d.drugid.ToString(),
                Text=d.drugname
            }).ToList();
        }

    }


Controller - 

 public class DrugsController : Controller
    {
        UnitOfWork uow = new UnitOfWork();
        public ActionResult IncomingDrugs()
        {
            DrugInModels drugInModels = new DrugInModels();
            BindDropDown(drugInModels);
            return View(drugInModels);
        }

        public void BindDropDown(DrugInModels drugInModels)
        {
            drugInModels.DrugList = GenericSelectListItems.Instance.GetDrugs();
        }
    }
read more

Tuesday, 3 May 2016

Enable live click in Jquery

No comments
to enable .live click in jquery add this function in document ready

  jQuery.fn.extend({
            live: function (event, callback) {
                if (this.selector) {
                    jQuery(document).on(event, this.selector, callback);
                }
            }
        });

full code ----

$(document).ready(function () {

        jQuery.fn.extend({
            live: function (event, callback) {
                if (this.selector) {
                    jQuery(document).on(event, this.selector, callback);
                }
            }
        });

        $('.sectionCredit').live('click', function () {        
            var $this = $(this);
            $('.sectionCredit').each(function () {
                if ($(this).hasClass('active')) {
                    $(this).removeClass('active');
                }
            });
            $this.addClass('active');
        })
    });
read more

Friday, 29 April 2016

using partial view in mvc

No comments
first create controller

example we have edit customer in controller so we will render partial view to edit customer info


controller - 

  public ActionResult EditAddressBook(int addressBookId)
        {
            UnitOfWork uom = new UnitOfWork();
            var addressBookRepository = uom.Repository<AddressBookRepository>();
            var addressResult = addressBookRepository.EditAddressBook(addressBookId);
            AutoMapper.Mapper.CreateMap<AddressBook, AddressBookModel>();
            AddressBookModel model = new AddressBookModel();
            model = AutoMapper.Mapper.Map<AddressBookModel>(addressResult);
            BindDropDownListAddressBook(model);
            return PartialView("_NewCustomer", model);
        }


partial view -



@using (Html.BeginForm("AddCustomer", "Shipment", FormMethod.Post, new { id = "frmAddCustomer", @class = "form-horizontal" }))
{
    <div class="box-body">


        <div class="form-group">

            <div class="col-md-4">
                <label>I.D<span class="text-red ">*</span></label>
                @Html.HiddenFor(x => x.AddressbookId, new { @id = "hdnAddressId" })
                @Html.TextBoxFor(m => m.OrderId, new { @class = "form-control  required", @data = "I.D", maxlength = 50 })
            </div>
            <div class="col-md-4">
                <label>Company Name<span class="text-red ">*</span></label>
                @Html.TextBoxFor(m => m.CompanyName, new { @class = "form-control required", @data = "Company Name", maxlength = 150 })
            </div>

            <div class="col-md-4">
                <label>Contact Name<span class="text-red">*</span></label>
                @Html.TextBoxFor(m => m.ContactName, new { @class = "form-control required", @data = "Contact Name", maxlength = 35 })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-4">
                <label>Address Line 1<span class="text-red">*</span></label>
                @Html.TextBoxFor(m => m.Address1, new { @class = "form-control  required", @data = "Address Line 1", maxlength = 35 })
            </div>
            <div class="col-md-4">

                <label>Addresss Line 2</label>
                @Html.TextBoxFor(m => m.Address2, new { @class = "form-control", maxlength = 35 })
            </div>
            <div class="col-md-4">
                <label>Country<span class="text-red">*</span></label>
                @Html.DropDownListFor(m => m.CountryId, Model.CountryList, "Select Country", new { @class = "form-control  required", @data = "Country" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-4">
                <label>Postal/Zipcode<span class="text-red">*</span></label>
                @Html.TextBoxFor(m => m.ZIP, new { @class = "form-control  required", @data = "Zipcode", maxlength = 10, @id = "AddressZip" })
            </div>
            <div class="col-md-4">
                <label>City<span class="text-red">*</span></label>
                @Html.TextBoxFor(m => m.City, new { @class = "form-control required", @data = "City", maxlength = 50 })
            </div>
            <div class="col-md-4">
                <label class="col-sm-2">Province/State<span class="text-red ">*</span></label>
                @Html.DropDownListFor(m => m.StateId, Model.StateList, "Select State", new { @class = "form-control  required", @data = "State" })
                @*@Html.TextBoxFor(m => m.StateId, new { @class = "form-control required", maxlength = 50, @data = "State" })*@
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-4">
                <label class="col-sm-2">Phone<span class="text-red">*</span></label>
                @Html.TextBoxFor(m => m.Phone, new { @class = "form-control required", @data = "Phone" })
            </div>
            <div class="col-md-4">
                <label class="col-sm-2">Email</label>
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="clearfix" style="height:20px">&nbsp;</div>  
        <button type="button" id="btnAddContacts" class="btn btn-primary">Submit</button>&nbsp;&nbsp;
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <div class="clearfix" style="height:70px">&nbsp;</div>
    </div>
}



render partial from jquery method - 

 function EditAddressBook(addressBookId) {
                $.ajax({
                    method: "GET",
                    url: '@Url.Action("EditAddressBook", "Shipment")',
                    data: { 'addressBookId': addressBookId },
                    success: function (result) {
                        if (result != undefined && result != null) {
                            $('#modalAddCustomer .modal-body').html('');
                            $('#modalAddCustomer #modalHead').html('Update Cusotomer');
                            $('#modalAddCustomer .modal-body').html(result);
                            $('#modalAddCustomer').modal('toggle');
                            $('#modalAddCustomer').modal('show');
                        }
                        else {

                        }
                    }
                });
            }


modal popup in which result i.e partial view will be - 


<div class="modal fade" id="modalAddCustomer" role="dialog" data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog" style="width: 65%">
            <div class="modal-content">
                <div class="box-header with-border">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title" id="modalHead">Add Customer</h4>
                </div>
                <div class="modal-body">
                </div>
            </div>
        </div>
    </div>

read more

Formating different json and strings with quotation

No comments
valid JSON formate in c# -

with dynamic value - 

 string json = "[{\"message\":\"" + message + "\",\"phoneNumber\":\"" + number + "\"}]";

with static value -

string json = "[{\"message\": \"this is test \", \"phoneNumber\": \"123456\"}]";

read more