Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Add new data row to data table Error: 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

We can not directly create a new DataRow() . When we try to do so, it throws this exception

System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

So in order to create new row in data table use the following method

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);
reference link : https://msdn.microsoft.com/fr-fr/library/9yfsd47w.aspx

In this way you can create a new data row and add it to data table.
Read more...

Check Debug and Release mode in c#

In this article i am going to explain how to programatically check Debug or Release mode in C#.

By default, Visual Studio defines DEBUG if project is compiled in Debug mode and doesn't define it if it's in Release mode. So, if you want to add different code for Debug and Release modes, use the following code

#if DEBUG
  // add debug mode code goes here
#else
  // add release mode code goes here
#endif

If you only want to check for release mode, then use the following code

#if !DEBUG
  // add debug mode code here...
#endif

In this way, we can check for Debug and Release modes programatically in c#.

For more posts on c# visit  C-Sharp

Read more...

Understanding MSIL, CLR and JIT

MSIL = Microsoft Intermediate Language
CLR = Common Language Runtime
JIT = Just In Time compiler

MSIL is the "machine code like" language that the C# compiles your code into.

The CLR is the "machine" that the MSIL runs on.

When runinng the MSIL the CLR converts your code into real machine code using the JIT.

In this context just in time stands for that only the MSIL needed for the moment is beeing compiled into machine code.

The real machine code is run on yor computers actual CPU.
CodeExecution cycle



Read more...

Using statement in C# (ASP.NET)

When you are dealing with objects in c#, you have to make sure that when you are done with the object, the object's Dispose method is called.

This can be done using try and finally blocks, where you use the object in try Block and destroy the object in finally block. But this can be easily done by "using" statement.

The using statement gets translated into a try and finally block. A using statement is translated into three parts: acquisition, usage, and disposal. The resource is first acquired, then the usage is enclosed in a try statement with a finally clause. The object then gets disposed in the finally clause. For example the following lines of code using the using statement,


Normally in the try catch block we would have written:

SqlConnection con;
try
{
    con=new Sqlconnection(connectionString);
}
catch
{
       //Do Something with the exception..
}
finally
{
    con.close();  //Disposing the object manually.
}

But using block has made this simple(rather to dispose objects manually) :

using(SqlConnection con=new Sqlconnection(connectionString))
{
      con.open();
      //Do Some Operation
}

And now, you dont need to worry about the object's memory release; since using block takes care of it automatically by invoking con.close() and everyother object that is used inside it.. :)

And thus, it helps us in managing the memory in a more efficient way..

Note: One advantage with the using block is that, even in the case if the statements in that block raises an exception, that block disposes the memory.. And thats good of it.. And even we need to look at the cons of it like: what if there is an error while acquiring the source? (like if we use the filestream), in that case we should use using block nested inside the try-catch block... :)


Read more...

Clear all fields in ASP.NET form

You can make use of OnClientClick event. This will reset all all the control present on the form. OnClientClick="this.form.reset();return false;"

See the Code :

<asp:Button ID="Reset_Button" runat="server" Text="Reset"
    Width="81px" OnClientClick="this.form.reset();return false;" />

Read more...

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

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