Select Top 1 or Top n basically returns the first n rows of data based on the sql query. Select Top 1 1 or Top n s will return the first n rows with data s depending on the sql query.
July 25, 2014
I generated two scripts using Database project in Visual studio - one for updating the database and another for rollback if deployment fails. When I ran the update script, the database updated fine but I ran the rollback script - it failed with the message "Rows were detected. The schema update is terminating because data loss might occur". The issue is somewhat obvious; I have added new fields in a table and if I rollback, meaning if drop those fields then any data in those fields will be lost. So, how can we create script so we can run it. Since the script is needed for rollback, it might be necessary to run it.
July 22, 2014
I am building a console application that checks for username and password before proceeding. However, by default, the characters typed in console will appear which is not good as I don't want to display the password the user types in. So, how can I hide or mask the password.
In this code snippet, I will show how we could mask the password. In short, I will read through the characters entered for password and mask it using empty characters. Backspace will still work as expected. If the user hits the Enter key, it will move on for the next prompt.
July 18, 2014
In this example, I will show how to serialize an object in xml. In this post, I will use an Employee object, create a List of Employees and then serialize the object to xml. Serialization can data from objects needs to be saved in the filesystem or other storage and then used later for retrieval. Here is the code on how this can be achieved.
Once the code is run, the xml produced looks like below.
I deployed an asp.net application but when I ran the application, it failed with the message "Could not load file or assembly 'Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." . I re-checked the web.config file and it had the following line under assembly section
July 17, 2014
July 16, 2014
Here is a scenario in an application where the user logs out which takes them back to the login page but when the user hits the back button, he/she can view the previous page. During the log out event, Session is cleared and caching is disabled but the previous page is still shown. So, how can we work around this?
The browser history is an option to go back to previously visited pages (both forward & backward). The browser caches psges visited and the history buttons - backward and forward can be considered as a pointer that is linked to the page previously visited by the user.
So, to fix the issue, we need to make sure the pages don't get cached. Since the pages are behind authentication - it makes perfect sense not to cache the pages at all. We can implement the caching or not caching in the following ways in asp.net.
In the above code, you can paste the code in the master page for the authenticated pages or alternatively, if there is no fixed master page for authenticated pages - then, it needs to be places on all all required pages.
I run an instance of Sql Server in my local machine but noticed that my laptop is running very low in memory. So, I stopped the Sql Server by right clicking on the "Sql Server" name in Sql Server Management Studio and clicked Stop. A few prompts appeared that I had to click to stop the Sql Server.
July 11, 2014
I have set up a website using Windows Azure and have created a sql database on azure but when I run the site, I get the error message "There is already an open DataReader associated with this Command". In my previous post, I showed how to resolve this issue with Entity Framework; however, even after setting the connection string correctly, that is adding 'MultipleActiveResultSets=True' did not fix the issue.
So, after few trials and errors, I figured that the connection string needs to be modified from within the website configuration in Azure portal as well. So,
- Login to Azure portal,
- Navigate to the website from the left hand panel,
- Click on configure from the top menu,
- Scroll to "Connection String" section towards the bottom of the screen,
- Click to edit and add MultipleActiveResultSets=True;
That should be it. The issue should be resolved now. Enjoy.
July 10, 2014
While retrieving data from a Sql Server database using Entity Framework, I have sometimes received the error "There is already an open DataReader associated with this Command which must be closed first.". Entity Framework handles database connections properly, so what exactly is this issue?
Ater diving into it, I figured that the MultipleActiveResultSets attribute is set to false in the connection string which should be turned to true as there are multiple data retrieval commands running against a single database connection and maybe a select statement is running while another select statement is already executing.
Changing the MultipleActiveResultSets attribute to true fixed the issue. Here is a sample connection string.
July 7, 2014
I am using Entity Framework to insert data to a database and I am getting the exception below.
System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
at MethodName(Int32 id, ...) in c:\Users\project\filename.cs:line 33}