Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

10/22/2016

Forest and Trees Problem: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

 

A "Can't see the tree for the forest" problem.


image_thumb[11]
There's an old phrase, "Can't see the forest for the trees", that in reverse, "Can't see the tree for the forest", applies to a recent "demo fail" of mine.

During a break in a C# class I was delivering last week I typed up a little demo for accessing SQL Server, and got an error. A quick read of the error said that it couldn't find the server, and it hinted at a protocol error.  

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

    image_thumb[12]

 

Seeing the big "Named Pipes" tree standing there… I Binged and Googled and found all kinds of stuff… that did not help.

While the answer to the problem was clearly stated in the message above, "Verify the instance name is correct", I didn't see it as I was looking at all of the other "trees" in that little code forest. The "tree" that I needed to deal with in this case was a C# beginner error of putting a backslash in a string. (I copy and pasted it without looking at it!) The back slash is an escape character to flag the next character as a special code. In this case "\v" is the code for a Vertical Tab. So, I had created a connection string looking for a server named "(localdb)VerticalTab11.0".

image

What made this little error a bit painful was that in this class I had mentioned escape characters in C#, and how to deal with them, at least four times! Oh well…

To solve the problem, escape the escape character ("(localdb)\\v11.0") or mark the entire string as a literal string with the At sign ("con.ConnectionString = @"Data Source=(localdb)\v11.0 …").

   image

For a list of the C# escape characters see this MSDN article:
   https://msdn.microsoft.com/en-us/library/h21280bw.aspx

.

12/12/2015

MVC: The controller for path '/' was not found or does not implement IController


Some error messages are no help at all!


While working on a Visual Studio 2013 MVC project I got this error:



When running in Debug mode Visual Studio reports:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code.
Additional information: Execution of the child request failed. Please examine the InnerException for more information.

The inner exception: 
{System.Web.HttpException (0x80004005): The controller for path '/' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance




A web search returns many possibilities for the error, but none that applied to my project. It turns out I had one of those "forest and trees" problems! I had typed Html.Action instead of Html.ActionLink in a Layouts view. In effect I was asking the layouts view to load a view that used the layouts view that then loaded the layouts view that loaded the view that used the layouts view… Yup, an endless loop.


The error message was not of much help and sent me on a wild goose chase. Once I realized what was going on I made one small edit and was back in business.



.

1/07/2015

Still exam procrastinating? Second Shot is back!

 

Time for New Year's resolutions? Or just finish some of last years…

Take any Microsoft Certified Professional (MCP) exam between January 5, 2015, and May 31, 2015. If you don't pass, get a free retake!

All of the Microsoft Certified Solutions Associate (MCSA), Microsoft Certified Solutions Expert (MCSE), Microsoft Certified Solutions Developer (MCSD), and Microsoft Specialist certification exams are eligible. Microsoft Technology Associate (MTA) exams and Microsoft Office Specialist (MOS) exams do not qualify for this promotion.

Details here: https://www.microsoft.com/learning/en-us/second-shot.aspx

 

.

11/18/2014

C# Compare Arrays

 

Sometimes we get so focused on the advanced features of .NET that we (or at least I) forget the basics. The basic in this case is checking to see if two arrays are equal.

int[] a = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
int[] b = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
if (b == a) 
  { Console.WriteLine("a Equals b!"); } 
  else 
  { Console.WriteLine("a NOT Equals b!"); }

The above always returns "a NOT Equals b!".  Yes, I knew that if (a == b) (same as a.Equals(b)) will not work as arrays are objects and "a == b" is testing reference equality, i.e. to see if they are the same object. But I also figured that as .NET has a built in class or method for everything you can think of, there had to be one to do an array comparison. Turns out there is, .SequenceEqual., which is part of LINQ.

if (b.SequenceEqual(a))
  { Console.WriteLine("a SequenceEqual b!"); } 
  else 
  { Console.WriteLine("a NOT SequenceEqual b!"); }

The above works great, but as LINQ is wrapper for other code, there must be some overhead, and it turns out there's a lot! I wrote a quick and dirty CompareArrays method and did a comparison of performance between my crude code and the LINQ SequenceEqual. Here's the results:

image

The manual code was around five times faster! BUT, it took a whole lot longer to write. Unless you are writing games or intense code, developer efficiency and code readability leans towards the LINQ SequenceEqual solution.

Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };
            int[] b = { 1, 7, 4, 6, 2, 8, 2, 8, 4, 6, 2 };

            DateTime d1 = DateTime.Now;
            for (int i = 0; i < 100000; i++)
            {
                b.SequenceEqual(a);
            }

            DateTime d2 = DateTime.Now;
            for (int i = 0; i < 100000; i++)
            {
                CompareArrays(a, b);
            }

            DateTime d3 = DateTime.Now;
            Console.Write("Time for SequenceEqual:    ");
            Console.WriteLine(d2 - d1);
            Console.Write("Time for my CompareArrays: ");
            Console.WriteLine(d3 - d2);

            Console.ReadLine();
        }

        static bool CompareArrays(int[] a, int[] b)
        {
            if (a.Length != b.Length) { return false; }
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i]) { return false; }

            }
            return true;

        }

    }
}

 

When I get really bored I convert CompareArrays into generics…

 

Hey! Look! I wrote about something other than SharePoint or PowerShell!

 

.

9/21/2014

Developer Apprentice Program in .NET

 

Know someone who is unemployed who would make a great programmer?

 

MAX Technical Training is doing something quite interesting!  Again!

image

Dislocated workers who live in Hamilton, Butler, Warren and Clermont counties and are not working may be eligible to take Developer Apprentice training in .NET programming. Good candidates include those with a background in IT who want to update their programming skills or those who enjoy math, problem solving and/or puzzles. Musicians and those who enjoy learning foreign languages tend to make good programmers.

The Hamilton Co. session is now going to happen on September 23.  Warren Co. is September 24, and the program is open to Clermont and Butler Co. folks as well. 

When: September 23, 2014 at 10 am
Where: OhioMeansJobs Hamilton County, 1916 Central Parkway, Cincinnati  45214

When: September 24, 2014 from 9 am – 10 am
Where: OhioMeansJobs Warren County, 300 East Silver St., Lebanon, OH  45036

"Interested in becoming a Developer or know someone who is?"

"Whether you are an employer looking hire a .NET programmer or a Job Seeker looking to build a new career - this is the program to meet both your needs."

"This intense 9 week "boot camp" immerses you in 42 intense full-days of training. To be accepted, you must pass two aptitude tests and a screening interview with the program director." 

"This will be MAX's fourth program. All of our past apprentices who have completed the program are successfully employed in developer related positions with Great American Insurance, Paycor, Western and Southern, or Assurex."

For more information: http://www.maxtrain.com/DynamicPage.aspx?DynamicContentID=166

 

.

12/29/2013

Is VB.Net dead?

 

Just a random thought at the start of a new year… Is VB.Net dead? Deprecated? Just not interesting to Microsoft Learning?

It seems the newer 204** series Microsoft MOC classes only cover C# (and JS / HTML5). I've also been reading that C# is the only language option in most of the newer certification exams.

On the other hand, it looks like Visual Studio is still treating VB as an equal to C#. There are no new VB and C# language features in VS 2013, but VB is still there. The VB team blog says "We are actively working on the next versions of Visual Basic and C#" (*).

What about VBScript? As of Internet Explorer 11, VBScript is considered deprecated. (MSDN article)

This will be entertaining to watch as many of my web and SharePoint class attendees are still working in VB shops!

Mike

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.