Advertisements

Archive

Author Archive

Telerik ORM : Row not found: GenericOID@ OptimisticVerificationException

January 12, 2018 Leave a comment

This kind of error occurred When DataAccess tries to update the row in the database which has the original values for the fields that have changed.

The reason the update fails is that the artificial columns added at runtime are nullable but the CLR property which these columns are mapped to are all non-nullable. Ex – bit column is mapped to Boolean CLR property.

The generated UPDATE statement tries to find a row with the CLR default value i.e 0 but the value in the column is NULL.

I have updated the method that maps the artificial properties as follows –

var propType = Type.GetType(property.PropertyType);
if(propType.IsValueType)
{
propType = GetNullableType(propType);
}
var primitivePropertyConfiguration = myConfig.HasArtificialPrimitiveProperty(property.PropertyName,propType).HasFieldName(property.PropertyName);

Here I check if the Type of the field is a value type and if it is, then I create a nullable CLR type instead of the non-nullable type.

The helper method used is as below

public static Type GetNullableType(Type type)
{
if (type.IsValueType)
{
Type nullable = typeof(Nullable);
return nullable.MakeGenericType(new[] { type });
}
return type;
}

Hope this help !

Advertisements
Categories: ASP.NET

EXT.NET : Getting selected RadioGroup value through inputValue

January 11, 2018 Leave a comment

How to get selected value from RadioGroup in EXT.NET

Consider following sample code

On the server side I would like to be able to do something similar to this:

var value = typeRadioGroup.CheckedItems[0].InputValue;

to get the selected value directly and proceed with the code instead of calling e.g.

if (dailyRadio.Checked)
// do something
else if (weeklyRadioChecked)
// something else

Hope This help !

Categories: Ext.NEt, Tips & Tricks Tags: ,

Data Annotations – ForeignKey Attribute in EF 6 & EF Core

January 7, 2018 Leave a comment

The ForeignKey attribute is used to configure a foreign key in the relationship between two entities in EF 6 and EF Core. It overrides the default conventions. As per the default convention, EF makes a property as foreign key property when its name matches with the primary key property of a related entity.

ForeignKey Signature: [ForeignKey(name string)]

  • name: Name of the associated navigation property or the name of the associated foreign key(s).

Consider the following example of one-to-many relationship among entities.

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

The above example depicts a one-to-many relationship between Student and Standard entities. To represent this relationship, the Student class includes a property StandardId with reference property Standard and Standard entity class includes collection navigation property Students. A property name StandardId in Student entity matches with the primary key property of Standard entity, so StandardIdin Student entity will automatically become a foreign key property and corresponding column in the db table will also be a foreign key column as shown below.

Entity Framework code-first example

The [ForeignKey] attribute overrides the default convention for a foreign key It allows us to specify the foreign key property in the dependent entity whose name does not match with the primary key property of the principal entity.

The [ForeignKey(name)] attribute can be applied in three ways:

  1. [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity
  2. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity
  3. [ForeignKey(ForeignKeyPropertyName)] on the navigation property in the principal entity

[ForeignKey] on the foreign key property in the dependent entity:

The [ForeignKey] on the foreign key property in the dependent entity and the related navigation property name can be specified as a parameter as shown below.

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey("Standard")]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the StandardRefId and specified the name of the navigation property Standard. This will create the foreign key column named StandardRefId in the Students table, preventing the generation of a StandardId column in the database.

Entity Framework code-first example

[ForeignKey] on the navigation property in the dependent entity:

The [ForeignKey] attribute can be applied to the navigation property and the related foreign key property name can be specified as shown below.

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    
    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the Standard navigation property and specified the name of the foreign key property StandardRefId. This will create the foreign key column named StandardRefId in the Students table, preventing the generation of a StandardId column in the database.

[ForeignKey] on the navigation property in the principal entity:

The [ForeignKey] attribute can be applied to the navigation property in the principal entity and the related foreign key property name can be specified in the dependent entity as shown below.

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    [ForeignKey("StandardRefId")]
    public ICollection<Student> Students { get; set; }
}

In the above example, the [ForeignKey] attribute is applied on the Students navigation property in the principal entity Standard. This will create a foreign key column StandardRefId in the Students table in the database.

Hope this help !

Error: WebForms UnobtrusiveValidationMode Requires a ScriptResourceMapping For jQuery in .Net

December 18, 2017 Leave a comment

Getting below error after using RequiredFieldValidator for textbox

Error: WebForms UnobtrusiveValidationMode Requires a ScriptResourceMapping For jQuery in .Net

In this post I am trying to explain solution for the above error.

Sample Code :

<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”reqName” runat=”server” Display=”Dynamic” ControlToValidate=”txtName”  Text=”*” Style=”color: red”></asp:RequiredFieldValidator>
<asp:Button ID=”btnSubmit” Text=”Submit” runat=”server” OnClick=”btnSubmit_Click” />

Solution

Now add key for Unobtrusive under web.config file <appSettings> tag as following code.

 

<configuration>
<system.web>
<compilation debug=”true” targetFramework=”4.5″ />
<httpRuntime targetFramework=”4.5″ />
</system.web>
<appSettings>
    <add key=”ValidationSettings:UnobtrusiveValidationMode” value=”None” />
</appSettings>
</configuration>

 

 

Entity Framework : Could not load type System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider

December 8, 2017 1 comment

When you have separate class library projects for Data access layer which contains your entity model and when you are giving reference of this class library to your web application on build you may get following error

Could not load type System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider

You can solve this error by following line assembly reference in your website project webconfig file

<system.web>
    <compilation debug="true" targetFramework="4.5">
        <assemblies>
            <add
                assembly="System.Data.Entity.Design, Version=4.0.0.0,Culture=neutral,
                PublicKeyToken=B77A5C561934E089" />
        </assemblies>
    </compilation> 
    <httpRuntime targetFramework="4.5" />
</system.web>

 

Microsoft.VisualStudio.Editor.Implementation.EditorPackage’ package did not load correctly: Visual Studio 2012

November 26, 2017 Leave a comment

The Problem

When starting Visual Studio …

  • … by loading a solution file, no windows display and the Solution Explorer remains empty.
  • … you get a bunch of error messages thrown in your face.
  • … you can only add text in the editor, not remove it.
  • … some other strange, unexplainable thing prevents you from using Visual Studio as you normally would.

TerminalServerIssue

 

The Solution

If you are having one of the issues above and neither restarting Visual Studio or rebooting your machine helps, then there is a large change your Component Model Cache is corrupt and you need to delete that folder using following steps

So, here is how you fix it:

  1. Close Visual Studio (if you haven’t done so already out of despair)
  2. Open a file explorer window and navigate to your AppData folder. You can get there by:
    1. In the search bar, type %APPDATA%/.. and press enter
    2. Alternatively, navigate to C:\Users\\AppData
  3. Go into Local\Microsoft\VisualStudio\<version> where <version> is 12.0 for Visual Studio 2013.
  4. Delete the ComponentModelCache folder.

Hope it helps !

 

Kendo grid error : kendo.all.min.js:12 Uncaught TypeError: e.slice is not a function

November 13, 2017 Leave a comment

Getting Uncaught TypeError: e.slice is not a function error message while binding kendo grid with data using controller action method

Following is the sample code for View and Controller action

View :

$(function () {
$(“#grid”).kendoGrid({
height: 400,
dataSource: {
serverPaging: true,
serverFiltering: true,
serverSorting: true,
pageSize: 10,
transport: {
read: “Employee/Read”,
contentType: “application/json”,
type: “POST”
},
},
schema: {
data: “Data”,
total: “Total”,
},

columns: [
{ field: “Salary”, format: “{0:c}”, width: “150px” },
{ field: “EmployeeName”, width: “150px” },
{ field: “SalaryColor”, width: “100px” },
{ command: “destroy”, title: “Delete”, width: “110px” }
],

editable: true, // enable editing
pageable: true,
sortable: true,
filterable: true,
toolbar: [“create”, “save”, “cancel”], // specify toolbar commands
parameterMap: function (options) {
return kendo.stringify(options);
}
});
});

Controller Read Action :

public ActionResult Read(int take, int skip, IEnumerable<Sort> sort, Kendo.DynamicLinq.Filter filter)
{
SalesERPDAL salesDal = new SalesERPDAL();

var result = salesDal.Employees.OrderBy(p => p.FirstName)
.Select(p => new EmployeeViewModel
{
EmployeeName = p.FirstName,
Salary = “5000”,
SalaryColor = “yellow”
}).ToDataSourceResult(take, skip, sort, filter);

return Json(result,JsonRequestBehavior.AllowGet);
}

Problem is in above code we are passing whole result instead of only Data part of result should be passed.

return Json(result.Data,JsonRequestBehavior.AllowGet);

Hope this help !

Categories: ASP.NET Tags: , , ,