Tuesday 21 November 2017

MSSQL - How to create a table from select query results in SQL Server

Watch this example on YouTube



SELECT * INTO NewTable FROM ExisitngTable

MSSQL - How to add new integer column to the table with default value

Watch this example on YouTube





ALTER TABLE table_name
ADD IntegerColumn int NOT NULL DEFAULT(123)

Saturday 18 November 2017

MSSQL - Fix Error - Conversion failed when converting the varchar value ' ID is set to: ' to data type int.

Error
Conversion failed when converting the varchar value ' ID is set to: ' to data type int.

Watch solution on YouTube

to fix it replace
Declare @id int = 10
print ' ID is set to: ' + @id
with
Declare @id int = 10
print ' ID is set to: ' + CAST(@id As Varchar)

MSSQL - Microsoft SQL Server Management Studio - How to increase font size

Watch on YouTube

Goto Tools->Options->Environment->Fonts and Colors

MSSQL - Fix Error - Incorrect syntax near '='

Watch this example on YouTube


To fix it replace
Declare @test int = 0
if(@test == 0)
begin
    print 'Zero'
end

with
Declare @test int = 0
if(@test = 0)
begin
    print 'Zero'
end

Saturday 11 November 2017

Saturday 4 November 2017

MVC - fix error - Unable to cast object of type 'System.Int32' to type 'System.String'."

Watch on YouTube


How to set max length of integer

 1. Metadata file


namespace MVCValidationTest.Models
{
    [MetadataType(typeof(SelectTestValidation_Metadata))]
    public partial class SelectTestValidation_Result
    {
        //[StringLength(3, ErrorMessage = "{0} cannot be longer than 3 chars")]
        [Range(1,999)]
        public int test { get; set; }

2. View

  @Html.TextBoxFor(x => x.test, new { maxlength = 3 })

MSSQL - How to fix error - Incorrect syntax near '='.

Watch this example on YouTube


 
To fix it replcae

Declare @temp int = 2;
if @temp == 2
Begin
  print ('found')
End
Else
Begin
  print ('not found')
End

with

Declare @temp int = 2;
if @temp = 2
Begin
  print ('found')
End
Else
Begin
  print ('not found')
End

Thursday 2 November 2017

MVC - How to add MaxLength to TextBoxFor or other controlls using data annotations

watch this example on YouTube


1. Metadata

        [StringLength(3, ErrorMessage ="{0} cannot be longer than 3 chars")]
        public string Name { get; set; }

2. Javascript

$(document).ready(function () {
    $('input[data-val-length-max]').each(
        function (index) {
            $(this).attr('maxlength', $(this).attr('data-val-length-max'));

        });
});

3. Layout

    @Scripts.Render("~/Scripts/MaxLength.js")

MVC - How to display proper name in Validation error message

Watch this example on YouTube



Replace
        [Required(ErrorMessage ="this field is mandatory")]
        public string Name { get; set; }

with
        [Required(ErrorMessage ="{0} field is mandatory")]
        public string Name { get; set; }

or even with
        [Display(Name="Modified Name")]
        [Required(ErrorMessage ="{0} field is mandatory")]
        public string Name { get; set; }

Wednesday 1 November 2017

MVC - Display only 'header' message in @HTml.ValidationSummary

Watch this example on YouTube

to do so replace
    @Html.ValidationSummary("Fix all errors")
with
    @Html.ValidationSummary(true, "Fix all errors")

MVC - Prevent @Html.ValidationSummary from displaying message on loading

Watch this fix on YouTube

 

to fix it add the following to css file
.validation-summary-valid{
    display:none;
}

MVC - How to modify @Html.ValidationSummary so it is not displaying header message

Watch this example on YouTube

To do so replace
   @Html.ValidationSummary("Fix all the errors")
With
   @Html.ValidationSummary("")