C# Google Gmail

April 16th, 2012 admin Comments off

 using System.Net.Mail;

namespace Portal.Infrastructure
{
public class Mail
{
public void sendEmail()
{

string emailAddress = "cory@coryclough.info";
string recipient = "someone-else@coryclough.info";
string password = "seceret1";

MailMessage mail = new MailMessage();
mail.To.Add(recipient);

// mail.To.Add("additional email addresses to send to");
mail.From = new MailAddress(emailAddress);
mail.Subject = "My Subject Line";

string Body = "Hi, this is a test email" +
"using Gmail in C#";
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
(emailAddress, password);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}

Categories: c# Tags:

ASP.NET MVC Where Controller and Action Method Names Stored

April 13th, 2012 admin Comments off

In the View of MVC
ViewContext.RouteData.Values["Controller"]
ViewContext.RouteData.Values["Action"]

In the Controller of MVC
ControllerContext.RouteData.Values["Controller"]
ControllerContext.RouteData.Values["Action"]

From HttpContext
HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString();

Categories: ASP.NET, c# Tags:

c# Regular Expression Object

April 12th, 2012 admin Comments off

using System.Text.RegularExpressions;

string needle = "dog";
string haystack = "dogs are cool";
Regex r = new Regex(needle);
Match m = r.Match(haystack);
if (m.Success)
{
ViewBag.Message = "true";
}
else
{
ViewBag.Message = "false";
}

//true

Categories: ASP.NET, c# Tags:

C# Query Active Directory LDAP

April 11th, 2012 admin Comments off

 In Visual Studio you will likely need to add the System.DirectoryServices reference to your project

the LDAP class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;

namespace Portal.Infrastructure
{
public class LDAP
{
public List<string> SearchName (string filter)
{

DirectoryEntry adFolderObject = new DirectoryEntry();

DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);

adSearcherObject.SearchScope = SearchScope.Subtree;

adSearcherObject.Filter = filter;

List<string> results = new List<string>();

foreach (SearchResult adObject in adSearcherObject.FindAll())
{
results.Add(adObject.Properties["CN"][0].ToString());
}

return results;
}
}
}

Usage

LDAP ldap = new LDAP();
ViewBag.Message = ldap.SearchName("samaccountname=cclough");

Categories: c# Tags:

Enable Windows Authentication IIS Express

April 10th, 2012 admin Comments off

 1. open root configuration file "Libraries\Documents\IISExpress\config\applicationhost.xml

2. find windows authentication and change to "true"
<windowsAuthentication enabled="true">
...
...
</windowsAuthentication>

 

Categories: IIS Tags:

ASP.NET MVC Razor View Ajax

March 27th, 2012 admin Comments off

 Here is an example on using an AJAX request using the ASP.NET MVC Razor view

@Ajax.ActionLink("Delete", "AssetDelete", "Home", new { id = item.ID },
new AjaxOptions
{
UpdateTargetId = item.ID.ToString(),  //the id property of a html element
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
Confirm = "Delete this asset from inventory?"
})

Categories: ASP.NET Tags:

Google Chrome Flash Downloader Plugin

March 23rd, 2012 admin Comments off

 http://download.cnet.com/Flash-Video-Downloader-for-Google-Chrome/3000-33362_4-75327988.html

Categories: Uncategorized Tags:

Microsoft SQL Server Aggregation Queries

March 23rd, 2012 admin Comments off

 1. Group records by employee ID and total Amount

SELECT EmployeeID, Sum(Amount) FROM Sales GROUP BY EmployeeID

2. Count the total records for each employee.  NULL will count as a record

SELECT EmployeeID, Count(Amount) FROM Sales GROUP BY EmployeeID

3. Find the highest Amount per employee

SELECT EmployeeID, Max(Amount) FROM Sales GROUP BY EmployeeID

Categories: Microsoft SQL Server Tags:

Microsoft SQL Server Constraints

March 23rd, 2012 admin Comments off

 1. add constraint to table by adding a primary key called PK_StateList_StateID on StateID

ALTER TABLE StateList ADD CONSTRAINT PK_StateList_StateID PRIMARY KEY CLUSTERED (StateID ASC)

2. add constraint to table to enforce positive value

ALTER TABLE StateList ADD CONSTRAINT CK_StateList_LandMass CHECK(LandMass  >=  0)

Categories: Microsoft SQL Server Tags:

Microsoft SQL Server Subquery

March 23rd, 2012 admin Comments off

 1.  Query all products from Products table that do not appear in the Invoice table because they have never been sold.

SELECT * FROM Products WHERE ProductID NOT IN (SELECT DISTINCT ProductID FROM Invoice)

2. Query all products from our Products table from the supplier Walmart using the Supplier look up table

SELECT * FROM Products WHERE SupplierID = (SELECT SupplierID FROM Supplier WHERE SupplierName = 'Walmart')

Categories: Microsoft SQL Server Tags: