Archive for Gennaio 16th, 2008

DropDownList inside a GridView (or DataGrid)

CodeKeep C# Feed Gennaio 16th, 2008

Description: DropDownList inside a GridView (or DataGrid)

Link: http://www.codekeep.net/snippets/d121e875-2827-462e-9170-608a979e2747.aspx

<asp:GridView ID="gvStates" AutoGenerateColumns="false" runat="server" OnRowCreated="gvStates_RowCreated">
<Columns>
  <asp:BoundField HeaderText="State" DataField="Name" />
  <asp:TemplateField HeaderText="Cities">
    <ItemTemplate>
      <asp:DropDownList ID="ddlCities" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlCities_SelectedIndexChanged">
      </asp:DropDownList>
    </ItemTemplate>
  </asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>


protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        // Create states array and bind it to Grid
        ArrayList states = new ArrayList();
        string[] cities = new string[] { "Portland", "Salem", "Eugene" };
        State state = new State("OR", cities);
        states.Add(state);
        cities = new string[] { "Seattle", "Tacoma", "Olympia" };
        state = new State("WA", cities);
        states.Add(state);
        this.gvStates.DataSource = states;
        this.gvStates.DataBind();
      }
    }
  protected void gvStates_RowCreated(object sender, GridViewRowEventArgs e)
  {
    if (!IsPostBack)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        // Bind drop down to cities
        DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCities");
        ddl.DataSource = ((State)e.Row.DataItem).Cities;
        ddl.DataBind();
      }
    }
  }
  protected void ddlCities_SelectedIndexChanged(object sender, EventArgs e) 
  {
    this.lblCity.Text = ((DropDownList)sender).SelectedValue; 
  }

Get Active Directory Group Members and See if User Belongs to it

CodeKeep C# Feed Gennaio 16th, 2008

Description: This code gets all the members of a group and checks to see if the logged in user is a member of the group.

Link: http://www.codekeep.net/snippets/e48e5c05-ac45-441c-8159-d76031fab886.aspx

    using System.DirectoryServices;
    using System.Collections.Specialized;

    public bool IsMember(string Group)
    {
        StringCollection groupMembers = this.GetGroupMembers(Group);
        foreach (string strMember in groupMembers)
        {
            if ("[NetBios Domain Name]\\" + strMember == HttpContext.Current.User.Identity.Name.ToString())
            {
                return true;
            }
        }
        return false;
    }
    public StringCollection GetGroupMembers(string strGroup)
    {
        StringCollection groupMemebers = new StringCollection();
        try
        {
            DirectoryEntry ent = new DirectoryEntry("LDAP://DC=DomainName,DC=com");
            DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
            SearchResultCollection coll = srch.FindAll();
            foreach (SearchResult rs in coll)
            {
                ResultPropertyCollection resultPropColl = rs.Properties;
                foreach (Object memberColl in resultPropColl["member"])
                {
                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                    object obVal = userProps["sAMAccountName"].Value;
                    if (null != obVal)
                    {
                        groupMemebers.Add(obVal.ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }
        return groupMemebers;
    }

Find Desktop Applications Actual User Settings File Path

CodeKeep C# Feed Gennaio 16th, 2008

Description: Returns the User Settings File Path for your application. Must set a reference to System.Configuration!

Link: http://www.codekeep.net/snippets/3f14368f-c589-4feb-a3f5-9e09f63febb6.aspx

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
 Console.WriteLine("Local user config path: {0}", config.FilePath); 

Extracting Day of week in words from date Parameter

CodeKeep C# Feed Gennaio 16th, 2008

Description: Extracting Day of week in words from date Parameter

Link: http://www.codekeep.net/snippets/0dfb6092-acbb-4d20-98d3-46bdad477676.aspx

string dayofweek; 
dayofweek = Convert.ToDateTime(dr["ApptDate"]).ToString("dddd");

HashTable

CodeKeep C# Feed Gennaio 16th, 2008

Description: using System.Collections;

Link: http://www.codekeep.net/snippets/37231773-b991-4e54-bf3a-2ba6ace4a71d.aspx

E não é necessário dizer que é de 'string' e 'int' 

Exemplo:
--------

Hashtable meuHash = new Hashtable();

meuHash.Add( "numero1?, 1);
meuHash.Add( "numero2?, 2);

SQL Access in Enterprise Library

CodeKeep C# Feed Gennaio 16th, 2008

Description: A Sample on how to use Enterprise Library Data Appilcation Block

Link: http://www.codekeep.net/snippets/68c6e8a7-5a03-4f12-96eb-2aaceb9bbd9c.aspx

        

Database db = DatabaseFactory.CreateDatabase("YourConnectionString");
System.Data.Common.DbCommand dbcommand = db.GetStoredProcCommand("usp_msg_YourStoreProcedure");
db.AddInParameter(dbcommand, "@Nickname", DbType.String, sNickName);
db.AddOutParameter(dbcommand, "@MobilePhoneNumber", DbType.String, 20);
db.ExecuteNonQuery(dbcommand);
return (db.GetParameterValue(dbcommand, "@MobilePhoneNumber") != null && db.GetParameterValue(dbcommand, "@MobilePhoneNumber").ToString().Trim().Length > 0) ? true : false;