Default Access Modifiers in C#

top level class: internal

method: private

members (of class): private (including nested classes)

members (of interface or enum): public

constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)

delegate: internal

interface: internal

Read more...

Difference between inner and outer join in SQL

Assume you have two tables A and B.

Inner join of A and B gives the intersect of A and B. Inner join requires atleast one match in both tables. (i.e., in A and B)


Outer join of A and B gives the unoin of A and B. Outer join returns all the record from the left table even though they don't have any common record.



Example:

Assume you have two tables A and B. Both tables have one column in common:

Table A Table B

A     B

ID     ID
1     6
2     1
3     5
4     2

Inner join :

It results the intersection of the two tables, i.e. the two rows they have in common.

select * from A INNER JOIN B on A.ID = B.ID;

The result will be...
1  1
2  2

Left outer join :

It will return all rows from A , plus any common rows from B.

select * from A LEFT OUTER JOIN B on A.ID = B.ID

Result :
1   1
2   2
3   null
4   null


Full outer join :

It will return the union of A and B, i.e. All the rows in A and all the rows in B.

select * from A FULL OUTER JOIN B on A.ID = B.ID;

   1      1
   2      2
   3     null
   4     null
  null    5
  null    6

Read more...

Case statement in sql Server


Case expression can be used in Sql. This can be used with SELECT list, WHERE clause, HAVING clauses, IN list, DELETE and UPDATE statements.

Case is used to check one Expression over multiple values.

Syntax:

CASE Expression
WHEN expression1 THEN value1
WHEN expression2 THEN value2
ELSE value3
END

Example:

Consider a situation where you have to show EmployeeID and EmployeeName , so that if EmployeeID is 1 then the name should be 'John', when EmployeeID is 2 then the name should be 'Smith' and when the EmployeeID is 3, then the name should be 'Adam'. It can be done using CASE as below...

Select EmployeeID , EmployeeName =
CASE EmployeeID
WHEN  1 THEN 'John'
WHEN 2 THEN 'Smith'
WHEN 3 THEN 'Adam'
ELSE 'Something'
END From EmployeeTable.

For More Information :Case statement in Sql
Read more...

Must declare the scalar variable @variableName c# Sql


When u need to pass parameters to sql Query. This can be done in two ways..

First one:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

string query = "select * from table where columnName = '" + txtSearch.Text + "'";

SqlCommandcommand = new SqlCommand(query,connection);

Bur this approach is not recommended. this may lead to error sometimes. So, whenever we need to pass parameters to query, use SqlParamater. The same query can be written as..

string query = "select * from table where columnName =@value";

SqlCommand command = new SqlCommand(query,connection);

command.Parameters.AddWithValue("@value",txtSearch.Text);

Using SqlParameters gives a cleaner, less error prone and SQL injection safe (comparative) code.


Read more...

LINQ Single and SingleOrDefault



Single means you are expecting only one result from LINQ query. If you get more than one or zero matches, then it throws exception.

SingleOrDefault means if you get single result, it returns that result but if it doesn't find any matches it returns default value but you will not get any exception.
Read more...

Return only one result from LINQ Query

You can do it in two ways, either use First or use Single.

First returns only first row, even there are multiple rows

Single expects only one row to be returned, and if there are multiple rows, it throws exception.

So, use Single if you are expecting only one row to be returned.

EX: var book= from book in store where (book.price > 100) select book).First();
Generally , the query returns all books having price greater than 100. but , as we are using  " .First() " only first book will be returned.


Read more...

return more than one value from a method


We can't return more than one values from a method.

An alternative way would be, add all those values to be returned into  Array and return that array.

Ex: public int[] returnArray ()
{
int[] myArray=new int[3];
myArray[0] = value1;
myArray[1] = value2;
myArray[2] = value3;

      return myArray;
}

Read more...

Add Item to a list at given position (Insert item in list) in c#

In this post am going to explain how to add new item to a list at specific position.

You can add items to a list using myList.add("some thing"). but this adds that item as the last item in that list.

If you want to add a item at a specific position or if you want to insert a item into a list you can do it like below,

myList.insert(position, item);

Ex: myList.insert(2, item) --> i am inserting this item at position 2.

Read more...

Collection was modified; enumeration operation may not execute c#


You will get this error when you are trying to remove list item in foreach loop.

either you have to use removeAll or removeAt.

It's good to iterate backward by index. like below,

for(int i = list.Count - 1; i >= 0; i--)
{
 if({some condition})
    list.RemoveAt(i);
}

Read more...

Initialize list with size in c#


public static List<t> listitems = new List<t>(10);

It just creates a list with maximum capacity 10, but the count will be 0 only. It means the list is still empty.

If you want to create a list without any size, you can do it like this,

public static List<t> listitems = new List<t>(10);

Read more...