Pages

Wednesday, November 23, 2011

The backup set holds a backup of a database other than the existing database

Wish you all a Happy Thanks Giving. Thanksgiving was real blessing this time because i got the needed rest from a tiring project. It was titring in many front because we were dealing with an accounting firm and we ought to get the project done before the end of November so they can sign off and proceed to go live. Now finally the the week of thanks giving we are done and they are merrily testing the solution. I wanted to blog this information here because it might be useful for many who are using custom database in there application solution.


We were buiilding a sharepoint solution and had a custom database for some of our custom operations. One of our developer who was new had to restore database from a backup and he got the error which he had no clue off. "System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing database". The error had nothing to do with him. He was trying to restore the database from the backup using SQL Server Management studio and using the restore option. There are couple of reasons why this error could come up

1. if restoring from a different version of SQL Server.
2. If  logical Name of the Database is different from the one that is backed up.

In our case it was the first option because we backed up long time ago and the developer was trying to restore it on SQL Server R2.

Following T-SQL query can be handy in fixing this. Make sure you run this query on the master and not on the database itself.


RESTORE DATABASE TestDatabase
FROM DISK = 'C:\BackupTestDatabase.bak'
WITH REPLACE

Happy Thanks Giving

http://g.co/doodle/ab23hc

Tuesday, October 18, 2011

CAML to Query User field

Sharepoint as a database is more of a Flat structure database. Querying is often times the most time consuming operation in especially when running against millions of List data. Microsoft has done a lot to increase the query time, still there are some limitations in sharepoint. Hence it is necessary to optimize the CAML as much as possible when querying against large Lists or document Libraries.

Often times I get asked by team members about writing CAML against Lookup Field and User Field. I decided to write it down here as a reference for everybody who wants to use CAML to query Sharepoint user field for me as well because every time some one asks me I tend to forget and needs to go back to code and give them the query. So I thought this is a right form so every body can be benefited.


Code:

SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Int'>{0}</Value></Eq></Where>", SPContext.Current.Web.CurrentUser.ID);
SPListItemCollection itemCollection = oList.GetItems(query);



I am using the sharepoint user field as sharepoint lookup field and performing the query based on user id. I prefer this method because there is no way this could go wrong as no 2 users can have same ID. This struck me when my team was implementing FBA and we created test user with same name. Interestingly the same query can be used for sharepoint Lookup field as well. If you notice the way Sharepoint stores the Look up field you can the format as

1;#lookupvalue

This same format is used for SPUser field as well. This set up is actually a blessing for us  "Developers"