Author Archives: Rob M

Visual Studio 2013 – project builds but reports The type or namespace name could not be found

Full error:

The type or namespace name ‘test’ could not be found (are you missing a using directive or an assembly reference?)

Namespace exists, all references are in place, but the error persists.

Few step to resolve it:

  • close the Visual Studio
  • delete *.suo files from the project
  • re-open the solution
  • rebuild the solution

Source Stack Overlow

ASP.NET C# Send email in background thread

System.Threading.Thread thread = new System.Threading.Thread(delegate()
{
    SMTPSend(message);
});

thread.IsBackground = true;
thread.Start();

private static void SMTPSend(MailMessage message)
{
    try
        {
            SmtpClient smtp = new SmtpClient();
            ...

On the presentation layer you do not need any AJAX or JavaScript calling code from the backend
Just plain ASP.NET button

<asp:Button ID="btnSend" runat="server" Text="Send Email" onclick="btnSend_Click" />

Code behind

protected void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        Notifications.Send(email);
    }
    catch (Exception ex)
    {
        litResult.Text = ex.ToString();
    }
}

AWS EC2 – Get running instances only

AWS SDK has Console Sample project where you can see code populating number of your running Instances.

DescribeInstancesResponse ec2Response = ec2.DescribeInstances(request);
int numInstances = 0;
numInstances = ec2Response.Reservations.Count;
Console.WriteLine("You have " + numInstances + " Amazon EC2 instance(s) running.");

What you actually get is number of all your Instances, no matter if they are running or not.
If you want to get only running ones, query Reservations collection filtering by their State

DescribeInstancesRequest instanceRequest = new DescribeInstancesRequest();
List<Reservation> reservations = ec2.DescribeInstances(instanceRequest).Reservations;
List<Instance> runningInstances = reservations.Where(r => r.Instances.Any(i => i.State.Name == "running")).Select(r => r.Instances[0]).ToList();
numInstances = runningInstances.Count;

SQL Server For Each Row Next

SELECT 
    RowNum = ROW_NUMBER() OVER(ORDER BY CustomerID)
    ,*
INTO #Customers
FROM SalesLT.Customer

DECLARE @MaxRownum INT
SET @MaxRownum = (SELECT MAX(RowNum) FROM #Customers)

DECLARE @Iter INT
SET @Iter = (SELECT MIN(RowNum) FROM #Customers)

WHILE @Iter &lt;= @MaxRownum
BEGIN
    SELECT *
    FROM #Customers
    WHERE RowNum = @Iter
    
    -- run your operation here
    
    SET @Iter = @Iter + 1
END

DROP TABLE #Customers

via SQL SERVER PLANET

Error 0xc004706b: failed validation and returned validation status “VS_ISBROKEN”. (SQL Server Import and Export Wizard)

It happens during exporting the data, while source table has identity columns and destination one as well.

Step 1: Edit Mappings

01

Step 2: Check Enable identity insert

02

The reason why I got this error was because my destination table had the IDENTITY property set on ID column, which was not allowing do any inserts using the export wizard. This is because identity column values are automatically generated.

Enabling the Enable Identity Insert option allows values to be inserted in the identity field and desired values are moved from source database to destination table.

C# check if a string contains an element from a collection

List of strings

if(listOfStrings.Any(s => myString.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0))
{
    result = listOfStrings[myString];
}

Dictionary, accessing string as a Key

if(dictionaryOfStrings.Keys.Any(c => myString.IndexOf(c, StringComparison.OrdinalIgnoreCase) > 0))
{
    result = dictionaryOfStrings[myString];
}

Dictionary, accessing string as a Value

if(dictionaryOfStrings.Values.Any(c => myString.IndexOf(c, StringComparison.OrdinalIgnoreCase) > 0))
{
    foreach (KeyValuePair<string, string> pair in dictionaryOfStrings)
    {
        if (myString.Equals(pair.Value))
        {
            result = pair.Key;
        } 
    }
}