Wednesday 26 July 2017

Catch all unhandled exceptions on Controll level

watch this example on YouTube

add this method at the end of your controller

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            Exception e = filterContext.Exception;
            //loging(e.Message.ToString());
            filterContext.ExceptionHandled = true;
            filterContext.Result = new ViewResult()
            {
                ViewName = "Error"
            };
        }

to create dummy exception
       throw new System.ArgumentException("something is wrong");

Tuesday 25 July 2017

MVC - Fix Error - CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type


watch solution on YouTUbe



CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

to fix it replace 

  @Html.Label(model => model.LastName)
with
 @Html.Label(Model.First().LastName)

Monday 17 July 2017

MVC - jQuery - dynamically change width of textbox or EditorFor

Watch this example on YouTube
<iframe width="560" height="315" src="https://www.youtube.com/embed/FjX4YbTWMzE?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>

add ID to EditorFor
    @Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = "form-control", @id="myText" } })

add this script
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(document).ready(function () {
            var textVal = $('#myText').val();
            $('#myText').css('width', ((textVal.length + 2) * 10).toString() + 'px');
        });     
    </script>
}

MVC - Create DropDownList with prepopulated data - for instance with Yes/No


Watch this example on YouTube


@Html.DropDownList("SomeName", new[]
{
    new SelectListItem{Text="YES", Value="1" },
    new SelectListItem {Text="NO", Value="2" }
}, "", new { @id="IsActive"})

MVC - jQuery - Get value of selected radio button

Watch this example on YouTube


Html:

USD: @Html.RadioButton("Currency", "USD")
CAD: @Html.RadioButton("Currency", "CAD")
<input type="button" onclick="CheckSelected()" value="click me" />

jQuery

@section Scripts {
    <script type="text/javascript">
        function CheckSelected() {
            var selected = $("input[name='Currency']:checked").val();
            alert(selected);
        }

        $(document).ready(function () {
      
        });
    </script>
}

MSSQL - Fix Error - Conversion failed when converting the varchar value 'Select * from Cities where ID = ' to data type int.

 Watch this on YouTube



Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the varchar value 'Select * from Cities where ID = ' to data type int.


to fix it replace

Declare @ID int = 1

Declare @SQL varchar(max)

Set @SQL = 'Select * from Cities where ID = ' +  @ID

exec (@SQL)

with

Declare @ID int = 1

Declare @SQL varchar(max)

Set @SQL = 'Select * from Cities where ID = ' + CAST( @ID AS VARCHAR)

exec (@SQL)