Friday, October 2, 2009

Adding an Entity Data Model to an MVC Application

When we try to access an Entity from a View, we get the following error :

Compiler Error Message: CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

In order to allow the Entities in our Model to be accessible/accessed by the View, we need to add the following reference in the Web.Config file :

<add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

SQL Server 2008, SELECT INTO statement

When we create/write a Select statement containing the INTO Clause, SQL Server creates a new table and inserts the results of the Query into the new table. When we run this Select statement containing the INTO Clause more than once, we get an Error : "There is already an object named 'Table1' in the database."

In order to avoid getting this error and to be able to run this Select statement multiple times by selecting into the same table, we need to add the following code to
1. check for the Existence of this table in the database,
2. drop the table if it already exists
3. Then run the Select statement with the Into Clause

IF EXISTS (SELECT * FROM sys.objects WHERE object_id =OBJECT_ID(N'[dbo].[Table1]') AND type in (N'U'))

BEGIN
Drop table Table1
END

Wednesday, September 30, 2009

Creating an MVC Web Application using Entity Framework

I was walking through some sample MVC applications I found on the web today, by trying to create them myself step by step. Even though I was following the steps explained in them very closely and accurately, I still could not get the Entity Data Model classes to get generated automatically in my application. In other words, even though I would create the sample database as described and the Model, I could not get the Entities to show up in the designer or the Model classes to get created.

I found out that if the tables in the database I am trying to generate a Model for, do not have a Primary Key defined on one of the columns, the EF will not be able to generate classes for the tables in that Database.
The Tutorials that I was walking through for learning MVC using EF did not mention this point.
So this was an important discovery I made and thought I might share with it others who may experience the same problem, in case they are new to EF.

Wednesday, September 9, 2009

Display Column Headers on each page of a Multi Page report created using SQL Server Reporting Services, 2008

1. Click on the down arrow on the side of Column headers at the bottom of the Report area/section.
2. Turn on the Advanced mode if not already on, by making sure that there is a check mark against it.
3. Once this mode is turned on, you will see static columns under Row Groups as well as Column Groups.
4. Select the very first Static section under Row Groups and this will open up the Properties pane on the right hand side for this static section
5. In the properties pane, set the following 2 properties :
a) KeepWithGroup = After
b) RepeatOnNewPage = True

I believe this is all we have to do to make the column headers repeat on each page. Since these settings are not a very obvious part of the report design process, I tend to forget them each time and thought it might be worthwhile to add them to my blog so I can refer to them next time I need to create such a report.

Friday, January 30, 2009

SQL Server 2008

How to add a new line character inside a SQL query statement :
char(13)+char(10)+REPLICATE (' ', x), where x stands for the number of spaces we want to add. So if we wanted to add 10 spaces we would use :

char(13)+char(10)+REPLICATE (' ', 10)