Playing with date time in both dotnet and sql server (t-sql) is always challenging and painful for me. Sometimes I forget that, entity framework cannot translate expressions into sql if I convert type using Convert class. like, Convert.ToDateTime(). If I do so then entity framework throws error like:
"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. "
To rescue from this, Microsoft introduces DbFunctions class from EF6 version. Prior of EF6 it was EntityFunctions. There are lot of built-in functions related to datetime type has been implemented there. Check them out.
Thursday, March 16, 2017
Saturday, January 28, 2017
Right Way for Editing EDMX File of Entity Framework (EF)
As an object relation mapper, Entity Framework enables user(in this case, developer) to work with relational data in object oriented manner. It helps user to do operations like insert, update, delete, search - in database without writing sql like string and getting help of intelligence of code editor like visual studio.
Updating Entity Framework from database could be hassle if user do not know where to do what. As for example, to update a stored procedure follow the following steps -
1. Open edmx file.
2. Right click and select Model Browser
Delete stored procedure
3. from Complex Type (if any)
4. from Function Imports
5. from Stored Procedure / Functions
Then save, then update from database.
If the Stored Procedure is complex type, that means, it is returning some rows (like table) then follow these steps -
1. Write these two lines at the beginning -
SET NOCOUNT ON
SET FMTONLY OFF
2. Use temporary table to store data and finally return that table.
IF OBJECT_ID('tempdb..##PGM_MonthlySalaryStatement') IS NOT NULL
BEGIN
DROP TABLE ##PGM_MonthlySalaryStatement
END
..........
..........
INSERT INTO ##PGM_MonthlySalaryStatement
..........
..........
SELECT * FROM ##PGM_MonthlySalaryStatement
Thats it.
Happy coding
Updating Entity Framework from database could be hassle if user do not know where to do what. As for example, to update a stored procedure follow the following steps -
1. Open edmx file.
2. Right click and select Model Browser
Delete stored procedure
3. from Complex Type (if any)
4. from Function Imports
5. from Stored Procedure / Functions
Then save, then update from database.
If the Stored Procedure is complex type, that means, it is returning some rows (like table) then follow these steps -
1. Write these two lines at the beginning -
SET NOCOUNT ON
SET FMTONLY OFF
2. Use temporary table to store data and finally return that table.
IF OBJECT_ID('tempdb..##PGM_MonthlySalaryStatement') IS NOT NULL
BEGIN
DROP TABLE ##PGM_MonthlySalaryStatement
END
..........
..........
INSERT INTO ##PGM_MonthlySalaryStatement
..........
..........
SELECT * FROM ##PGM_MonthlySalaryStatement
Thats it.
Happy coding
Thursday, June 23, 2016
Channel 9 usefull tutorials
Some channel 9 good tutorials:-
Creating Virtual and Augmented Reality Applications on Windows Platform
Creating Virtual and Augmented Reality Applications on Windows Platform
Developer Productivity: What's New in C# 6
Twenty C# Questions Explained
Security Fundamentals
Game Development:-
Construct 2 Advanced Game Development
Developing Advanced 2D Games with HTML5
Twenty C# Questions Explained
Security Fundamentals
Game Development:-
Construct 2 Advanced Game Development
Developing Advanced 2D Games with HTML5
SQL Server DB:-
Application Performance Monitoring
What's New with ASP.NET 5
Customizing ASP.NET Authentication with Identity
Web API Design
Cross-Platform Development with Xamarin & Visual Studio
Lighting Up Real-Time Web Communications with SignalR
Cross-Platform Development with Xamarin & Visual Studio
Lighting Up Real-Time Web Communications with SignalR
MVC:-
PowerShell:-
Advanced Tools & Scripting with PowerShell 3.0
Advanced PowerShell Techniques
Getting Started with PowerShell 3.0
TFS and Git:-
Software Testing:-
JavaScript:-
Introduction to jQuery
JavaScript Fundamentals: Development for Absolute Beginners
HTML5 & CSS3 Fundamentals: Development for Absolute Beginners
JavaScript Fundamentals: Development for Absolute Beginners
HTML5 & CSS3 Fundamentals: Development for Absolute Beginners
Miscellaneous:-
Microsoft Campus Tours
Do More With Your Microsoft Lumia Phone
A Developer's Guide to Windows 10
Visual Studio 2015 Enterprise Videos
Visual Studio TV
Thursday, February 18, 2016
EF error - Type '{your_entity_name}' already defines a member called '.ctor' with the same parameter types
Couple of days ago I have decided to set custom database connection string to my entity framework data model in one of my project. Then I have created a partial class with the same name of my entity and modify connection string name of base parameter. like example below -
public partial class DBPointOfSalesEntities
{
public DBPointOfSalesEntities()
: base(my.GetEntityConnectionString)
{
}
}
and deleted default constructor from [model].cs file.
But the problem is, whenever I update my model, it generates default constructor with default base parameter value. And occurs error for ambiguous constructor name as I have created above.
Then after googling, I found a nice solution - there is a [model].tt file which is called T4 template. Entity model is generating its automatic code based on this template. There is a specific code snippet which is responsible for generating default constructor after updating model from database each time. following is that code snippet. I have just deleted those lines and my problem fixed.public <#=code.Escape(container)#>()
: base("name=<#=container.Name#>")
{
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
this.Configuration.LazyLoadingEnabled = false;
<#
}
foreach (var entitySet in container.BaseEntitySets.OfType())
{
// Note: the DbSet members are defined below such that the getter and
// setter always have the same accessibility as the DbSet definition
if (Accessibility.ForReadOnlyProperty(entitySet) != "public")
{
#>
<#=codeStringGenerator.DbSetInitializer(entitySet)#>
<#
}
}
#>
}
Happy coding...
Wednesday, January 13, 2016
Using "Crystal Report" with "Visual Studio 2010"
First shocking news is Crystal Report tool is not included by default with Visual Studio 2010. To come back from this shock, download crystal report tool from following link
=> http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0.exe
After install don't forget to restart your computer. Because this is WINDOWS. Then after creating your first report if you get the following exception -
Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies.
Than open App.Config file. Then find the attribute useLegacyV2RuntimeActivationPolicy and change the value to "true"
Happy Coding..........
Thursday, November 5, 2015
ACM problem set 10300 - Ecological Premium
Hello there.
1
10
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
Output:-
99998000010
And following table is showing all fundamental integer types of variables in C:
Ecological premium is a very easy problem. Just you have to look at datatype. Consider following test case -
Input:-
10
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
99999 99999 99999
Output:-
99998000010
And following table is showing all fundamental integer types of variables in C:
| Keyword | Size | Range | Format Specifier |
|---|---|---|---|
| int | 2 | -32768 --> +32767 | %d |
| long | 4 | -2,147,483,648 --> 2,147,438,647 | %ld |
| long long | 8 | −9223372036854775807 --> +9223372036854775807 | %lld |
| unsigned int | 2 | 0 --> 65535 | %u |
| unsigned long | 4 | 0 --> 4,294,967,295 | %lu |
| unsigned long long | 8 | 0 --> 18446744073709551614 | %llu |
So carefully choice datatype.
Happy coding.
Happy coding.
Friday, July 31, 2015
ACM problem set - 100 - The 3n+1 Problem
2 months ago when I have started solving ACM ad-hoc problems, I started with The 3n+1 problem. And I have failed to solve as usual. Then next few days I was thinking some complex solutions for that problem but my misfortune. I have failed again and again I have failed.
Then I have focused on other ad-hoc problems. Solved some and learned that, "Sticky with Story!". The story of some ad-hoc problems have written in such way that readers started to think like they are going to solve a dynamic greedy np-hard problem. :D :D :D. One of my academic fellow who already competed regional math olympierd in Indonesia 2 times, he also gave-up ACM for this The 3n+1 problem!!
Here I am giving some clue for them who have tried this problem already and becoming frasted :)
clue 1: look at data type: declare all of your variables long (%ld)
clue 2: input would not always be given in sorted order. check whether first number < second number
clue 3: Show output in given order: If you swap first number and second number at the beginning of your program then be careful when showing output.
clue 4: If you got Time Limit Exit then check - does your program terminates correctly?
To terminate your program write program like this -
while( scanf("%ld %ld", &firstNumber, &secondNumber) != EOF )
{
// ... rest of your code ...
}
clue 5: The program algorithm is given in story. so you do not need to think about new algorithm.
clue 6: after each output there will be a new line. so,
printf("%ld %ld %ld\n", printNumberOne, printNumberTwo, output);
clue 7: debug, debug and debug if you get wrong output.
This problem is a Brute Force problem. so think straight.
Happy coding.
Then I have focused on other ad-hoc problems. Solved some and learned that, "Sticky with Story!". The story of some ad-hoc problems have written in such way that readers started to think like they are going to solve a dynamic greedy np-hard problem. :D :D :D. One of my academic fellow who already competed regional math olympierd in Indonesia 2 times, he also gave-up ACM for this The 3n+1 problem!!
Here I am giving some clue for them who have tried this problem already and becoming frasted :)
clue 1: look at data type: declare all of your variables long (%ld)
clue 2: input would not always be given in sorted order. check whether first number < second number
clue 3: Show output in given order: If you swap first number and second number at the beginning of your program then be careful when showing output.
clue 4: If you got Time Limit Exit then check - does your program terminates correctly?
To terminate your program write program like this -
while( scanf("%ld %ld", &firstNumber, &secondNumber) != EOF )
{
// ... rest of your code ...
}
clue 5: The program algorithm is given in story. so you do not need to think about new algorithm.
clue 6: after each output there will be a new line. so,
printf("%ld %ld %ld\n", printNumberOne, printNumberTwo, output);
clue 7: debug, debug and debug if you get wrong output.
This problem is a Brute Force problem. so think straight.
Happy coding.
Thursday, July 16, 2015
ACM problem set - 10055 - Hashmat the Brave Warrior
Hello all. Today is my last fasting day of this Arabic fiscal year. I am preparing myself to celebrate EID-day tomorrow. But I am missing my family a lot. Without them nothing could be fulfilled.
Today I was looking into an acm problem 10055 - Hashmat the brave warrior. At first I was disappointed by the problem setter by thinking that, what a easy problem! - Simple subtraction. And wrote code in 2 minutes and got 3 times penalty from online judge.
What the hell!! simple subtraction :(
Then I read the problem again and again and found the tricks hidden in story. :)
You can try once. just find the tricks. you do not need to solve.
Happy coding.
Today I was looking into an acm problem 10055 - Hashmat the brave warrior. At first I was disappointed by the problem setter by thinking that, what a easy problem! - Simple subtraction. And wrote code in 2 minutes and got 3 times penalty from online judge.
What the hell!! simple subtraction :(
Then I read the problem again and again and found the tricks hidden in story. :)
You can try once. just find the tricks. you do not need to solve.
Happy coding.
Thursday, June 4, 2015
Do you know which windows port is used by Microsoft SQL Server?
The default TCP port of Microsoft SQL Server default instance is 1433. But if the named instance is different than default instance then SQL server could use different port. Here I found a useful script to find out which TCP port is currently used by SQL server.
DECLARE @InstName VARCHAR(16)
DECLARE @RegLoc VARCHAR(100)
SELECT @InstName = @@SERVICENAME
IF @InstName = 'MSSQLSERVER'
BEGIN
SET @RegLoc='Software\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\'
END
ELSE
BEGIN
SET @RegLoc='Software\Microsoft\Microsoft SQL Server\' + @InstName + '\MSSQLServer\SuperSocketNetLib\Tcp\'
END
EXEC [master].[dbo].[xp_regread] 'HKEY_LOCAL_MACHINE', @RegLoc, 'tcpPort'
Now, why it is important to know this TCP port number?
If you are planning to access database from remote computer and/or
if the firewall of your computer is opened then you have to allow that TCP port number as exception.
Thats it.
Happy Coding.
DECLARE @InstName VARCHAR(16)
DECLARE @RegLoc VARCHAR(100)
SELECT @InstName = @@SERVICENAME
IF @InstName = 'MSSQLSERVER'
BEGIN
SET @RegLoc='Software\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\'
END
ELSE
BEGIN
SET @RegLoc='Software\Microsoft\Microsoft SQL Server\' + @InstName + '\MSSQLServer\SuperSocketNetLib\Tcp\'
END
EXEC [master].[dbo].[xp_regread] 'HKEY_LOCAL_MACHINE', @RegLoc, 'tcpPort'
Now, why it is important to know this TCP port number?
If you are planning to access database from remote computer and/or
if the firewall of your computer is opened then you have to allow that TCP port number as exception.
Thats it.
Happy Coding.
Sunday, April 19, 2015
Subversion cleanup issue.
I have been using subversion for last two years. TortoiseSVN is a great free subversion client for freelancer like me. Although I am using AnkhSVN subversion client to manage subversion from Visual Studio IDE. Today during work, I have deleted a folder manually from my project working directory. After that my nightmare has just began. Whenever I was trying to commit/update/revert/unlock my project, it was showing a nice error "Previous operation has not finished: run 'cleanup' if it was interrupted". Sometimes the error text was different but was containing at-least "cleanup" word. Like, "Please execute the 'Cleanup' command". Well, there is a cleanup button in both TortoiseSVN and AnkhSVN subversion client. But same error was showing after clicking cleanup button too.
It was clear to me that, I was ended up in such issue, which was not possible to resolve by my subversion client. After a long time I have found a solution in stackoverflow. Subversion repository is using a internal SQLite database (wc.db in .svn folder) which contains a table named work_queue. Whenever we are trying to do an event, subversion repository searches previous pending task in that table and compare current task with that pending task and forward.
The solution was to delete all entries of work_queue table. There is an executable file of SQLite in SQLite official website. Download and unzip in project working directory. Then run the following command from windows command prompt(cmd.exe) to see the pending tasks -
sqlite3.exe .svn/wc.db "select * from work_queue"
To delete all old pending task, run the following command
sqlite3.exe .svn/wc.db "delete from work_queue"
Then try again "Cleanup" from subversion client.
Hope this will help to get rid of this cleanup issue.
Happy coding.
Tuesday, October 7, 2014
Accessing "enum" values in .NET
Accessing enum values are very interesting in this sense that if you directly call enum item then it will return the left part of equal (=) sign. If you convert the enum item into integer then you will get right part of equal (=) sign. Lets see an example.
public enum NeedRefreshDataSource
{
Yes = 1,
No = 2
}
Now,
if you write NeedRefreshDataSource.Yes then you will get - Yes which is NeedRefreshDataSource type value.
if you write NeedRefreshDataSource.yes.ToString() then you will get - "Yes" which is string type value.
if you write Convert.ToInt32(NeedRefreshDataSource.yes) then you will get - 1 which is integer type value.
if you write Convert.ToInt32(NeedRefreshDataSource.yes).ToString() then you will get - "1" which is string type value.
Happy Coding...
public enum NeedRefreshDataSource
{
Yes = 1,
No = 2
}
Now,
if you write NeedRefreshDataSource.Yes then you will get - Yes which is NeedRefreshDataSource type value.
if you write NeedRefreshDataSource.yes.ToString() then you will get - "Yes" which is string type value.
if you write Convert.ToInt32(NeedRefreshDataSource.yes) then you will get - 1 which is integer type value.
if you write Convert.ToInt32(NeedRefreshDataSource.yes).ToString() then you will get - "1" which is string type value.
Happy Coding...
Monday, August 18, 2014
Export crystal report to Excel - some tips.
Developing a crystal report which will be exported into Excel is always tricky, time consuming and needs a lot of patience. There are a lot of blog posts, forum discussions, articles in the web about this matter. During last couple of days I was hanging with one of my report and at last I've figured out some tricks about exporting crystal report to excel. Here is some -
1. Do not use your mouse to resize/align your header/detail fields. Always depend on Properties pane. make sure the height. width and left properties are always same of related header and detail fields.
2. Top property of header/detail fields should be always 0.
2. Do not use line object. It will create an extra excel row after export.
3. You can use (single) right and bottom border of detail field objects. and only right border of header objects.
4. May be the report which will export to excel will not be presentable for clients. So create another presentable report for client which may have lines or brders or colors whatever you want.
5. After export into excel - may be your header or detail section will be created using more than one excel rows. This may not be a problem. But you can decrease the height of detail/header fields to accommodate in single excel row.
6. Suppress (Drill Down) the report header, report footer, page header, page footer. Because they are value less in Excel.
7. In your data source do not keep null data in any field. At-least fill data with empty string/something not valuable like dash (-).
May be these tricks will help you.
Happy coding!
(also published in codeproject.com)
1. Do not use your mouse to resize/align your header/detail fields. Always depend on Properties pane. make sure the height. width and left properties are always same of related header and detail fields.
2. Top property of header/detail fields should be always 0.
2. Do not use line object. It will create an extra excel row after export.
3. You can use (single) right and bottom border of detail field objects. and only right border of header objects.
4. May be the report which will export to excel will not be presentable for clients. So create another presentable report for client which may have lines or brders or colors whatever you want.
5. After export into excel - may be your header or detail section will be created using more than one excel rows. This may not be a problem. But you can decrease the height of detail/header fields to accommodate in single excel row.
6. Suppress (Drill Down) the report header, report footer, page header, page footer. Because they are value less in Excel.
7. In your data source do not keep null data in any field. At-least fill data with empty string/something not valuable like dash (-).
May be these tricks will help you.
Happy coding!
(also published in codeproject.com)
Monday, August 4, 2014
Getting worksheet from workbook when there is no such worksheet exists.
During application-level excel add-in development sometimes we need to check whether the worksheet is null or not. Consider the following lines -
string myWorkSheetName = "a_worksheet_Name";
Excel.Workbook Wb= Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet mySheet = Wb.Worksheets.get_Item(myWorkSheetName) as Excel.Worksheet;
or,
Excel.Worksheet mySheet = Wb.Worksheets[myWorkSheetName] as Excel.Worksheet;
The worksheets indexer ([])
or the get_Item method of Worksheets class does not return nullable object. In this case we can take advantage of LINQ -
Excel.Worksheet mySheet = Wb.Worksheets.Cast().Where(w => w.Name == myWorkSheetName).FirstOrDefault();
If the worksheet does not found then mySheet will contain null.
string myWorkSheetName = "a_worksheet_Name";
Excel.Workbook Wb= Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet mySheet = Wb.Worksheets.get_Item(myWorkSheetName) as Excel.Worksheet;
or,
Excel.Worksheet mySheet = Wb.Worksheets[myWorkSheetName] as Excel.Worksheet;
The worksheets indexer ([])
or the get_Item method of Worksheets class does not return nullable object. In this case we can take advantage of LINQ -
Excel.Worksheet mySheet = Wb.Worksheets.Cast().Where(w => w.Name == myWorkSheetName).FirstOrDefault();
If the worksheet does not found then mySheet will contain null.
Monday, June 2, 2014
MS Office Add-in development: Enable "Embed Interop Types".
Warning 1 A reference was created to embedded interop assembly 'c:\Program Files\Reference Assemblies\Microsoft\VSTO40\v4.0.Framework\Microsoft.Office.Tools.Common.dll' because of an indirect reference to that assembly created by assembly 'c:\Program Files\Reference Assemblies\Microsoft\VSTO40\v4.0.Framework\Microsoft.Office.Tools.Word.dll'. Consider changing the 'Embed Interop Types' property on either assembly.
Developers get this is common warning during developing add-in for Microsoft Office. Because the type equivalence feature is not enabled by default in a office project. To enable this feature go to properties of
-- Microsoft.Office.Tools.Common
-- Microsoft.Office.Tools
-- Microsoft.Office.Tools.Word
-- Microsoft.Office.Tools.Excel
-- Microsoft.Office.Tools.Outlook - references and
set "True" to "Embed Interop Types" property.
The type equivalence feature, embeds type information to the office solution. And enables the version independence at run time
Developers get this is common warning during developing add-in for Microsoft Office. Because the type equivalence feature is not enabled by default in a office project. To enable this feature go to properties of
-- Microsoft.Office.Tools.Common
-- Microsoft.Office.Tools
-- Microsoft.Office.Tools.Word
-- Microsoft.Office.Tools.Excel
-- Microsoft.Office.Tools.Outlook - references and
set "True" to "Embed Interop Types" property.
The type equivalence feature, embeds type information to the office solution. And enables the version independence at run time
Tuesday, October 29, 2013
Md Moheeb Ullah Patuary invited you to check out Dropbox
| |||||||||||||||
Saturday, April 13, 2013
AllowUserToResizeColumns property of DataGridView control.
If you see that you cannot resize the column of a DataGridView control though you set true value of AllowUserToResizeColumns property then check AutoSizeColumnsMode property also. Choose Fill/None value of this AutoSizeColumnsMode property.
Happy coding..
Monday, April 8, 2013
SQL Execution Error: Cannot insert explicit value for identity column in table.....
Sometimes we get a very interesting error when we try to insert a new row in a table. The error is like following:
Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF
Then we developers starts googling and find almost same solution.
SET IDENTITY_INSERT TableName On
insert into TableName Values(1,1,1)
SET IDENTITY_INSERT TableName OFF
This solution is ok. But before going to that solution we need to know in which context we have to set identity_insert on.
The context is if we want to insert data to an Identity Specified column. (Generally we specify the primary key column identical.)
So, do we want to insert the data to a Identity column? If answer is “No” then check your sql query. I have seen many beginners do this wrong in their sql query.
So, Happy Coding.
So, Happy Coding.
About a MS SQL Server Table Schema Change Warning - Saving changes is not permitted......
After changing a table schema sometimes we get a MS SQL Server warning saying that - Saving Changes is not permitted. The changes you have made require the following tables to be dropped and re-created......
To avoide this warning do the following:-
1. From Tool menu select Options.
2. Expand "Designers" node. Then uncheck "Prevent saving changes that require table re-creation". Then click OK.
For Visual Studio select Tools --> Options then under "Database Tools" under "Table and Database Designers" select "Prevent saving changes that require table re-creation"
That's all. Happy coding :)
Wednesday, March 20, 2013
Test: Post Hit Counting for Blogspot Blogger
Hello all. last day I have thought about post hit counter for my blog. Then I googling and find some good posts on it. Among them I choose this post and implement it in my blog. It is working like a charm.
One thing you need to know that you can see the hit counter only for new posts after setting up the post counter feature in your template.
Thank you Mohammad Mostafa and Happy Blogging.
Subscribe to:
Posts (Atom)




