1
votes

I am working on a new web app and I am trying to connect to an existing Azure table storage.

using System.Web.Mvc;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;

namespace WebApplication3.Controllers
{
public class GetEmailAddressesController : Controller
{
   public ActionResult Address()
    {
        string emails = "";

        // Parse the connection string and return a reference to the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("________"));

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        CloudTable table = tableClient.GetTableReference("experimentsEmailAddresses");

        // Construct a table query.
        TableQuery<TableData> query = new TableQuery<TableData>();

        foreach (TableData entity in table.ExecuteQuery(query))
        {
            emails += entity.Email + ";";

        }

        ViewBag.Message = " " + emails;
        return View();
    }

}
} 

The code will compile but when I run it on debug mode I get an error:

System.ArgumentNullException: 'Value cannot be null. Parameter name: connectionString'

i.e. my connection string isn't valid, although I copied it from the Access keys in Azure.

What is the best solution for that?

2
Where are you storing your connection string within the config file?Rob Reagan
under web.config I added <add key="StorageConnectionString" value="___" />I.zv
Specifically, it is under appSettings or connectionStrings?Rob Reagan
it is under appSettingsI.zv
My other suggestion would be to: 1. Step through and make sure that CloudConfigurationManager.GetSetting("________")); is returning what you think it is. 2. Take the connection string returned by your app and attempt to connect through Azure Storage Explorer.Rob Reagan

2 Answers

1
votes

I changed

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

to

CloudStorageAccount storageAccount = cloudStorageAccount.Parse("StorageConnectionString");

And it seems to work perfectly.

0
votes

I think you should change:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("________"));

To:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

Hope it helps!