Skip to main content

Posts

ViewData vs ViewBag vs TempData vs Session

ViewData vs ViewBag vs TempData vs Session   In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article, I am trying to explain the differences among these four. ViewData ViewData is a dictionary object that is derived from ViewDataDictionary class. public ViewDataDictionary ViewData { get ; set ; } ViewData is a property of ControllerBase class. ViewData is used to pass data from controller to corresponding view. It’s life lies only during the current request. If redirection occurs then it’s value becomes null. It’s required typecasting for getting data and check for null values to avoid error. ViewBag ViewBag is a dynamic property that takes advantage of the new dynam...

Difference between document.ready and window.onload or pageLoad

Difference between document.ready and window.onload or pageLoad   We think pageLoad() and jQuery’s $(document).ready() events do the same. Both methods seem too similar in simple demo example. But $(document).ready() and pageLoad() methods are very much differ in functioning.In this article, I will explain the major differences between $(document).ready() and pageLoad() methods. Introducing $(document).ready() JQuery’s document.ready() method gets called as soon as DOM is ready (means browser has parsed the HTML and built the DOM tree). It is cross browser compatible means behave equally on all browsers. If your web page has large images, it will not wait for loading of images completely. Hence it may called before pageLoad() method. We can have multiple document.ready() methods on a web page that will be called in coming sequence.      <script type="text/javascript">     $(document).ready(function(){      // G...

How can you handle concurrency at field level in LINQ to SQL?

How can you handle concurrency at field level in LINQ to SQL? Ans. In LINQ to SQL, you can also handle concurrency at field level and this is the best way provided by the LINQ. To achieve this you need to define UpdateCheck attribute at field level and it has the following options: 1. Never: - This option will never check for concurrency conflicts. 2. Always: - This option will always check for concurrency conflicts. 3. WhenChanged: - This option will check for concurrency conflicts when the field’s value has been changed. [Column( DbType = "nvarchar(50)" , UpdateCheck = UpdateCheck.Never)] public string CustomerID { set {CustomerID = value ;} get { return _CustomerID;} }

How can you handle concurrency in LINQ to SQL?

How can you handle concurrency in LINQ to SQL? Ans. When multiple users are updating the same record at the same time, a conflict will occur. To handle this concurrency conflicts, LINQ provides you following three ways. 1. KeepCurrentValues - This option will remains the LINQ object values as it is and does not push the new values from the database in to the LINQ object. 2. OverwriteCurrentValues - This option will replace the LINQ object values with the database values. 3. KeepChanges - In this case changed properties of an object/entity remains as it is but the properties which are not changed are fetched from the database and replaced. All these options are available in RefereshMode enum which exist in System.Data.Linq namespace. To handle concurrency conflicts, wrap the code with in a try block and catch the ChangeConflictException by using catch block and iterate through the ChangeConflicts collection to resolve the conflict as shown below: DataContext db = new DataContext(); try...

What is difference between ADO.NET and LINQ to SQL?

What is difference between ADO.NET and LINQ to SQL? Ans. There are following differences between ADO.NET and Entity Framework: ADO.NET  and   LINQ to SQL It is a part of .NET Framework since .NET Framework 1.0 It is a part of .NET Framework since .NET Framework 3.5 SqlConnection/OleDbConnection is used for database connectivity. We can use context for database connectivity. Difficult to debug and cause syntax errors at run-time. Easy to debug and cause syntax errors at compile-time. It has full type checking at run-time and no IntelliSense support in Visual Studio, since it used the T-SQL to query the database. It has full type checking at compile-time and IntelliSense support in Visual Studio, since it used the .NET Framework languages like C# and VB. It used T-SQL to query the data to query the database and some other syntax for querying the other data source. It used LINQ to query the data which provides the uniform programming model (means common query syntax) to que...

What is difference between XElement and XDocument?

What is difference between XElement and XDocument? Ans. XElement and XDocument are the classes defined with in System.Xml.Linq namespace. XElement class represents an XML fragment. While XDocument class represents an entire XML document with all associated properties. For Example: XDocument doc = new XDocument( new XElement( "Book" , new XElement( "Name" , ".NET Interview FAQ" ), new XElement( "Author" , "Shailendra Chauhan" )) );

What is difference between LINQ and Stored Procedures?

What is difference between LINQ and Stored Procedures? Ans. There are the following differences between LINQ and Stored Procedures. 1. Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure. 2. LINQ has full type checking at compile-time and IntelliSense support in Visual Studio as compared to stored procedure. This powerful feature helps you to avoid run-time errors. 3. LINQ allows debugging through .NET debugger as compared to stored procedure. 4. LINQ also supports various .NET framework features like multi –threading as compared to stored procedures. 5. LINQ provides the uniform programming model (means common query syntax) to query the multiple databases while you need to re-write the stored procedure for different databases. 6. Stored procedure is a be...