Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

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
 
 
 
 

Row Numbers in SQL Query

SELECT row_number() OVER (ORDER BY EmployeeId) AS RowNumber,
    EmployeeID FROM EmployeeMaster

OutPut :    RowNumber   EmployeeID
                       1                A001
                       2                A008
                       3               A012

Monday, June 20, 2011

How to show the tooltip for gridview header columns using asp.net

1: .aspx code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Tooltip for Gridview Header</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" onrowdatabound="gvdetails_RowDataBound">
<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="UserId" HeaderText="UserId" SortExpression="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from UserInformation"/>
</div>
</form>
</body>
</html>
 
2: connection string in web.config
 
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>
 
3: In code behind gridview RowDataBound event write following code to display tooltip for gridview headers.
 
protected void gvdetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This condition is used to check RowType is Header
if(e.Row.RowType==DataControlRowType.Header)
{
for (int i = 0; i < gvdetails.Columns.Count; i++)
{
e.Row.Cells[i].ToolTip = gvdetails.Columns[i].HeaderText;
}
}
}
  
 



 

Open Popup from Codebehind

Two Type of Open Popup from Codebehind

1: 

string strScript = "<script> ";
strScript += "var newWindow = window.open('Rpt_CSViewReport.aspx?EmployeeID=" + e.CommandArgument + "&MNo=" + ddMonth.SelectedValue + "&MName=" + ddMonth.SelectedItem.Text + "&Year=" + ddYear.SelectedValue + "', '_blank','height=450, center:yes, width=600, status=no, resizable= yes, menubar=no, toolbar=no, location=yes, scrollbars=no, status=no');";
strScript += "</script>";
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "strScript", strScript.ToString());


2:

Response.Write("<script language=javascript>window.open('Rpt_CSViewReport.aspx?EmployeeID=" + e.CommandArgument + "', '_blank','height=450, center:yes, width=600, status=no, resizable= yes, menubar=no, toolbar=no, location=yes, scrollbars=no, status=no');</script>");