Thursday, November 10, 2011

File copying using FTP

Normal FTP file copying process using built in classes provided System.Net NameSpace.
The code is as follows, do not forget to add System.Net namespace.

Wednesday, November 9, 2011

Swapping two integers

Swapping Two Integers using different methods.
     1) Using Temporary variable
     2) Using Arithmetic Operators
     3) Using EX-OR operator

 Here is the complete source code.

Output it as shown Below:

Tuesday, November 8, 2011

DataRowState

DataRowState : Its a state corresponding to each row in DataTable.
Consider the following scenario, we have a set of records in DB and from the User Interface we need to provide the facility for user to update any existing records or to add any new records. But, this set of changed records need to send to the DB all at once (i.e for example on a Button Click event), Till that time we need to retain all the records at our page itself. and user also have an option to cancel these changes.
So, while sending all the records to DB at once , we need to find a way to check which row is to be inserted , and which row need to be updated in DB, one option is to maaintain one more column in the table as flag, but its very difficult to maintain these flags. another option is to take the advantage of the DataRowState (if we assume records will be maitained in the DataTable). Each row will be having a State (like Added, Unchanged, Modified etc..), using this States we can easily find out which row should be saved and which row should be updated.
DataRowState is very much useful in this kind of scenarios.
First we will look at the different states present in DataRowState,
Added The row has been added to a DataRowCollection, and AcceptChanges has not been called.
Modified The row has been modified and AcceptChanges has not been called.
UnChanged The row has not changed since AcceptChanges was last called.
Detached The row has been created but is not part of any DataRowCollection. A DataRow is in this state immediately after it has been created and before it is added to a collection, or if it has been removed from a collection.
Deleted The row was deleted using the Delete method of the DataRow.
The below is the example with the all states.
The output will be as follws:
In the above output we can see that , at last the changes are canceled , but still one of the row is not exist. this is because it has got removed(not deleted). we cannot get the records back once we use Remove. but if we use Delete , still we can get our records back.

Friday, June 24, 2011

NUnit Sample Application

Steps to use NUnit

1) First download current version of NUnit here.

2) Install it in your machine.

3) Create one sample Class Library in visual studio,and name it Calculations.

4) Now, we just write some small caluculation methods like Addition and Subtracton etc..

Code(cs file) will be like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculations
{
public class Calc
{
private int sum;
private int subtr;

public int Addition(int a, int b)
{
sum = a + b;
return sum;
}
public int Subtraction(int a, int b)
{
subtr = a - b;
return subtr;
}
static void Main(string[] args)
{
}
}
}


5) Then Create another class library, where we can write the test cases for the above methods.

Let us assume it as a NunitSampleTest.cs

a)For this Class library we need to add nunit reference.

If you have installed NUnit in C drive, You can find this reference from the below path
C:\Program Files\NUnit 2.5.10\bin\net-2.0\framework\nunit.framework.dll

b)Then we need to add Calculations referce.

Now we need to write the test cases as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calculations;
using NUnit.Framework;

namespace NUnitSampleTest
{
[TestFixture]
public class NUnitTestClass
{
[Test]
public void AdditionTest()
{
Calc newobj = new Calc();
int sum = newobj.Addition(20, 10);
Assert.AreEqual(30, sum);
}
[Test]
public void SubtractionTest()
{
Calc newobj = new Calc();
int subtr = newobj.Subtraction(20, 10);
Assert.AreEqual(10, subtr);
}
static void Main(string[] args)
{
}
}
}

[Note: In the above code we can find out two new attributes, i.e TestFixture and Test and one class Assert.
The TestFixture attribute is specified for the classes, When we attach this attribute to a class, the Test Runner application will scan it for test methods.
The only restrictions on classes that use the TestFixture attribute are that they must have a public default constructor (or no constructor which is the same thing).

The Test attribute marks a specific method inside a class that has already been marked as a TestFixture, as a test method.

Assertions are central to unit testing. The Assert claas provides a variety of static methods, using which we can test the result with the actual expected value.]

this all completes the codeing part

Now to test this application, we need to open NUnit UI, this we can find out at the below path
(C:\Program Files\NUnit 2.5.10\bin\net-2.0)
or
Goto start->All Programs ->NUnit 2.5.10


The window will be like as shown below




Now Goto File->open Project , then Go to the path and click on the NunitSampleTest.Dll



It will open the methods as shown below..





so right click on the individual methods or whole class and click on Run



If all the test cases are passed successfully, then we can see the green color Line as shown below




But if get Red color line, then it means TestCase is failed.



The above figure indicates that we have an issue in Subtraction Method..




Note: To Make things simpler we have hard coded the values in the TestCase methods.
Rather than hard coding the values, we can create one seperate class with values and can pass them to respective methods.

Friday, March 4, 2011

Do's and Dont's While Designing with CSS

(1) Don't Use Style Tag in the page:

Define a new CSS class in CSS File (for example Sample.css) for every style you need and call the respective class in your HTML Element/ASP.NET Control.

For Ex:

For HTML control:

Do : < div class="textalignleft" > < / div >
Don't : < div style="text-align:left;" > < / div >

For ASP.NET control:

Do : < asp:label runat="server" text="SampleLabel" ID="lblSample" CssClass="textalignleft" > / asp:label >

Don't :< asp:label runat="server" text="SampleLabel" ID="lblSample" style="text-align:left;" > < / asp:label >

Define the class in CSS file (Sample.css) as follows:

.textalignleft
{
text-align:left;
}

(2) Avoid giving multiple properties to same CSS class:

Instead define mutiple classes with single property and call the multiple classes in the HTML/ASP.NET control class tag as follows

For Ex:

Do : < div class="textalignleft yellowcolor fullscreen" > Test < / div >

In the css class define as follows:

.textalignleft
{
text-align:left;
}
.yellowcolor
{
background-color: #ffff00;
}
.fullscreen
{
width:100%;
}

Don't : < div class="leftyellowtextfullscreen" > Test < / div >

.leftyellowtextfullscreen
{
text-align:left;
background-color: #ffff00;
width:100%;
}

Because these Single property classes can be used for other controls or other div's

(3) Always Check for existing classes for your required property, before creating a new one:
Do not repeat class name or create a new class with same existing property values.

For Ex:

.textaligncenter
{
text-align:center;
}
.textcenter
{
text-align:center;
}



(4) Use lower case to define class name

For Ex:

Do:
.textaligncenter
{
text-align:center;
}

Don't:
.TextAlignCenter
{
text-align:center;
}

(5) Define all CSS class in CSS file and not in respective Page/ Page Head Tag

For Ex:

Do: Define in Sample.css
.displaynone
{
display:none;
}


Don't
< asp:Content ID="cntInstrumentSearchHeader" ContentPlaceHolderID="head" Runat="Server" >
.displaynone
{
display:none;
}
< / asp:Content >


(6) Avoid giving CSS to HTML element

For Ex:

Don't

input, select
{
font-size: 1em;
width: 17.5em;
margin: 0;
float: left;
margin-right: 4px;
}

table
{
position: relative;
margin: 0 8px 8px 8px;
max-height: 400px;
clear: both;
}