Using Visual Studio with Git Hub

This is one of those great examples of writing a blog post to yourself to remind you how to do something.

If your using Visual Studio 2012 then to add Git support you will need the Visual Studio Tools for Git plugin created by Microsoft’s TFS Power Tools Team (http://visualstudiogallery.msdn.microsoft.com/abafc7d6-dcaa-40f4-8a5e-d6724bdb980c), if your using a later version of Visual Studio then it’s already built in.

If you’re thinking in of using Git Hub as your source control provider then the most basic thing you’re going to need to know is how do you get the Git plugin to link up to GitHub. Here’s a couple of different methods;

Creating a Project in Git Hub

Click New Repository. Enter a new, Select Public or Private and click Create new Repository.

A new repository will be created in Git Hub

Clone the Project in Visual Studio

Now the project has been created in Git Hub you will need to clone it to your machine so that you can start adding files and sync then back.

Open a new instance of Visual Studio and do the following:

  1. Open the Team Explorer window
  2. Click the connect button
  3. In the list of Local Git Repositories click clone
  4. In the URL box enter the HTTP URL from Git Hub
  5. The second box should auto populate with a location on your hard disk
  6. Click Clone

Creating a Repository on Git Hub using the Git Hub app

Rather than creating the Git Hub repo through the GitHub website you can use their app. Once you’ve got the app installed and logged in do the following:

  1. Select the GitHub account you want to add the repo to on the left
  2. Click Create button at the top
  3. Enter a name and click create
  4. The repository will be created in GitHub and automatically sync with the folder on your machine

Adding an existing Repository to Visual Studio

If you already have a repository cloned on your machine but it’s not showing in Team Explorer you can add it by clicking on Add

  1. Click the Connect button to view the list of local repositories
  2. Click add an enter the path to the repository on your hard drive
  3. Click Add
Advertisements

LINQ to SQL Connection Strings

LINQ to SQL is great but like all great things at some point it does something that you don’t expect and gives you a headache. An example of this happened to me this week with the differences between how connection strings are handeled when you LINQ to SQL model is in a class library rather than a Website or Web Application.

What makes this issue particulalry annoying is that it only appears when you try and change the database server that your code is looking at which could end up being when it’s going live or moving to a staging server.

So we all know about connection strings, their quite simple and you just store them in your web.config file, which is how LINQ to SQL works when your using them in a Website. But as soon as you move them to a class library things change. First your connection string name is no longer that simple name you gave it e.g. ConnectionString, now it is prefixed with the namespace which is annoying but not the end of the world. Second discovery though is no matter what you do, it just doesn’t seem to pick up the connection string from the web.config file. Reason being your origional connection string has now compiled itself in the class library’s dll and that is what it is using.

The Solution

Depending when you discovered this the solution is not to bad as you either have a lot of code to change or only a small amount. You can always pass a connection string to the constructor when you are createing an instance of the data context e.g.

DataClasses1DataContext da = new DataClasses1DataContext(connectionstring);

You can also set the connection string on your LINQ to SQL model to be blank, this will remove the default contructor and force you to pass a connection string. This way you web application has the choice of what connection string to use and you can keep re-using your class library in different projects.

Visual Studio 2010 – Snippet Support

This week I have been playing around with the Beta 1 of Visual Studio 2010 and one thing I have to say I like is the new Snippet Support. We’re all used to using it’s InteliSense support allowing us to only type <aso:Bu and then hit space if we wanted to add a .NET button control to the page, but Snippets takes that a step further. Have a look at this example of adding a MultiView control to the page.

Adding A MultiView Control

The normal method would be to type <asp:Multi and then hit space to complete the word, followed by adding an ID, runat etc. All of which would be made easier with the current InteliSense support. Now though you can just type <mu and the Multiview snippet will be highlighted.

Hit tab and the word will be completed as below.

Hit tab again though and all the other stuff you were about to type is entered for you.

An auto ID is given, the runat and activeviewindex properties are also written for you along with the first view automatically added.

Snippet support isn’t just limited to .NET controls either, it will also work with HTML markup and JavaScipt.

HTML Example

In this HTML example of an “A” tag you can see as simple as an “A” tag may be, snippets can save you time even here by automatically writing the whole thing, and then recognising that you will want to change the values of the two sections and thus makes that even easier by then letting you tab between the two sections.

So there you have it, we can now all wonder how we ever survived having to write the first couple of letters of each word, when really all we ever needed to do was write the first couple of letters of the main tag and have the rest written for us.