jrz
jrz

Reputation: 1397

connection to Azure table storage fails

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?

Upvotes: 1

Views: 1936

Answers (2)

jrz
jrz

Reputation: 1397

I changed

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

to

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

And it seems to work perfectly.

Upvotes: 1

Itay Podhajcer
Itay Podhajcer

Reputation: 2654

I think you should change:

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

To:

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

Hope it helps!

Upvotes: 0

Related Questions