Sunday, June 2, 2013

Could not establish trust relationship for the SSL/TLS secure channel with authority PC1. (C#)

Overriding errors for trust relationship failures by trusting all certificates

While developing an application, I needed to browse to a url to read a file. Accessing the server url threw a error. So I added a method to trust all certificates from the url before I called the url.. That resolved the issue I was facing.


Code Snippet

Added this code before calling the server url :

// Trust all certificates

System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) => true);
   

Thursday, May 30, 2013

What is a Self join in SQL? Explain with examples. (SQL)

Self join is just like any other join, except that two instances of the same table will be joined in the query.
Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join.
CREATE TABLE emp
(
empid int,
mgrid int,
empname char(10)
)

INSERT emp SELECT 1,2,'Vyas'
INSERT emp SELECT 2,3,'Mohan'
INSERT emp SELECT 3,NULL,'Shobha'
INSERT emp SELECT 4,2,'Sridhar'
INSERT emp SELECT 5,2,'Sourabh'

//Query which will return employees who have managers

SELECT t1.empname [Employee], t2.empname [Manager]
FROM emp t1, emp t2
WHERE t1.mgrid = t2.empid

//Query using a LEFT OUTER JOIN that returns the employees without managers (super bosses)

SELECT t1.empname [Employee], COALESCE (t2.empname, 'No Manager') [Manager]
FROM emp t1
LEFT OUTER JOIN emp t2
ON t1.mgrid =t2.empid


 

Monday, May 27, 2013

String Reversal (C#)

class Program()
{
static void Main()
{
string str = "this is a string";
char[] charArr = str.ToCharArray();
StringBuilder build = new StringBuilder();

for (int i = charArr.lenght-1; i >=0; i--)
{
build.Append(charArr[i]);
}
Console.WriteLine(build.ToString());
Console.ReadKey();
}
}

What is the difference between a ref and out keyword? (C#)

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. For example:
class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value; Method(out value);
        // value is now 44     }
}

class RefExample
    {
        static void Method(ref int i)
        {
           // The following statement would cause a compiler error if 'i' were boxed as an object.            

 i = i + 44;
        }
        static void Main()
        {
            int val = 1;
            Method(ref val);
            Console.WriteLine(val);
            // Output: 45         }
    }
Although variables passed as an out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.

The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument. These two methods, for example, are identical in terms of compilation, so this code will not compile.

class CS0663_Example
{
    // compiler error CS0663: "cannot define overloaded methods that differ only on ref and out"     public void SampleMethod(out int i) {  }
    public void SampleMethod(ref int i) {  }
}

Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:

class RefOutOverloadExample
{
    public void SampleMethod(int i) {  }
    public void SampleMethod(out int i) {  }
}

Find the sum of all natural numbers which are multiples of 3 and 5 below 1000. (C#)

Question


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Solution


static void Main()
{
//List all numbers below 100 which are multiples of 3 or 5
List Multiples = new List();
for (int i=0; i<1000 i="" int="">
{
if (i%3 ==0) || (i%5==0)
{
Multiples.Add(i);
}
}
Console.Write (Multiple.Sum());
Console.ReadKey();
 
 

Write a method to print out multiplication tables (C#)

Question


Write a method where when you input a number, it prints out a multiplication table of column header times row headers up to the input number. For example, if input = 2, it would write a table with 1 times 1, 1 times 2, 2 times 1 and 2 times 2.

Solution


class Program
{
static void Main ()
{
int input = 10;
if (input <=0)
{
Console.Write("No table");
}
else
{
int[ , ] table = new int[input,input];
 for(int i =0; I < input; i++)
 {
 for (int j =0; j < input; j++)
 {table[i,j] = (i+1) * (j+1);}
}
}
foreach (var i in table)
{
Console.write(i);
Console.Write(Environment.NewLine);
}
}
Console.Readkey();
}



 
 




 

Saturday, May 25, 2013

Write a method to find all prime numbers between 50 and 100

class Program
{
static void Main()
{
//Call the method to check for prime numbers and only print if the number is prime.
for (int x=50;  x<101 p="" x="">{
bool result = IsPrime(x);
if (result == true)
Console.Writeline("{0} is a prime number", x);
}
Console.ReadKey();
}

public static bool IsPrime(int n)
{
//negative numbers, 0 and 1 are not prime numbers
if ( n<= 1)
return false;

else
{
//you need to divide a number only by 2,3,5,7

int [] primeNumDivisors = new int[]{2,3,5,7};

foreach (int I in primeNumDivisors)
{
// no need to divide the number by itself
if ( n ! =I)
{
if (n% i ==0)
return false;
}
}
return true;
}
}
}