Pages

Wednesday 28 August 2013

MVC WebGrid - Sorting is not working with Enum datatype columns...

In MVC Web Grid,
If we bind Web Grid from server side for efficient paging and sorting, we may face this problem.
If one or more columns has Enum as Datatype then, sorting will not work properly for that columns.
In Sorting url, sort=ASC always. So sorting works only in one direction.

To resolve this,

we have to define columnNames property in grid.Bind() method.
For e.g.,

 var grid = new WebGrid();
 grid.Bind(data, new[] { "Id", "Name", "Note", "Date",  "EntityType" }, autoSortAndPage: false, rowCount: 10);




here,
EntityType is an Enum datatype.

Tuesday 27 August 2013

NHibernate Exception while updating records: could not delete collection [CHILD_TABLE_NAME]

While updating records using NHibernate, if we get the following exception :

could not delete collection: [CHILD_TABLE_NAME]

it is because of many to one relationship between two tables.
To resolve this exception,
we just need to add inverse="true" in mapping file of parent table. for e.g.,


 <bag name="ChildTables" generic="true" inverse = "true">
      <key>
        <column name="ParentTableId" not-null="true" />
      </key>
      <one-to-many class="ChildTable" />
  </bag>

Wednesday 26 September 2012

DB operations using LINQ

Get data from table tblEmpDetail using edmx object TestMVCAppEntities2:


TestMVCAppEntities2 _db = new TestMVCAppEntities2("metadata=res://*/TestMVCAppEntity.csdl|res://*/TestMVCAppEntity.ssdl|res://*/TestMVCAppEntity.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=SERVERNAME;Initial Catalog=DBNAME;Persist Security Info=True;User ID=USERID;Password=PWD;Connect Timeout=0;MultipleActiveResultSets=True\"");

IEnumerable<tblEmpDetail> temp = (from x in _db.tblEmpDetails
                                                             select x).ToList();




Add or update data in table tblEmpDetail:


 tblEmpDetail temp = new tblEmpDetail();          
           
TestMVCAppEntities2 _db = new TestMVCAppEntities2("metadata=res://*/TestMVCAppEntity.csdl|res://*/TestMVCAppEntity.ssdl|res://*/TestMVCAppEntity.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=SERVERNAME;Initial Catalog=DBNAME;Persist Security Info=True;User ID=USERID;Password=PWD;Connect Timeout=0;MultipleActiveResultSets=True\"");

            temp.EmpId = entity.Id;
            temp.Address1 = entity.Address1;
            temp.Address2 = entity.Address2;
            temp.CompanyName = entity.CompanyName;
            if (entity.Id == 0)
            {
                _db.ObjectStateManager.ChangeObjectState(temp, EntityState.Added);
                _db.tblEmpDetails.AddObject(temp);
            }
            else
            {
                if ((temp.EntityState == EntityState.Detached))
                    _db.tblEmpDetails.Attach(temp);
                _db.ObjectStateManager.ChangeObjectState(temp, EntityState.Modified);

            }
            _db.SaveChanges();


Delete data from table tblEmpDetail:

TestMVCAppEntities2 _db = new TestMVCAppEntities2("metadata=res://*/TestMVCAppEntity.csdl|res://*/TestMVCAppEntity.ssdl|res://*/TestMVCAppEntity.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=SERVERNAME;Initial Catalog=DBNAME;Persist Security Info=True;User ID=USERID;Password=PWD;Connect Timeout=0;MultipleActiveResultSets=True\"");


             tblEmpDetail item = _db.tblEmpDetails.Single(x => x.EmpId== 1);
            _db.tblEmpDetails.DeleteObject(item);
             _db.SaveChanges();
         


Wednesday 30 May 2012

Dynamically bind Telerik RAD Chart

Add Telerik chart in html source view :

<telerik:RadChart ID="rdChartSales" runat="server">
<Appearance Border-Visible="false"></Appearance>
<telerik:RadChart>
 


In code behind write the following code :
//clear series

rdChartSales.Clear();
rdChartSales.Series.Clear();
rdChartSales.PlotArea.XAxis.Clear();



//set height, width and apperance of chart

rdChartSales.Height = ChartHeight;
rdChartSales.Width = ChartWidth;
rdChartSales.Appearance.Dimensions.Height = ChartHeight;
rdChartSales.Appearance.Dimensions.Width = ChartWidth;
rdChartSales.ChartTitle.Appearance.Visible =
rdChartSales.Appearance.Border.Visible =
rdChartSales.PlotArea.Appearance.Dimensions.Width = 150;
rdChartSales.PlotArea.Appearance.Dimensions.Height = 120;
  




//set apperance of series
ChartSeries() chartSeries1 = new ChartSeries();
chartSeries1.Type = ChartSeriesType.StackedBar; chartSeries1.Appearance.TextAppearance.Visible = true;
chartSeries1.Appearance.FillStyle.MainColor = Color.Black;
chartSeries1.Appearance.FillStyle.SecondColor = Color.Red;


//add series to chart

rdChartSales.Series.Add(chartSeries1);
//add y axis range

rdChartSales.PlotArea.YAxis.AddRange(0,4,1);

//Add XAxis Periods
string[] xAxisArray = new string[] { "Bar1", "Bar2", "Bar3", "Bar4" };
foreach (string item in xAxisArray)
{
item1 = new ChartAxisItem(item);
rdChartSales.PlotArea.XAxis.Items.Add(item1);
}


//set values for chart series

rdChartSales.Series[0].SetValues(Target);

Wednesday 14 March 2012

The transaction log for database tempdb is full

If you get the error message for Tempdb Transaction log is full.


The transaction log for database 'tempdb' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases

Solution: 1

-- Check Tempdb Tran log file size using  dbcc sqlperf(logspace)



USE MASTER
GO
ALTER DATABASE TEMPDB MODIFY FILE (NAME='templog', SIZE=1500MB)

Solution:2

ALTER DATABASE Tempdb
ADD LOG FILE
( NAME = tempdblog2,
FILENAME = 'D:\MSSQL\DATA\tempdblog2.ldf',
SIZE = 10MB,
MAXSIZE = 100MB,
FILEGROWTH = 10MB)

Tuesday 6 March 2012

Exception:Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota.

In WCF, if there are large data to return, 
we get the following exception

Maximum number of items that can be serialized or deserialized in an
Object graph is ‘65536’.Change the object graph or increase the
MaxItemsInObjectGraph quota.

To resolve this,
we need to add the following settings in config file:

<behaviors>
<endpointBehaviors>
<behavior name="ExampleBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
</endpointBehaviors>
<
serviceBehaviors>
</behaviors> 

and in binding of WCF Service, we need to add behaviorConfiguration attribute

<endpoint address="exampleaddress" binding="basicHttpBinding" 
bindingConfiguration="BasicHttpBinding_IExampleService"
contract="ExampleService.IExampleService"
name="BasicHttpBinding_IExampleService"
behaviorConfiguration="ExampleBehavior"/>

Tuesday 28 February 2012

ASP.NET MVC 4 Features

The ASP.NET MVC 4 Beta includes a bunch of great new features and capabilities.  Some of the highlights include:
  • Bundling and Minification – ASP.NET MVC 4 includes the new bundling and minification support.  These features enable you to build web applications that load faster and feel more responsive to users, by minimizing the number and size of HTTP requests that your pages make.  Included with the MVC 4 beta are new “cache busting” helper methods that enable easy proxy caching of bundled files (with automatic invalidation if you change the cached CSS or JavaScript). 
  • Database Migrations – ASP.NET MVC 4 includes the new Entity Framework 4.3 release, which includes a bunch of great new features.  One of the most eagerly anticipated features it provides is database migration support.  This enables you to easily evolve your database schema using a code focused migration approach – and do so while preserving the data within your database.
  • Web API – ASP.NET MVC 4 includes some fantastic new support for creating “Web APIs”.  This enables you to easily create HTTP services and APIs that can be programmatically called from a broad range of clients (ranging from browsers using JavaScript, to native apps on any mobile/client platform).  The new Web API support also provides an ideal platform for building RESTful services.
  • Mobile Web – ASP.NET MVC 4 includes new support for building mobile web applications and mobile web sites, and makes it much easier to build experiences that are optimized for phone and tablet experiences. It includes jQuery Mobile, and includes new support for customizing which view templates are used depending upon what type of device is accessing the app. 
  • Razor Enhancements – ASP.NET MVC 4 includes V2 of our Razor View engine.  Razor V2 includes a bunch of juicy enhancements that enable you to make your view templates even cleaner and more concise – including better support for resolving URL references and selectively rendering HTML attributes.
  • Async Support and WebSockets – You’ll be able to take advantage of some additional language and runtime capabilities when using ASP.NET MVC 4 with .NET 4.5 and VS 11.  Async support is one of the big ones, and the ASP.NET MVC runtime support for this combined with the new C#/VB async language enhancements (which are super elegant and clean) is going to enable you to write incredibly scalable applications.  You will also be able to take advantage of the new WebSocket support built-into .NET 4.5 to build applications with even richer browser/server communication.