My Thoughts & Views

Archive for the ‘C#’ Category

Interfaces
–contain only abstract methods
–interface can’t be inherited from a class
–using interfaces we can achieve multiple inheritance
–doesn’t allow accessibility modifiers (Public/Private/Internal)
–can’t contain fields, constructors
–is must implementable & its scope is upto any level of its inheritence chain.

Abstract Class
–contain both abstract methods as well as concrete methods
–can extend another class and implement multiple interfaces
–we can’t achieve multiple inheritance
–allows accessibility modifiers
–can contain fields, constructors
–class Abstract class is must inheritable & its scope is upto derived class

Steps:
1. Add wnvhtmlconvert.dll to Project Reference under Visual Studio


2. Import it into your code behind file “using Winnovative.WnvHtmlConvert;”

3. Add your PdfConverterKey into Web.Config

<add key=”PdfConverterKey” value=”your key here”/>

4. Write Below Code (Click to Expand)

And you are done.
Let me know if you have any questions

Method :

Usage :

I need to write comments for the below code, bcoz of lack of time i am posting only abstract things,if you want to know something, write comment, i will reply.

HTML login control

< input type="text" name="txtUserName" id="txtUserName" class="textBoxes" />

jQuery Rule: Add the below rule into script tag in header & add reference to jquery-1.2.6.js,jquery.validate.js in Head Element

$(document).ready(function() {

$("#frmSelfRegister").validate({
rules: {
txtUserName:
{
required: true,
remote: { type: "post",
url: "Register/IsLoginAvailable" }
},
messages:{

txtUserName: { required: "User Name is Required.",
remote: jQuery.format("{0} is not available.")}
}
});
});

The above validation code executed when the focus changes from txtUserName to some other, it will call IsLoginAvailable method available in the RegisterController, as specified in the url.

Controller Code :

Below code is the one which checks for availability of a username & returns the status to above jQuery Method. The following methods return type is JsonResult because remote method of jQuery accepts the JsonResult object only.

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult IsLoginAvailable(FormCollection collection)
{
JsonResult result = new JsonResult();

try
{
ilogIn.Text = this.GetLoginByUsername(collection.Get("txtUserName").
ToString().Trim());

}
catch (Exception ex)
{
Helper.ErrLogger(ex.Message);
}

if (ilogIn.Text == collection.Get("txtUserName").ToString())
{
result.Data = false;
}
else
{
result.Data = true;

}
return result;
}

Suppose you have two interfaces, lets say Interface-IA, Interface-IB & in both you have declared Name as a property as follows.

public interface IA
{

string Name { get; set; }
String Adress { get; set; }

}

public interface IB
{

string Name { get; set; }

}

Now, you write an interface -IC which derives from above 2 Interfaces as below,

public interface IA
{

int Id { get; set; }

}

When you are try to do the following, you will get an error saying ‘Ambiguity between IA.Name and IB.Name’.

class Test
{

public void GetSomething()
{
IC objIc = new IC();
Textbox1.Text = objIc.Name();
}

}

It can be solved by upcasting as below.

class Test
{

public void GetSomething()
{
IC objIc = new IC();
Textbox1.Text = ((IA)objIc).Name();
}

}

The DateTime class is not very exact and only can measure in milliseconds. When doing performance measuring on an application you most certainly need to be able to measure with smaller time units than that. High-resolution timers are supported in the Win32 API so following code wraps it up in a simple class:

public class PerformanceTimer
{
[DllImport(“Kernel32.dll”)]
public static extern void QueryPerformanceCounter(ref long ticks);

long startTime = 0;
long stopTime = 0;

public void Start()
{
QueryPerformanceCounter(ref startTime);
}

public void Stop()
{
QueryPerformanceCounter(ref stopTime);
}

public long Time
{
get
{
return stopTime – startTime;
}
}
}

I’m sure the code is easy to understand. Here is the sample code to show how to use it.

PerformanceTimer perfoTimer = new PerformanceTimer();
perfTimer.Start();

// Do something

perfTimer.Stop();
System.Diagnostics.Trace.WriteLine(perfoTimer.Time);

Problem : Calculate the sum of a column in a dataset

Solution :
After filling the dataset from your query you can calculate the sum of a numeric column.

Dataset’s DataTable provides a default method called Compute through which you can perform any aggregate function based operation on columns in a DataTable.

Suppose you have

1. Column : Salary
2. DataSet : dsData
3. DataTable Name : Payroll

Then

dsData.Tables(“Payroll”).Compute(“SUM(Salary)”, String.Empty)

Gives the sum of that column.

Compute accepts two arguments : 1. Expression (Any aggregate Expression)
2. Filter (Any Filtering option, here String.Empty means no filter value)