Issue:
How to Transpose (PIVOT (actually UNPIVOT ) / Transform) Columns into Rows
Sometimes you want to transpose Columns into Rows in SQL Server.
Solution:
The below T-SQL will transpose or transform Columns into Rows. It uses the reverse of PIVOT which is UNPIVOT.
DECLARE @Table Table
(NameCol1 varchar(10),
NameCol2 varchar(10),
NameCol3 varchar(10))
INSERT INTO @TABLE VALUES ('Name 1', 'Name 2', 'Name 3')
--INSERT INTO @TABLE VALUES ('Name 4', 'Name 5', 'Name 6')
--INSERT INTO @TABLE VALUES ('Name 7', 'Name 8', 'Name 9')
SELECT Name, Nameval
FROM
(SELECT NameCol1, NameCol2, NameCol3
FROM @TABLE) p
UNPIVOT
(NameVal FOR Name IN
(NameCol1, NameCol2, NameCol3)
)AS unpvt
-- OUTPUT
Name Nameval
------------- ----------
NameCol1 Name 1
NameCol2 Name 2
NameCol3 Name 3
Tuesday, October 9, 2012
Tuesday, September 18, 2012
Problem in mapping fragments starting at line (xxx):All the key properties (____) of the EntitySet ____ must be mapped to all the key properties
Issue:
Error 3003: Problem in mapping fragments starting at line xxx:All the key properties (table.column) of the EntitySet table must be mapped to all the key properties (table.column, table.column) of table table.
Cause:
The most common cause of this issue is when there are some key constrain changes in the database and these changes are not properly reflected in the Entity Model. Usually the change is reflected in the Database mapping in the Entity but not in the Entity classes. In this case, "Update Model from Database" option also doesn't work quiet well.
Solution:
1) Goto the Properties Window of the column that has the issue. (On the Entity Diagram Design View, right click -> Mapping Details -> Select the table with the issue column -> In Mapping Details window select the column and press F4 for its properties). In the properties window change Nullable property value from (None) to False.
2) If the Nullable property is already False, then the best option is to remove the table from the Entity Model. Then selection option "Update Model from Database". Select the removed table and click on Finish. Now the issue should be fixed.
Error 3003: Problem in mapping fragments starting at line xxx:All the key properties (table.column) of the EntitySet table must be mapped to all the key properties (table.column, table.column) of table table.
Cause:
The most common cause of this issue is when there are some key constrain changes in the database and these changes are not properly reflected in the Entity Model. Usually the change is reflected in the Database mapping in the Entity but not in the Entity classes. In this case, "Update Model from Database" option also doesn't work quiet well.
Solution:
1) Goto the Properties Window of the column that has the issue. (On the Entity Diagram Design View, right click -> Mapping Details -> Select the table with the issue column -> In Mapping Details window select the column and press F4 for its properties). In the properties window change Nullable property value from (None) to False.
2) If the Nullable property is already False, then the best option is to remove the table from the Entity Model. Then selection option "Update Model from Database". Select the removed table and click on Finish. Now the issue should be fixed.
Thursday, September 13, 2012
SQL Server "Saving changes is not permitted" Management Studio (SSMS)
"Saving changes is not permitted" - Error from SSMS when saving Table in Design View
"Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created."
This error happens from SQL Server Management Studio, when trying to save (edit/update) Table structure in Design View. Actually this is a SQL Server Management Studio (SSMS).
Resolution:
Tools -> Options -> Designers-> Uncheck "Prevent saving changes that require table re-creation"
"Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created."
This error happens from SQL Server Management Studio, when trying to save (edit/update) Table structure in Design View. Actually this is a SQL Server Management Studio (SSMS).
Resolution:
Tools -> Options -> Designers-> Uncheck "Prevent saving changes that require table re-creation"
Sunday, July 22, 2012
C# - Convert String to DateTime using ParseExact() method of DateTime
It is a common requirement to convert a Date/Time you have as a string to .NET native DateTime type.
The point to note here is to convey to the .NET Framework regarding the Date Format you provide as the input string. ParseExact() method of DateTime Type comes handy here.
Below is the solution to these
Convert String to DateTime in C# .NET
// String to DateTime
String dateText;
dateText = "1999-09-01 21:34 PM"; // Provide this according to your computer personal settings
DateTime myDate;
myDate = new DateTime();
myDate = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);
Convert DateTime to String in C# .NET
//DateTime to String
myDate = new DateTime(1999, 09, 01, 21, 34, 00);
String dateText = myDate.ToString("yyyy-MM-dd HH:mm tt");
Tuesday, July 10, 2012
C# - Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'int'
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
Issue:
In Linq queries sometimes you get the exception "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'". A scenario where you find this exception is as illustrated below:
int domainId = from p in pages
where p.Value.aspxFile == pageFileName
select p.Value.domain;
Logically I know there will only be one value being returned from this Linq Query statement, so it should work for me. But by syntax, this Linq statement returns type IEnumerable Collection.
Solution:
int domainId = (from p in pages
where p.Value.aspxFile == pageFileName
select p.Value.domain).First();
This solves my issue and it will return only one object and not a collection. The object type is determined by the compiler at compile time which will be int in my case.
You can use .First() or FirstOrDefault() or Single(). But for Single() make sure that there is exactly only one element in the list.
Monday, April 2, 2012
ASP.NET AJAX not working on Google Chrome and Safari - Update panel/Popup Extender.
Issue:
Recently i faced this issue when working with ASP.NET Ajax Updatepanel and Popup extender. The Popup was always visible and took a fixed space on the page like a normal div.
Solution:
Added the below javascript code to a .js file.
Sys.Browser.WebKit = {}; //Safari 3 is considered WebKit
if( navigator.userAgent.indexOf( 'WebKit/' ) > -1 )
{
Sys.Browser.agent = Sys.Browser.WebKit;
Sys.Browser.version = parseFloat( navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
Sys.Browser.name = 'WebKit';
}
Refer the .js file in the ScriptManager.
<ajax:ToolkitScriptManager ID="scripts" runat="server" ScriptMode="Release" EnableHistory="true"
EnableSecureHistoryState="false" EnablePageMethods="True" CombineScripts="true"
OnAsyncPostBackError="Page_OnAsyncError" OnNavigate="OnHistoryNavigate">
<Scripts>
<asp:ScriptReference Path="~/js/webkit.js" />
</Scripts>
</ajax:ToolkitScriptManager>
Sunday, April 1, 2012
JAVASCRIPT - How to remove last character from string
A common requirement, I have been approached is to have a javascript function to remove last character of a string. This is usually for some delimited string like a comma separated string.
var yourStr = "1, 2, 3, 4,"
var yourstrLen = yourStr.length;
yourStr = yourStr.slice(0,yourstrLen-1);
alert (yourStr);
Instead of
yourStr.slice(0,yourstrLen-1);
yourStr.slice(0, 1); is also equally acceptable, because negative value sets offset from the end of the string.
Subscribe to:
Posts (Atom)