Wednesday, June 17, 2015

Asp.net, C#: Connect to Azure Storage Table and fetch/filter all the records in specific table

Azure Storage Table
Create a project(console) in Visual Studio.
Install Cloud Storage package from nugget package(steps below).
Step 1: Right click on project in solution explorer


Step 2: Click on Online Packages > Search for Azure > Install Windows Azure Storage:






Create a class for every Table in Storage. Here in following example, I am creating a class for table. Class name could be anything. But column names must be same as columns in storage table.

using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureStorage.Classes
{
    class TableName1 : TableEntity
    {
        public TableName1(string pKey, string rKey)
        {
            this.PartitionKey = pKey;
            this.RowKey = rKey;
        }

        public TableName1() { }

        public string CustomCloumn1 { get; set; }
        public int CustomCloumn2 { get; set; }
        public int CustomCloumn3 { get; set; }
        public int CustomCloumnN { get; set; }
        public DateTime TIMESTAMP { get; set; }
    }
}

PS: we need to inherit the class from TableEntity class. This class resides in following name space:
Microsoft.WindowsAzure.Storage.Table;
Now we need to hit storage table in Azure and get data from there. For this we need StorageAccountName and StorageAccountKey. And the new need to create a connection string key in app.config file. You may create a connection string via some tool or simply copy paste below key in appSettings.

  <appSettings>
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAzureAccountName;AccountKey=YourAzureAccountKey" />
  </appSettings>



Following code will fetch all the records in storage table TableName1. I have used a tick here. I have specified partition key not equal nitin. Which will get all the records (as there is not partition key nitin.). similarly you may choose desired operation and value to match. Partition key and row keys are default columns. In my next post I will explain how can we specific custom filter expressions,  and filter on multiple user defined columns, and filter on various types of data(like date).

static void Main(string[] args)
{
    int counter = 0;
    StringBuilder sb = new StringBuilder();

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

    // Create the CloudTable object that represents the "TableName1" table.
    CloudTable table = tableClient.GetTableReference("TableName1");

    // Construct the query operation for all customer entities where PartitionKey!="nitin".
    TableQuery<TableName1> query = new TableQuery<TableName1>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.NotEqual, "nitin"));

    // Print the fields for each customer.
    foreach (TableName1 entity in table.ExecuteQuery(query))
    {
        if (counter > 500) break;
        counter++;
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
        sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
    }
    Console.WriteLine(counter);
    Console.WriteLine(sb.ToString());

    Console.WriteLine("Done... press a key to end.");
    Console.ReadKey();
}

Congrats All Done. Run and check. Let me know if you face any issue.



C#, asp.net - The name 'ConfigurationManager' does not exist in the current context

This is due to System.Configuration.dll, Reference to this dll is missing in project and you are using this class(which is in System.Configuration.dll). To resolve this you need to add reference of System.Configuration.dll.

Right click References in Solution Explorer.
Add Reference.
In Assemblies > Framework, browse to System.Configuration.dll.
Check the checkbox.

Click OK.

Thursday, June 11, 2015

407 proxy authentication required Azure Storage Table

Hi Friends,

This is not an error. I am facing this error since last 2 days. Initially I searched over the internet for some resolution, but later I noticed that I am facing this error after making very small changes in code.
I resolved this error by just Cleaning, Build and Rebuilding solution.

Search text in SQL Stored Procedure



#Declare an input text as given below.

DECLARE@SEARCH_TEXT VARCHAR(255)
SET@SEARCH_TEXT= 'nitindhiman@gmail.com’

#Query for Search Text in Stored Procedure

SELECT DISTINCT
OBJ.NAME AS OBJECT_NAME,
OBJ.TYPE_DESC
FROM SYS.SQL_MODULES MOD
INNER JOIN SYS.OBJECTS OBJ ON MOD.OBJECT_ID = OBJ.OBJECT_ID
WHERE MOD.DEFINITION LIKE '%'+@SEARCH_TEXT+'%'
ORDER BY TYPE_DESC


Tuesday, April 14, 2015

class does not contain a property with the name

Values in repeater are not displaying either via <%#Eval("Display") %>or <%#DataBinder.Eval(Container.DataItem, "Display")%>. Following is the class schema.


public class SwitchUser
{
    public string EmailId;
    public string Password;
    public string Display;

    public SwitchUser()
    {
    }

    public SwitchUser(string emailId, string password, string display)
    {
        EmailId = emailId;
        Password = password;
        Display = display;
    }

}
This is due to following public properties of class:
    public string EmailId;
    public string Password;
    public string Display;

Currently these are fields but not properties. Add getter and setter to these fields to make them properties. Following is the method to create properties.
    public string EmailId{ get; set; }
    public string Password{ get; set; }
    public string Display { get; set; }

This will resolve the error.