TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Azure Document DB Retrieve data show in Gridview
Naren Kumar Kumaresan
Jun 08
2015
Code
2.2
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Step 1:
Add web config.
<appSettings>
<add key=
"EndPointUrl"
value=
"your end url"
/>
<add key=
"AuthorizationKey"
value=
" your AuthorizationKey"
/>
<add key=
"DatabaseId"
value=
"your database id"
/>
<add key=
"CollectionId"
value=
"your collection Name"
/>
</appSettings>
Step 2:
Add one.aspx page.
<div>
<asp:gridview ID=
"gv"
runat=
"server"
></asp:gridview>
</div>
Step 3:
Codebehind page .cs.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Configuration;
using
System.Data.SqlClient;
using
System.Data;
using
System.IO;
// Add DocumentDB references
using
Microsoft.Azure.Documents;
using
Microsoft.Azure.Documents.Client;
using
Microsoft.Azure.Documents.Linq;
using
System.Web.Services;
// Add Newtonsoft.Json references
using
Newtonsoft.Json;
public
partial
class
_Default: System.Web.UI.Page
{
string
databaseId = ConfigurationManager.AppSettings[
"DatabaseId"
];
string
collectionId = ConfigurationManager.AppSettings[
"CollectionId"
];
string
endpointUrl = ConfigurationManager.AppSettings[
"EndPointUrl"
];
string
authorizationKey = ConfigurationManager.AppSettings[
"AuthorizationKey"
];
protected
void
Page_Load(
object
sender, EventArgs e)
{
GetStartedDemo();
}
public
class
RootObject
{
public
string
ArticleId
{
get
;
set
;
}
public
string
HeadLine
{
get
;
set
;
}
public
string
OtherHeadline
{
get
;
set
;
}
public
string
Abstract
{
get
;
set
;
}
public
string
AuthorID
{
get
;
set
;
}
}
public
async
void
GetStartedDemo()
{
// Make sure to call client.Dispose() once you've finished all DocumentDB interactions
// Create a new instance of the DocumentClient
var client =
new
DocumentClient(
new
Uri(endpointUrl), authorizationKey);
// Check to verify a database with the id=FamilyRegistry does not exist
// Database database = client.CreateDatabaseQuery().Where(db => db.Id == "Yourdatabaseid").AsEnumerable().FirstOrDefault();
Database database = client.CreateDatabaseQuery().Where(db = > db.Id ==
"Yourdatabaseid"
).AsEnumerable().FirstOrDefault();
// Check to verify a document collection with the id=FamilyCollection does not exist
DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c = > c.Id ==
"your collection name"
).AsEnumerable().FirstOrDefault();
var families = client.CreateDocumentQuery(documentCollection.DocumentsLink,
"SELECT * FROM Collectionname"
);
string
a = t.ArticleId;
DataTable dt =
new
DataTable();
dt.Columns.AddRange(
new
DataColumn[3]
{
new
DataColumn(
"ArticleId"
,
typeof
(
string
)),
new
DataColumn(
"HeadLine"
,
typeof
(
string
)),
new
DataColumn(
"Abstract"
,
typeof
(
string
))
});
foreach
(var family
in
families)
{
string
s = Convert.ToString(family);
var t = JsonConvert.DeserializeObject < RootObject > (s);
dt.Rows.Add(t.ArticleId, t.HeadLine, t.Abstract);
}
gv.DataSource = dt;
gv.DataBind();
}
}
Gridview
Azure
json