Posts

Showing posts with the label C#

My Orchard CMS Experience

Image

Google Service Account with Aspnet MVC Behind A Proxy

    Recently I had a fun project to attach google analytics data to an internal site which took longer complete than I thought intial thought but now I could complete in just a day. My biggest issue was finding out the issue was related to the company's proxy. Which I quickly fixed by just added the following snippet at the bottom of this article. For everything else this is just my notes and things I found to help me drill down into the queries for grabbing the data. 

QuestionsHow to get the Access Token for GA? - Credential.RequestAccessTokenAsyncQuerying example - Use Google Analytics API to show information in C# - Stack Overflow
Behind a Company Firewall
    Both errors I encountered were "No connection could be made because the target machine actively refused it 172.217.1.77:443" and “requested service '…' could not be activated” the first time it's accessed".  <system.net>     <defaultProxy useDefaultCredentials="true">       …

Asp.net: Dynamic Sitemap.xml

    This is an article I started to working for the project: rustkickers, which for now I'm putting on hold. What I was trying to do was kill two birds with one stone. If you need a good sitemap to submit to google or to create a "site map" for you users then this would do it. I haven't finished making it but there is where my notes are.

Generating dynamic XML Sitemaps in ASP.NET MVC - Ben Foster
ASP.NET MVC: Get all controllers - Stack Overflow

Resource
http://www.robtennyson.us/post/dynamic-sitemaps-with-aspnet-mvc - Example
http://blog.divergencehosting.com/2009/04/01/dynamic-create-google-sitemap-aspnet/ - Another example
Create a Google Site Map with ASP.NET
ASP.NET MVC: Get all controllers - Stack Overflow
http://msdn.microsoft.com/en-us/library/yy2ykkab(v=vs.100).aspx

Maybe create a atom feed instead of a sitemap.xml?
http://weblogs.asp.net/seanmcalinden/create-rss-and-atom-feeds-using-custom-asp-net-mvc-action-results-and-the-microsoft-syndication-classes

Diving Back into Selenium and Getting Somewhere!

Image
     Recently I've gone back to using Selenium and started to really get comfortable using it. One of the problems that I constantly face when using Selenium is not having good example and looking at Selenium as a whole in whether it is worthwhile. My problems only get work when I have to ask me whether to use the Selenium IDE or the server package.  Which is more scalable to do? Which one is better to give to a team QA that non-technical? Some of these questions can be answered on the Selenium site but some of them depend on your work environment and how much time you want to spend on creating a system of some kind to do all that.     What you really want for all project is something that you can automate across popular browsers not just a single one. Plus avoid doing a dry run every time to correct your UI tests. So the IDE which really a plugin is not going to do that for you but even if I gave it to a QA non-technical team I don't think it would be used effectively. Instea…

NPOI Does Not Support CSV

    I've been using NPOI for sometime now and was recently asked to create a process to output some data in a csv for better support. I was hoping to bring NPOI in the mix but I had to ask myself does NPOI support CSV/TSV?, simply put no.

Alternatives
http://filehelpers.sourceforge.net/
https://github.com/MarcosMeli/FileHelpers

Library Version - Reading/Writing
Project Version - Server/Storage Features included.

And... There just doing it my way. Personally speaking I didn't like jumping into FileHelpers, I felt was much more overhead than what I need so I looked at creating a small snippet of code for myself. Unlike with NPOI, I also didn't like from what I could see was examples that needed a model to be passed in. Either way, I will try to post an update when I have finished that piece of the puzzle.

Resources
Returning in the middle of a using block - A question for the code examples I saw.asp.net - Response Content type as CSV
Back to Basics - Keep it Simple and Develop Yo…

Asp.net MVC: Partial View to handle Multiple Models

    I was working on this idea a few years back and the idea was to make two partial views in an asp.net MVC more scalable. I was looking the down road for something that could handle whatever the object was and display it. What I later discovered thanks to a colleague was that I should just do a reflection of the object but I feel the better longer term solution could be found in third link of resources. Rather than spend anymore time on this idea I simply kept the two partial views and wrote this down as I have bigger plans with this strategy in mind for a project.

ResourcesUsing partial views in ASP.net MVC 4 - Stack OverflowHow to get the list of properties of a class? - Stack OverflowHow to get data to my view that comes from more than one object? - Stack OverflowWhen To Use Templates vs When to Use Partial Views - Stack OverflowASP.NET MVC 3 Partial View Template - Stack Overflow

Setting up QunitJs for Asp.net MVC Debugging

    Using QUnit is important for me on some of my projects that are more front-end driven. So developing, debugging and testing all need to happen in javascript but without being in the final production. One thing I do not want to do is include the QunitJs in my bundles and would serve no purpose because the bundling and minifying does not happen for debugging. Instead what I did was create an extension method and return a bool, like the one shown in the resources.
//Location ../Helpers/IsDebuggingpublic static bool IsDebugging(this HtmlHelper helper) { #if DEBUG return true; #else return false; #endif } //Location ../Views/Shared/_Layout.cshtml//These two files are not apart of any bundled packages.@if(Html.IsReleaseBuild()) { @Scripts.Render("~/Scripts/qunit-1.14.0.js") @Styles.Render("~/Content/qunit-1.14.0.css") }Side Notes
The intellisense will do some weird things with the coloring for code inside the else statement but it will have …

Using Entity Framework and a Storage Procedure with Parameters

    So I was stuck sometime ago on a problem I had where I was using my sql store procedure with the Entity Framework. Eventually found what I needed and the purpose of this article is just to write out a small example. I've come up with multiple ways to achieve the same thing now but this is the most basic form of it.

public IList<KeyValuePair> GetIds(string id) { using (var db = DBFactory.GetDB()) { return db.Database.SqlQuery<KeyValuePair>( "EXEC db.MyDatabase @id", new SqlParameter("id", id)); } }
ResourcesHow to pass parameters to DbSet.SqlQuery Method in Entity FrameworkHow to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5The data reader has more than one field. Multiple fields are not valid for EDM primitive types
KeyValuePair(TKey, TValue) Structure (System.Collections.Generic)

Orchard: Filter out Content Types From Restricted Users.

I want to be very clear I did not write this code but it was provided to me and I was given permission by a colleague of mine to submit it.. To respect his privacy I will not be able to provide his name but on his behalf I did submit the code to help improve the orchard project. To explain it a little bit what the purpose of this code edit, it is to filter out content that is editable but the user does not have the permissions to create them. Normal what Orchard would do is display all creatable content and if a user clicks on a particular content type that is restricted it simply replies to the user that they do not have access to it. However with a lot of CMS projects I have developed it is sometimes better to simply hide what the user can not or should not be able to do. Less questions come up and even less problems are seen from the user's perspective. This is what I consider to be a best practice to provide the user with a good experience.

The Location:\orchard\src\Orchar…

Unauthorized Ajax Handler for Asp.net MVC

Image
Requirements
     I need respond to users that they are unauthorized, and I need it to not return a redirect to them from a ajax call which Asp.net MVC does by default. A even better solution would be that I also do not force users to refresh page they are currently on and instead give them the ability to re-login via ajax on that current screen. This would allow users to save any  information they are currently working on. I should warn the user that it they are no longer logged in and if possible prompt them and within that same prompt allow them a way to login back in all without refreshing or a redirect. For now I'm going to just focus on the first problem which is suppressing Asp.Net's default behavior with unauthorized calls and do this just for Ajax calls.



Solution - Thanks to Joe Harrison for missing pieces! 
//C# code override an Authorization Attribute create a customized one.
protectedvirtualvoid HandleUnauthorizedRequest(AuthorizationContext filterContext)
{ if (fi…

At the Start of My C# Research

This article is a list of resources that I used way back at the start of my career with C#. Since there are a lot of C# tutorials already out I just wanted to write out some of my experiences and topics that I was curious to learn when starting out. The main reason why I wanted to used C# for programming was for Asp.net MVC applications and toward the second year I discovered XNA.
Getting Ready at the Start!
    If you're a Java Developer the using Statement is much like a import and include in C++. Having experience with both languages doesn't hurt and speaking from my own experiences with both languages, C# is probably the easiest compared to all of them. And for multiple reasons, but mostly because of Visual Studio's Intellisense in my opinion. 

C# language itselfRandom resources that I used, for multiple projects.Visual C# ResourcesDelegates Tutorial C# Short Type - Sometimes its just good to compare (Java, C++).Creating Custom Routes : Official Microsoft SiteString …

MVC Excel Email Attachment with NPOI

    The First Question I had after successfully creating a Excel sheet was how do I create an attachment from a stream? A second to be more specific was how do I add an attachment to an email using System.Net.Mail? Ultimately I wanted to have the Excel Exports in C# using NPOI but I started small and thankfully others had already asked and answered these questions. After which was just a matter of knowing What the correct content-type for excel files was?
First Attempt
MailMessage mail = new MailMessage(); HSSFWorkbook workbook = CreateExcel(OrderId); MemoryStream ms = new MemoryStream(workbook.GetBytes()); mail.Attachments.Add(new Attachment(ms, "example.xls", "application/vnd.ms-excel")); _emailService.SendEmail("example@example.com", "Email test", "Testing 123"); Changed it to..MemoryStream ms = new MemoryStream(); workbook.Write(ms); This was based on the QA "NPOI - Sending workbook as ema…

MVC Data Exported to Excel with NPOI

This is an article for just writing an Excel file using NPOI and I want the quickest and easiest way to export a excel file from Asp.net MVC. To keep everything in work fluid motion I will keep the user on the same page and do a forced download within that page. This is a setup that I have seen in multiple times and I think works great in today's modern website designs.

Creates excel files
Leniel Macaferi's blog: Creating Excel spreadsheets .XLS and .XLSX in C#
Create Excel Spreadsheets Using NPOI
var workbook = new HSSFWorkbook(); var ExampleSheet = workbook.CreateSheet("Example Sheet"); var rowIndex = 0; var row = ExampleSheet.CreateRow(rowIndex); row.CreateCell(0).SetCellValue("Example in first cell(0,0)"); rowIndex++; Problems I facedXLS is not the same as CVS which is obvious but truthfully I did not know the difference between the two file types (other than their different extensions). So I asked myself what is the Difference between CSV and XLS files? W…

C# vs. Javascript Foreach

     This article is about a certain style of writing in Javascript that I like use when iterating through an array combined with a foreach which looks something like the following...
var someArray = ["1", "2", "3"]; $.foreach(someArray, function(key, value){ console.log(key + " , " + value); });      Ultimately when I tried to take this same concept over to C# the style did not convert over in the exact same way. I found that the temp variable being used to iterate with did not have any values that tracked where it was in place of the iteration. I found myself asking a similar question in how do you get the index of the current iteration of a foreach loop? To see what I tried you can look at another person's problem where I had attempted in the same manner. Attempting to use the "current" value in the GetEnumerator() sadly does not work.

Solution
     To summarize the answer for this problem the "foreach" uses th…

A Static DropDownList for ASP.Net MVC

Image
     When creating a static dropdowns list in Asp.net MVC, I have a two approaches that I like to commonly use. I know there are many ways to achieve this UI structure and I don't consider either one of the following approaches to be the final solution. However for awhile now I have been using these two approaches and I think they are much simpler and easier to maintain compared to other ways that I have seen. Rather than writing out the HTML select tag and all the options I take advantage of the Html Helper Dropdownlist. I avoid model bind since it is static and I don't want create the list outside of the view. I think this would only create more work to maintain and confusion for other coders if they new to my applications.  I did not come up with either solution completely on my own and I actually forget where I saw them since it did take a while to find these approach in my research.What's important to take away in this article is that if you look carefully you will se…

XNA Resources

DownloadsSource for XNA Dev Kits
Forums:MSDN Forum
Gamedev stackexchange
Library:XNA Game Studio 4.0 Refresh

Advanced Topics
Advanced Xbox 360 Programming
Cross Platform Ability
Currently only project that I know of that allow you to use C# on multiple platforms is MonoGame.
Use Visual Studio 2012How to install XNA
Research from others: 2D Collision Detection MatricesXNA tutorials
XNA Series 1: Collision Detection - Code for Cake


If you into game development you probably at some point want to know how to do 3D programming. A great resource that I use and that shows how to use a camera in multiple ways can be found on dhpoware the have many examples such as first shooter and 3rd person as well as free roam camera examples.

XNA Series 1: Collision Detection - Code for Cake

2D DevelopmentChanging the frequency for the update and draw

NetworkingCreating a Network Session

Finished Car Game
Xbox LIVE Indie Games - racing game (archived)

Tutorial on 3.0 Framework
Tutorial 4: Make a Game in 60 Minutes

Tuto…

What is the Rust Spot?

Image
The Idea     A lot of times When I try to explain an Idea, I find it increasingly difficult to explain the more complex it is. So I will begin to explain it here in a nutshell. I want to create 3D models of my car and all the parts that it is made of. Simple right? Now I won't go into details of why just yet but I will say you have no idea how many parts making up your car. So the complex idea is this, I want to create a website that not only creates a 3D model of all the parts on a car but compares the parts for compatibility with other makes and models. To go further with at by give a complete break down of the car and instruction on installation or remove of parts. Still with me?
Good here is a example of what a normal diagram of a car part...

The Problem     Now from the picture you have a general idea of what your parts look like, but what about the placement of where it is in your car? Maybe your looking at the wrong model? Cars have a number of key differences between mode…