Angelos Petropoulos' WebLog

Thoughts occasionally worth reading

Error: 40 - Could not open a connection to SQL Server

clock September 27, 2007 15:12 by author angelosp

When you fire up your application and you receive this message, you follow some standard steps and try to find the answer to the following questions

 

  1. Is my SQL Server up and running?
  2. Can I connect to the box SQL Server is hosted?
  3. Is my connection string correct?
  4. Am I using the correct IP address and instance name?
  5. Is SQL Server accepting the type of connection I want (Named Pipes, TCP/IP, etc)?
  6. Is my firewall blocking either the outgoing connection request or the inbound one?
  7. Is my router not forwarding the connection request to the correct machine and port?

 

Somewhere between #1 and #7 you will find yourself answering "no", fixing the problem so the answer becomes "yes" and the annoying error message "Error: 40 - Could not open a connection to SQL Server" goes away.

So, what happens when you have answered "yes" to all the questions and the stupid message is still there? In my case it was because I answered "yes" to question #5 without it being true. You see, I assumed that if the SQL Server Configuration Manager says that TCP/UP is enabled (see screenshot below) then my SQL Server is ready to accept TCP/IP connections.

Silly me! When the machine has more than one interface (i.e. IP address that SQL Server can listen to for incoming connections), each one has to be enabled separately.

You will need to enable at least one IP address before SQL Server starts accepts TCP/IP connections. Make sure you enable the one you are trying to connect through :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


CodeFormatter - Format code, XML, etc. with syntax highlighting in HTML

clock September 26, 2007 15:29 by author angelosp

For ages I've been struggling with pasting code into my blog and having it look readable, let alone nice. I searched around the web for a solution and found a couple of Visual Studio add-ins (CopySourceAsHTML, Output Source Files in HTML Format), but they were not what I was looking for. Having a VS add-in to do this is cool, but it is not always the best solution.

  1. I am not always on my personal development machine and I may not have the add-in or even Visual Studio all together installed.
  2. I don't only post C# code. I also post SQL, XML, HTML, etc. It would be nice if the solution could cater for all of them.

After searching a bit more online I came across this cool solution, which is an online source code formatter. It can do C#, VB, SQL, XML, HTML and even MSH! It also has some nice little extras, such as alternate row highlighting and line numbers.

Unfortunately it didn't play nice with my blog. The output makes use of the "<pre>" HTML tag, which didn't agree with the engine my blog uses, so quite simply I couldn't use it. More searching online didn't yield any better alternatives, so I decided to take the plunge, crank open the code and mess with it until it works. While I was there I took the opportunity to fix a couple of CSS bugs related to line numbers and alternate row highlighting.

Here is the result.

I've made it available to everyone, so enjoy. You will notice that the biggest change is that by default the "<pre>" tag is not used in the HTML output, but the extra option allows you to turn that functionality back on.

Many thanks to manoli for making the source code available.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


GridView - Exception thrown when rendering the control about missing property

clock September 25, 2007 12:34 by author angelosp

I'm not big on ASP.NET, but I've been working on an ASP.NET 2.0 project lately and much to my surprised I faced an issue when combining inheritance and the GridView control.

The GridView control, which was introduced in ASP.NET 2.0, is the preferred grid control as it offers various improvements over its predecessor the DataGrid. One of the improvements is allowing for easier data binding with less code and mark-up.

My requirements were quite simple, load a list of business entities from the database and display them on the page in a tabular format. I decided to use a generic list to hold the business entities and render them on the page using the GridView control. Well that didn't work. The data binding of the GridView worked correctly, but during the rendering of the control an exception would be thrown complaining that a property of my business entity that was bound to a column in the GridView was missing. To be precise the error message is "A field or property with the name 'XXXX' was not found on the selected data source". I doubled checked to make sure I didn't have any spelling mistakes anywhere and that I was indeed using properties that existed in my business entities.

I was confused at first, but then I noticed that it was not the first column being rendered that was failing. I started thinking what was different between the first columns that were rendered successfully and the one that was failing and I realised that the issue was inheritance. All of my business entities inherited from a base class that contained certain properties (e.g. ID). The columns I was trying to render were bound to a mixture of properties, some defined in the base class and others defined in the child class.

The following example illustrates this clearly.

My business entities

/// <summary>
/// Base class for all business entities.
/// </summary>
public abstract class BusinessEntityBase
{
     /// <summary>
     /// The ID of the business entity
     /// that is the primary key in the DB.
     /// </summary>
     public int ID;
}
 
/// <summary>
/// A business entity representing a customer.
/// </summary>
public class Customer : BusinessEntityBase
{
     /// <summary>
     /// The customer's name.
     /// </summary>
     public string Name;
}

My data binding code

protected void Page_Load(object sender, EventArgs e)
{
     // Create a list of customers
     List<Customer> customers = new List<Customer>();
 
     Customer customerA = new Customer();
     customerA.Name = "CustomerA";
     customers.Add(customerA);
 
     Customer customerB = new Customer();
     customerB.Name = "CustomerB";
     customers.Add(customerB);
 
     // Bind the list of customers to the gridview
     this.GridView1.DataSource = customers;
     this.GridView1.DataBind();
}

My mark-up for the GridView control

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
     <Columns>
         <asp:BoundField DataField="ID" HeaderText="Customer ID" />
         <asp:BoundField DataField="Name" HeaderText="Customer Name" />
     </Columns>
</asp:GridView>

During rendering the following exception is thrown

 


So how do you work around this issue? I tried searching online, but I didn't come up with anything. I will admit that I didn't do a very thorough investigation, but I just needed to get on with the work so I ditched "BoundField" and went with "TemplateField" instead. By changing the mark-up for the GridView to the following the issue was resolved and the control rendered successfully.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
     <Columns>
         <asp:TemplateField HeaderText="Customer ID">
             <ItemTemplate>
                 <%# ((GridViewTest.Customer)Container.DataItem).ID%>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Customer Name">
             <ItemTemplate>
                 <%# ((GridViewTest.Customer)Container.DataItem).Name%>
             </ItemTemplate>
         </asp:TemplateField>
     </Columns>
</asp:GridView>

I'd still like to know why this issue occurs ... 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Optimize Managed Code For Multi-Core Machines

clock September 19, 2007 17:32 by author angelosp

The UK MSDN Flash Newsletter this week pointed me to this great MSDN article regarding managed code and multi-core machines. We are all very excited about having multi-core CPUs and our shiny new laptops now being able to "do more than one thing at a time", but unless we start writing applications to take advantage of this we will not see our application's performance increase during CPU intensive tasks.

This article is definitely worth a read if you are interested in multi-threading or performance.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


.NET Reflector Add-In - SilverlightBrowser

clock September 18, 2007 11:42 by author angelosp

SilverlightBrowser

One of the nice advantages to Silverlight is that you can view the source of a site to see how things work, but with Silverlight 1.1 Alpha that process became more complex. While you can still fish through the JavaScript and Xaml to find the .NET assembly(dll) that is doing all the logic it takes a lot more time.

Thus I decided to write a plugin for Lutz Roeder's Reflector that takes a URL to a Silverlight page and finds the assembly for that page. It also loads up the JavaScript and root Xaml for the page.

Using the Plugin
Once it is installed hit "CRTL+U" or "File->Open Silverlight URL".
Paste a link to your favorite Silverlight site into the textbox and hit go.

This is very cool.

Get it here

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Subscribe

Syndicate
AddThis Feed Button
AddThis Social Bookmark Button

Search

Calendar

<<  August 2008  >>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

Archive

Tags

Categories


Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in