I am trying to add a database diagram for the tables in Sql Server Management Studio and it's failing with the message "Database diagram support objects cannot be installed because this database does not have a valid owner". This is a bit weird as I thought the owner is already set.
May 30, 2014
In this post, I will quickly show how to get system related information like machine name, user name, domain name, or how long ago the system started, etc. Most of these information can be retrieved from System.Environment class and I will take advantage of this class to display this data.
Below is an example of how most of these information can be obtained from the System.Environment class.
May 29, 2014
Tables in a database can many to many relationships. For example, an Order can contain multiple Items and an Item can be in multiple Orders. In this scenario, the tables are linked to each other using a link table that contains the OrderId and ItemId. Here is how the table structure looks like.
To import these tables in Entity Framework model, I will create a new edmx file and drag and drop the tables into the designer view. You will notice that the link table does not appear in the designer view and the link is denoted by a line with multiple (*) sign at both ends.
Now the model can be queried in the usual way as we would query any other entity framework model. Here is an example on how to get Items by OrderId and to add Items and Orders in the database.
A List can contain simple types like int, string or other objects. Other day I was working and had to remove duplicates from a List collection. In this post, I will talk about how I remove the duplicates using minimum code. To start with a clue, I used a HashSet to remove the duplicates.
HashSet has the feature to only contain distinct items within it. Therefore, I could convert the List to HashSet and back to a list and this would remove all the duplicates for me. For HashSets to only contain unique items, the object needs to override the Equals and GetHashCode methods. Below is an example of how this can be done. I have created an Employee class with few members and overridden Equals and GetHashCode methods. Then I have another class EmployeeCollection where I have removed the duplicates.
In the previous post, I showed how to save an image to a database. In this post, I will show how to retrieve an image and display it in an asp.net page. Note that the image is saved in bytes format rather than as a file and displaying it in an asp.net Image control requires more work. Therefore, in this post, I have used the html img element to display the image.
May 26, 2014
May 24, 2014
This should have been an easy task to create a composite key using the design view in sql server management studio but it took me a while to figure it out. Basically, creating the composite key is easy and can be done in the following way.
I am importing an excel spread sheet to sql server database but for some reason I am getting the error “The value violated the integrity constraints for the column”. I checked the table data type and it should really just work but could not get it to work.
I am importing an excel spread sheet into a sql server database and is getting the error "DTS_E_INDUCEDTRANSFORMFAILUREONERROR" and it more messages like "The value could not be converted because of a potential loss of data" and "The source column is of nVarchar(272 -length) and the destination column is nVarchar(256 -length)"
So, really there are 2 failure messages here. After some investigation, I realised that with the first message "The value could not be converted because of a potential loss of data", it's because one of the rows had a double value whereas I initially set the sql table to have int data type. As a result, it failed while converting this particular row. Changing the data type to float fixed this issue.
The second error message is because the string value in the excel is lot bigger in length compared to the max length set in the sql table. Changing the max number of characters in sql table fixed this issue.
Overall, importing an excel spread sheet to sql table is quite easy provided correct data types are applied to the sql table.
May 7, 2014
This will cause the browser to load the specified page. The page may or may not be part of the existing application. This will result in a http get and the new page will not have access to any data on the current page. This can be triggered by any control that performs postback or even on Page_Load event.
Response.Redirect can be set in the following way.
PostBackUrl property can only be set on an asp.net button control. The UseSubmitBehavior property needs to be set to true. This will result in a http post and the new page can retrieve current page's data using Request.Form and Page.PreviousPage. The user will notice the change in the once the button is clicked. PostBackUrl can contain pages from current application or any other pages as well.
PostBackUrl is set in the following way.
To retrieve data,
Server.Transfer performs a http post and data can be obtained similar to PostBackUrl, that is, using both Request.Form and Page.PreviousPage. Server.Transfer can only transfer to a page in the current application. Also the url does not change in the browser. Like Response.Redirect, this can be triggered by any control that performs postback or even on Page_Load event.
Server.Transfer can be used in the following way.
To retrieve data,
May 6, 2014
I wrote this jQuery validation for a site and it all worked in my test page but for some reason it stopped working in the real page. Since the real page is a currently live, I started creating and testing the validation on the dummy page first. After some debugging for finding the issue, I realised that the jQuery is not running within the actual page since the controls and html elements are within the Update Panel.
May 5, 2014
Browser Link is a new feature in Visual Studio 2013 and it allows refreshing of multiple browsers at once. This is great for web developers as now we can test the application in multiple browsers and make modifications and see the changes all at once.
May 2, 2014
We may need to take a database offline various reasons, for example, it was a test database and we don’t need it anymore or the application has been decommissioned, etc. Sometimes, I have noticed that Sql Server Management Studio becomes unresponsive and hangs for a long time while taking the database offline. In this post I will talk about how to fix this issue.
Usually, the database can be taken offline by right-clicking on the name of the database and clicking Tasks > Take Offline as shown below.
Once I click on “Take Offline”, I expect the database to be taken offline but instead it hangs. To avoid the issue, I can write sql script to take the database offline. Following is the script that will take the database offline and then bring it online.
May 1, 2014
Most (if not all) web sites will have a default start page for example, default.aspx, index.aspx, home.aspx, etc. The default start page kicks in when someone goes to a url that does not refer to the page (no file extension) but possibly to a folder - for example, example.com/ or example.com/test . Without the default page, the user will be shown or not shown the directory listing page depending on the IIS configuration. For security reasons, it's better not to show end users the directory listing page. I once saw an application with directory listing that was showing the log folder and files.