Tuesday, August 11, 2015

String or binary data would be truncated

Error: String or binary data would be truncated

When you see the above error, this is related to SQL Server column data size specific error.

You might specified length of the string is less but you are passing more than what you specified.


Just you have to increase the size of the column for which ever column you are getting the error.

Check the table structure for the table which you are trying to insert. The data which you are trying to insert is more then the length which you specified for the same column. I think you'll find that the length of one or more fields is NOT big enough to hold the data you are trying to insert. For example, if the country name field is a varchar(6) field, and you try to put more than 6 characters in to it, you will get this error.

Tuesday, July 7, 2015

c# - linq lambda expression and '&&' operator in where clause

Are you trying to apply “AND” operator in lambda expression.

Then below example will help you. If you are trying apply “AND” operator with where clause then you need to use the below syntax and that to be in where clause

public class ComplexClass
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string CellNumber { get; set; }

        public string Email { get; set; }
    }

List<ComplexClass> lstcomplexClass = new List<ComplexClass>();

resultItems = lstcomplexClass.Where(x => (x.Name == “Test”) && (x.Id== 1)).ToList();



And operator “&&” has to be applied in the Where clause. By applying the where you will get the item say “x”(in our example) from the list. Then system will apply the condition on the item to get the items from the list.

Monday, June 29, 2015

Use Anchor Tag for calling an Action in MVC

 If you are trying to call a "Action" from the Anchor tag in MVC below is the way to do it.


<a href="@Url.Action("ActionName", "ControllerName", new { area = "AreaName" })">LinkText</a>


Example:

<a href="@Url.Action("Registration", "Account", new { area = "ManageAccount" })">Regsitration</a>


You can also do the same in below way as well.
@Html.ActionLink("Registration", "Registration", "Account", new { area = "ManageAccount" }, null)



Then, is there any difference between the Url.Action and @Html.ActionLink?


Yes, there is a difference. Html.ActionLink generates an <a href=".."></a> tag

But, Url.Action returns only an url.


For example:

@Html.ActionLink("LinkText", "action", "controller", new { id = "123" }, null)

generates:


<a href="/ controller/action /123"> LinkText </a>


Where as Url.Action("action", "controller", new { id = "123" }) generates:

/controller/action/123