Visual Studio, C# : Why should or should not you use Code Contracts #Root #By Sayed Ahmed

http://programmers.stackexchange.com/questions/211337/why-would-i-use-code-contracts

Why Use?

To avoid null reference exceptions

To avoid critical and costly failure of code/software

However, code may look over crowded, and can be difficult to maintain and extend if you use Contracts.

 

If yours is not a critical application, and in general, failures can never be that costly for you: then you might not need to use it everywhere ().

 

However, be careful in writing your code, deal (address) with the cases where the objects might be null, additionally, properly initialize your variables.

 

Read the page as provided in the url above.

 

Related Tools: If you want to use Contracts, you need to install both of the tools below:

 

https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970

https://visualstudiogallery.msdn.microsoft.com/02de7066-b6ca-42b3-8b3c-2562c7fa024f

 

That’s right I had to install to work with.

After you install, you will see a tab Code Contracts in the project properties.

 

The useful part:

Contracts vs. Null Object pattern

Now this is at least in the same ball park. Languages like Scala and Haskell has had great success with this approach to eliminating null references entirely from programs. (Even if Scala formally allows nulls the convention is to never use them)

If you already employ this pattern to eliminate NREs you’ve basically removed the largest source of runtime failures there is in basically the manner contracts allow you to do it.

The difference might be that contracts has an option to automatically require all your code to avoid null, and thus force you to use this pattern in more places to pass compilation.

On top of that contracts also give you the flexibility to target things beyond null. So if you no longer see any NRE in your bugs you might want to use contracts to strangle the next most common issue you might have. Off by one? Index out of range?

But…

All that being said. I do agree that the syntactic noise (and even structural noise) contracts add to the code is quite substantial and the impact the analysis has on your buildtime should not be underestimated. So if you decide to add contracts to your system it would probably be wise to do so very carefully with a narrow focus on which class of bugs one tries to address.

 

 

Read this as well:

http://research.microsoft.com/en-us/projects/contracts/faq.aspx

  From: http://sitestree.com/?p=2115
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-07-25 08:51:38

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Restart Jupyter Notebook Kernel using Python Code

import IPython IPython.Application.instance().kernel.do_shutdown(True) 
#automatically restarts kernel
Worked for me
https://stackoverflow.com/questions/37751120/restart-ipython-kernel-with-a-command-from-a-cell




*** . *** *** . *** . *** . ***

Courses: http://Training.SitesTree.com (Big Data, Cloud, Security, Machine Learning)
Blog
: http://Bangla.SaLearningSchool.com, http://SitesTree.com

8112223 Canada Inc./JustEtc: http://JustEtc.net

Shop Online: https://www.ShopForSoul.com/
Linkedin: https://ca.linkedin.com/in/sayedjustetc

Medium: https://medium.com/@SayedAhmedCanada

C# Image Dimension . Keep the dimension as it is when Exporting to Excel #Root #By Sayed Ahmed

System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:ggsggs Accessimagesmembers1.jpg");

int width = img.Width;
int height = img.Height;

---
Excel and Exporting

When you export to Excel using OpenXML standard
You usually mention where to start and where to end. I.e. cell to insert from and the width/height in number of cells.

Hence, you need to know how many pixels are in one cell for excel (if you want to keep the same height and width)

In general: 
64 pixels on the width
15 pixels on height
(may have an assumption here, Excel with Arial and 10 size font)
--

There are C# Classes and Methods to know/calculate how many pixels per cell.
It actually also depends on some configurations of the Excel file
---
When you use Calibri and 11 size font,
56 pixels on the width
and 17 pixels on height
may work

ideally, you should use code/C# classes to find out (pixels per cell) for the current document.

Still, if you want to use configurations and hard-coded numbers, you can provide the configurations in the web.config file under app.settings using key/value pair.
To retrieve:

WebConfigurationManager.AppSettings["WidthInPixelsForACell"];

--

From: http://sitestree.com/?p=2099
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-07-18 22:49:17

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Using Kendo Chart; Kendo Chart Events #Root #By Sayed Ahmed

Using Kendo Chart; Kendo Chart Events

Sayed Ahmed

 

One example that helped in real projects

http://demos.telerik.com/kendo-ui/tabstrip/events

 

The example shows how you can execute functionality on different Kendo Chart events. You can see the methods and events mapping as below

$("#tabstrip").kendoTabStrip({
                    select: onSelect,
                    activate: onActivate,
                    show: onShow,
                    contentLoad: onContentLoad,
                    error: onError,
                    contentUrls: [ , "../content/web/tabstrip/ajax/ajaxContent1.html", "error.html" ]
                });

Problem to Solve:
In my case, under a Kendo Chart (actually in an embedded iframe), there were some buttons. The kendo chart had multiple tabs. The height of the tabs were not the same as the contents were different. Hence, because of the iframe height (as was set to the height of the most height tab content - before this solution was applied), the buttons were far off (down) sometimes (if a tab content was not much). I was required to bring the buttons just under the content.

Solution:
Used Kendochart events to resolve the issue, and depending on the tab content height, re-sized the iframe height.

if you use select event, you may see that the buttons are still far off esp. when you move from a more height tab to a less height tab for the first time.  

Activate event helped to resolve the issue.

Another Problem to Solve:
the iframe was showing remote content i.e. cross domain content where I had access to both domains. Used post message communications to send the page height information to the client/parent and from parent/client the iframe size was adjusted. 

Please also check if you need to configure the remote Webserver for allow-access-origin or similar. I did though there were multiple stuff to do on cross-domains; hence, if for this particular case, allow-access-origin is mandatory or not, I need to double check. However, apparently, you need it or you need something similar as even for post message communications, you need to work with both domains.

Anyway, the complete code from the URL is as below as I just mentioned (just in case, the page is not available in future.).

 $(document).ready(function() {
                function onSelect(e) {
                    kendoConsole.log("Selected: " + $(e.item).find("> .k-link").text());
                }

                function onShow(e) {
                    kendoConsole.log("Shown: " + $(e.item).find("> .k-link").text());
                }

                function onActivate(e) {
                    kendoConsole.log("Activated: " + $(e.item).find("> .k-link").text());
                }

                function onContentLoad(e) {
                    kendoConsole.log("Content loaded in <b>"+ $(e.item).find("> .k-link").text() + "</b> and starts with <b>" + $(e.contentElement).text().substr(0, 20) + "...</b>");
                }

                function onError(e) {
                    kendoConsole.error("Loading failed with " + e.xhr.statusText + " " + e.xhr.status);
                }

                $("#tabstrip").kendoTabStrip({
                    select: onSelect,
                    activate: onActivate,
                    show: onShow,
                    contentLoad: onContentLoad,
                    error: onError,
                    contentUrls: [ , "../content/web/tabstrip/ajax/ajaxContent1.html", "error.html" ]
                });
            });
        </script>

From: http://sitestree.com/?p=2091
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-07-12 18:32:41

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Misc Dot Net Stuff #Root #By Sayed Ahmed

In real life projects in .Net platform, you might need to know and understand the following:

 

http://stackoverflow.com/questions/10498102/debugging-with-vs-other-than-localhost

http://stackoverflow.com/questions/4653236/unable-to-start-debugging-on-the-web-server-could-not-start-asp-net-debugging-v

http://stackoverflow.com/questions/16922742/what-is-the-entry-point-for-an-asp-net-mvc-4-application

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh750287.aspx

https://msdn.microsoft.com/en-ca/library/dd577079.aspx

http://blogs.msdn.com/b/amb/archive/2012/07/31/easiest-way-to-generate-machinekey.aspx

http://stackoverflow.com/questions/8747912/how-to-install-dotnetnuke-so-i-can-migrate-existing-users

http://stackoverflow.com/questions/11930381/log4net-multiple-appenders-writing-to-event-viewer

https://portal.smartertools.com/kb/a2487/where-are-my-iis-log-files-stored.aspx

  From: http://sitestree.com/?p=2084
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-08-07 18:01:36

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

misc #Root #By Sayed Ahmed

misc From: http://sitestree.com/?p=2062
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-07-04 10:06:53

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Some C# Stuff (or similar) as I used #Root #By Sayed Ahmed

Sayed Ahmed

 

Some C# Stuff (or similar) as I used

 

HP var_dump in C# to dump array or objects?

http://stackoverflow.com/questions/23824899/php-var-dump-in-c-sharp-to-dump-array-or-objects

 

How to pass an object array from javascript to server

http://stackoverflow.com/questions/12229530/how-to-pass-an-object-array-from-javascript-to-server

 

How to Convert JSON object to Custom C# object?

http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object

 

The following did work for me

        JavaScriptSerializer serializer = new JavaScriptSerializer();
            var jsonObject = serializer.Deserialize<dynamic>(json);
            name = (string)jsonObject["user"]["name"];
            teamname = (string)jsonObject["user"]["teamname"];
            email = (string)jsonObject["user"]["email"];
            players = jsonObject["user"]["players"];
        }

You could use newtonsoft.json or similar.
My case was little more complicated, JSON string representing: arrays of objects of arrays ofobjects. You could also define the class and could do without dynamic and using Newtonsoft's DeSerialize

C# Parsing JSON array of objects

http://stackoverflow.com/questions/19910476/c-sharp-parsing-json-array-of-objects

 

The following might also work

string json = "{'results':[{'SwiftCode':'','City':'','BankName':'Deutsche    Bank','Bankkey':'10020030','Bankcountry':'DE'},{'SwiftCode':'','City':'10891    Berlin','BankName':'Commerzbank Berlin (West)','Bankkey':'10040000','Bankcountry':'DE'}]}";

        var resultObjects = AllChildren(JObject.Parse(json))
            .First(c => c.Type == JTokenType.Array && c.Path.Contains("results"))
            .Children<JObject>();

        foreach (JObject result in resultObjects) {
            foreach (JProperty property in result.Properties()) {
                // do something with the property belonging to result
            }
        }

From: http://sitestree.com/?p=2052
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-06-30 18:13:33

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

PIVOT Examples – SQL #Root #By Sayed Ahmed

http://stackoverflow.com/questions/24470/sql-server-pivot-examples

SELECT Action,
       MAX( CASE data WHEN 'View' THEN data ELSE '' END ) ViewCol, 
       MAX( CASE data WHEN 'Edit' THEN data ELSE '' END ) EditCol
 FROM t
 GROUP BY Action

---

SELECT act AS 'Action', [View] as 'View', [Edit] as 'Edit'
FROM (
    SELECT act, cmd FROM data
) AS src
PIVOT (
    MAX(cmd) FOR cmd IN ([View], [Edit])
) AS pvt

--

Table setup:

CREATE TABLE dbo.tbl (
    action VARCHAR(20) NOT NULL,
    view_edit VARCHAR(20) NOT NULL
);

INSERT INTO dbo.tbl (action, view_edit)
VALUES ('Action1', 'VIEW'),
       ('Action1', 'EDIT'),
       ('Action2', 'VIEW'),
       ('Action3', 'VIEW'),
       ('Action3', 'EDIT');

Your table: SELECT action, view_edit FROM dbo.tbl

Your table

Query without using PIVOT:

SELECT Action, 
[View] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'VIEW'),
[Edit] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'EDIT')
FROM tbl t
GROUP BY Action

Query using PIVOT:

SELECT [Action], [View], [Edit] FROM
(SELECT [Action], view_edit FROM tbl) AS t1 
PIVOT (MAX(view_edit) FOR view_edit IN ([View], [Edit]) ) AS t2

Both queries result:
enter image description here

 

  From: http://sitestree.com/?p=2040
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-06-14 21:36:12

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Excel and C# #Root #By Sayed Ahmed

Adding image at a particular cell in Excel Spreadsheet

https://social.msdn.microsoft.com/Forums/lync/en-US/5c6e7ebd-66e2-40fa-9194-aed5cdc3f0ae/adding-image-at-a-particular-cell-in-excel-spreadsheet?forum=oxmlsdk

 

How to: Insert text into a cell in a spreadsheet document (Open XML SDK)

https://msdn.microsoft.com/en-us/library/office/cc861607.aspx

 

You will need reference to OpenXML, Systems.IO.Packaging, System.xml and/or similar.

 

You may need to copy the related DLLs to your production at deployment (depends on your deploy method)

 

to insert image located at another site (if any and under your control), you may need to change IIS config for : Allow Access Control

 

 

  From: http://sitestree.com/?p=2038
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-06-14 21:29:23

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Random Information on DNN #Root #By Sayed Ahmed

DotNetNuke.Common.Globals.NavigateURL() does not do a redirect, it simply creates a target URL based on the parameter you’ve provided.

IIS 7 enable 32 bit application mode
appcmd apppool set /apppool.name:MyNukeSite /enable32BitAppOnWin64:true

on IIS 6
cscript %SystemDrive%inetpubAdminScriptsadsutil.vbs set w3svc/AppPools/Enable32bitAppOnWin64 1
aspnet_regiis.exe -i

How to avoid adding multiple references to JQuery Libraries. And how to refer only when JQuery is not loaded. Will help in DNN as well.
// ‘load-jquery.js’
window.onload = function () {
if (window.jQuery === undefined) {
var script = document.createElement(‘script’);
script.src = ‘path/to/jquery.min.js’;
document.getElementsByTagName(‘head’)[0].appendChild(script); // load jQuery
}
};
ASP.NET MVC based CMS

N2 Open Source ASP.NET CMS http://n2cms.com/

Oxite http://www.visitmix.com/Lab/Oxite

Hydrogen CMS http://www.hydrogencms.net/Home.aspx From: http://sitestree.com/?p=2025
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-05-17 21:23:12

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada