Blog Archives

ASP.NET: Text data from database table gets encoded twice

I had a table with data stored in a UTF-8 format. Such data gets encoded twice when it is displayed on ASP.NET page, for example for “’” I see something like this “’”.

To solve this problem I added handler to translate the table data:

text = Encoding.UTF8.GetString( System.Text.Encoding.Default.GetBytes(text) );

Getting custom data for ItemTemplate within asp:Repeater

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="NewsDataSource">
  <HeaderTemplate>
    <table style="border-collapse: collapse;" bgcolor="#ffffee" border="0" cellpadding="2">
      <tbody><tr><td>
  </HeaderTemplate>
  <ItemTemplate>
      <p class="Pbullets" dir="ltr">
      <img src="img/bullet<%# getBulletImage(Container.DataItem) %>.gif" border="0"> <a href="page.asp" class="bullets" /><%# Eval("Title") %></a></p>
  </ItemTemplate>
  <FooterTemplate>
      </td></tr></tbody>
    </table>
  </FooterTemplate>
</asp:Repeater>
protected string getBulletImage(object dataItem)
{
  DataRowView row = dataItem as DataRowView;
  switch( int.Parse(row["field"].ToString()) )
  {
  case 1:
    return "A";
  case 2:
    return "B";
  case 3:
    return "C";
  }
  return "";
}

Getting user’s email address from ASP.NET

Today I was asked to use a real email ID instead of user login id. Apparently some people have a different login ID and email alias. So I decided to try to use a Request object, but I could not find the way to access required information. So I decided to try company’s Directory Service. After searching I found following solution:

string getEmail( string userName )
{
  // Set the correct format for the AD query and filter
  string ldapEntry = "LDAP://" + "<domain name>";
  string loginName = "<domain login>";
  string loginPassword = "<domain password>";SearchResult result = null;

  using (DirectoryEntry root = new DirectoryEntry(ldapEntry, loginName, loginPassword))
  {
    using (DirectorySearcher searcher = new DirectorySearcher(root))
    {
      try
      {
        searcher.Filter = "(SAMAccountName="+userName+")";
        searcher.PropertiesToLoad.Add("mail");
        SearchResultCollection results = searcher.FindAll();
        result = (results.Count != 0) ? results[0] : null;
      }
      catch (Exception ex)
      {
        return "<not found>";;
      }
    }
  }
  return result.Properties["mail"][0] as string;
}

Interestingly, I have to use user id and password to access DS. So I used dummy and/or very restricted user id and password just to gain access to DS.

WP Like Button Plugin by Free WordPress Templates