-2

I wrote a c# function to populate a ext.net store. It works fine in one application, but the same code does not work in another. I am getting a System.NullReferenceException on line 26. This is line 26:

MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Here is my c# function:

protected void fillStore(Ext.Net.Store store, string query)
{
    SqlDataReader MyReader;
    SqlConnection MyConnection = new SqlConnection();
    MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

    SqlCommand fillCommand = new SqlCommand();
    fillCommand.CommandText = "select id, name from b2b_group";
    fillCommand.CommandType = CommandType.Text;
    fillCommand.Connection = MyConnection;

    fillCommand.Connection.Open();
    MyReader = fillCommand.ExecuteReader(CommandBehavior.CloseConnection);

    store.DataSource = MyReader;
    store.DataBind();

    fillCommand.Dispose();
    MyConnection.Dispose();
}

For simplification, I replaces the query string that would be passed through by a hard-coded one of "select id, name from b2b_group".

I can not seem to figure out why it is giving a nullReferenceException, especially seeing as I have the same code working in another project.

I know that there is some little thing I am overseeing, could anyone spot it?

Thank you so much!

starvator
  • 989
  • 1
  • 11
  • 26
  • 2
    Obviously it can't find your connectionstring. Have you verified this in your debugger? Show the value of `MyConnection.ConnectionString`. Show how your XML is defined. – Jeroen Vannevel Oct 06 '14 at 14:27
  • Well it seems to me that `ConnectionStrings["MyConnectionString"]` is probably null. Are you sure you added it to the config for this new project? – pquest Oct 06 '14 at 14:28
  • That's it! I forgot my connection String -.- I knew I made a stupid mistake... – starvator Oct 06 '14 at 14:28
  • @JeroenVannevel I read that, but yes, agreed, it is a duplicate. – starvator Oct 06 '14 at 14:30

1 Answers1

5

The connection string 'MyConnectionString' is not present in your configuration file. Check connectionStrings section.

Fabio
  • 1,890
  • 1
  • 15
  • 19