Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Friday, July 1, 2011

How To Format DateTime In GridView Boundfield And Template Columns

In many situations we will bind date and currency values to gridivew at that time our gridview is something like this

In some situations we don’t want to show complete date and time we need to display only date or only time or we need to display date in different formats and we need to change the currency format display also. To display date with different formats in gridview we need to set DataFormatString and HtmlEncodeproperties.

Here is the list of date formats for gridview.

Character
Description
Date Format
Example
Y or y
Month and year
MMMM,YYYY
May 2011
d
Short Date
MM/dd/yyyy
15/1/2011
D
Long Date
dddd,MMMM dd,yyyy
Sunday, May 15, 2011
f
Full date and time without secs
dddd,MMMM dd,yyyy HH:mm
Sunday, May 15, 2011 4:23PM
F
Full date
dddd,MMMM dd,yyyy HH:mm:ss
Sunday, May 15, 2011 4:23:45PM
g
Date time without sec
MM/dd/yyyy HH:mm
5/15/2011 4.25 PM
G
Full date
MM/dd/yyyy HH:mm:ss
5/15/2011 4.25:35 PM
M or m
Month and day
MMMM dd
May 15
R or r
Full date with GMT
ddd,dd MMMM yyyy HH:mm:ss GMT
Sun,15 May 2011 4.25:35 GMT
s
Sortable Date time
yyyy-MM-ddTHH:mm:ss
2011-05-15T16:33:55
t
Short time
HH:mm
4:56 PM
T
Long Time
HH:mm:ss
4:56:45 PM
u
Sortable DateTime Pattern using universal time
yyyy-MM-ddTHH:mm:ssz
2011-05-15T16:33:55z
U
Long Date
ddd,dd MMMM yyyy HH:mm:ss
Sunday, May 15, 2011 4:54:10 PM
C
Currency Format
$00.00
$10.00
D
Decimal Format
10
10

Now we will implement these formats in our gridview for this first bind the date

values from your database and design your aspx page like this 



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Gridvew Date format</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" 

 AllowPaging="true"AllowSorting="true" AutoGenerateColumns="false">

<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>

<asp:BoundField DataField="Date1" HeaderText="Date1" HtmlEncode="false" 

DataFormatString="{0:s}" />

<asp:BoundField DataField="Date2" HeaderText="Date2" HtmlEncode="false" 

DataFormatString="{0:D}" />

<asp:BoundField DataField="Date3" HeaderText="Date3" HtmlEncode="false" 

DataFormatString="{0:m}" />

<asp:BoundField DataField="Date4" HeaderText="Date4" HtmlEncode="false"

 DataFormatString="{0:d}" />

<asp:BoundField DataField="Total" HeaderText="Total" HtmlEncode="false"

 DataFormatString="{0:C2}" />

</Columns>
</asp:GridView>

<asp:SqlDataSource ID="dsdetails" runat="server" 

SelectCommand="select * from DateFormat"

ConnectionString="<%$ConnectionStrings:dbconnection %>">

</asp:SqlDataSource>

</div>
</form>
</body>
</html>
After that set your database connection in web.config like this


<connectionStrings>

<add name="dbconnection" connectionString="Data Source=SandeepRauniyar;

Integrated Security=true;Initial Catalog=MySampleDB"/>

</connectionStrings >
Demo


If you want to use these date string formats in itemtemplate field we

need to bind fields like this


<asp:TemplateField HeaderText="Date1">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date1","{0:y}") %>'/>
</ItemTemplate>
</asp:TemplateField>
I hope it helps you to format date in gridview






How To Use Multiple Web.Config Files in Asp.Net Application

First create one new web application that contains Default.aspx page and one web.config file. Now right click on your application and select New folder after creation of new folder right click on new folder select Add new item and add one aspx page and one web.config file to this folder that files structure like this





After that write the following code in root folder web.config file appsettings section like this
<appSettings>
<add key="rootfolder" value="Root Folder web.config"/>
<add key="test1" value="Testing the application for root folder web.config"/>
</appSettings >
After that open Default.aspx page in root folder and write the following code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RootFolder web.config</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>
Root Folder values
</b>
</div>
<br />
<div>
AppKey value:
<b>
<asp:Label ID="lbltxt" runat="server" Text="<%$appSettings:rootfolder %>"/>
</b>
<br />
Appkey Test value:
<b>
<asp:Label ID="Label1" runat="server" Text="<%$appSettings:test1 %>"/>
</b>
</div>
</form>
</body>
</html>
Now run your application and check the output for root folder
OUTPUT 


After that open the sub root folder web.config file and write the following code in appsettings section like this
<appSettings>
<add key="subrootfolder" value="Sub Root Folder web.config"/>
<add key="test1" value="Testing the application for sub root folder web.config"/>
</appSettings >
After that open ChildPage.aspx page in sub root folder and write the following code

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>SubRoot Folder web.config</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>
SubRoot Folder values
</b>
</div>
<br />
<div>
AppKey value:
<b>
<asp:Label ID="lbltxt" runat="server" Text="<%$appSettings:subrootfolder %>"/>
</b>
<br />
Appkey Test value:
<b>
<asp:Label ID="Label1" runat="server" Text="<%$appSettings:test1 %>"/>
</b>
</div>
</form>
</body>
</html>
Now run your application and check the output for sub root folder
OUTPUT 
If you observe above outputs Default.aspx page getting values from root folder web.config file and ChildPage.aspx page getting values from subroot folder web.config file.

In this way we can use multiple web.config files in asp.net web application. I hope it helps you

Thursday, June 23, 2011

How to import Contacts from GMAIL using ASP.NET and C#

Step-1:  Download Google data API setup from the specified URL 

Or you can directly download with the following
 
That Set Up will installs set of Google Data dll’s (Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll) into client installed Machine, you should collect that dll’s for your program.

Step-2:  Design our aspx page (Default.aspx) by using with the following UI as simple scenario.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>IMport Gmail Contacts</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
UserName</td>
<td>
<asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password</td>
<td>
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gvmails" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Step-3:  Add those downloaded google dll’s as reference to your website in visual studio->Solution Explorer ->Right Click-> Click on Add Reference….->Browse ->Get dll’s from Installed Location->Press OK.

Step-4:
A) Add namespace these namespace to your code behind

using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
B) After that write following code in code behind


public static DataSet GetGmailContacts(string App_Name, string Uname, string UPassword)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn C2 = new DataColumn();
C2.DataType = Type.GetType("System.String");
C2.ColumnName = "EmailID";
dt.Columns.Add(C2);
RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
foreach (Contact t in f.Entries)
{
foreach (EMail email in t.Emails)
{
DataRow dr1 = dt.NewRow();
dr1["EmailID"] = email.Address.ToString();
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);
gvmails.DataSource = ds;
gvmails.DataBind();
}
 
DEMO