Showing posts with label lists. Show all posts
Showing posts with label lists. Show all posts

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...

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...