Monday, June 4, 2012

Debugging sql stored procedure

Steps to debug t-sql in sql server 2005
  • open visual studio 2008
  • open new project database under other project types
  • choose database reference
  • open server explorer
  • connect to the datase
  • expand the stored procedure to debug stored procedure
  • right click the stored procedure and click "Step into stored procedure" in context menu
  • provide necessary parameters for stored procedure
  • click F10 to step through and F11 to step into (if it calls anther stored procedure) stored procedure

Friday, October 28, 2011

Invalid cross-thread operation

When UI controls are accessed from thread other than the main thread ‘invalid cross thread operation’ exception will be thrown. The message may prompt as “Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on”.

It can be handled from any of following two solutions.

A. Control Invoke:

· Check if invoke required

· Invoke the control if required

private void button1_Click(object sender, EventArgs e)

{

Thread myThread = new Thread(

delegate()

{

UpdateLabel("Hello New Label!");

}

);

myThread.Start();

}

int i = 0;

private void UpdateLabel(string text)

{

i++;

text = text + i;

if (this.label1.InvokeRequired)

this.label1.Invoke((MethodInvoker)delegate()

{

UpdateLabel(text + " invoked");

});

else

this.label1.Text = text;

}

B. Using background worker:

Background worker can be used to avoid such exceptions especially when we have to report progress or report completion. The controls can be accessed in RunWorkerCompleted or ProgressChanged events. Controls like progress bar can be accessed in progress changed event to make the user feel progress is on while the operation is going on. RunWorkerCompleted can be used to update labels,textbox etc after operation completed. If UI controls accessed in DoWork event, it will throw cross thread exception.

backgroundWorker2.RunWorkerCompleted += (x, y) =>

{

this.label1.Text ="Set some text.";

};

backgroundWorker2.ProgressChanged+=(x,y)=>

{

Progressbar1.value=y.ProgressPercentage;

}

Monday, May 16, 2011

Stratey design pattern

Strategy design pattern, also known as Policy,is a behavioral design pattern.

Intent:
  • Encapsulates the algorithms to make them interchangeable from clients.


Scope:
  • When there are multiple algorithms which need to be interchangeable
  • Many related classes differ only in their behavior;to configure multiple behavior of the classes
Participants:
  • strategy
  • concrete strategy
  • context
Structure:


fig.UML Diagram

Sample Code:

Strategy:

abstract class SortStrategy
{
public abstract void Sort(List list)
}

Concrete Strategy:

class Quicksort:Sortstrategy
{
public overrid void Sort(Listlist)
{
Console.WriteLine("
Quicksort")
}
}

class SheellSort:SortStrategy
{

{
Console.WriteLine("SheellSort")

}

Context:

class SortContext
{
private List list=new
List();
private Strategy sortStrategy;
public void SetSortStrategy(SortStrategy sortStrategy)
{
this.sortStrategy=sortStrategy;
}
public void Add(string name)
{
list.Add(name);
}
public void Sort()
{
sortStrategy.Sort(list);
}


Client:

static void Main()
{
SortContext s=new SortConext();
s.Add("car");
s.Add("bus");
s.SetSortStrategy(new QuickSort());
s.Sort();
s.SetSortStrategy(new ShellSort());
s.Sort();
}






Creating crystal report with custom datasource datatable

We can use datatable as cutom datasource to crystal report with the following steps.
A.add dataset and datatable as below
-right click the project and add dataset


-double click the datatable in dataset toolbox
-right click the new datatable and add columns



B Add crystal report template
and follow the report wizard





on the list of available data sources,under the node project data and ado.net datasets,there is dataset1 which is to be selected.Move it to selected tables


add available fields to fields to display





add crystal report viewer to a form where report is to be displayed and
add following code to the form load.


Dataset1 ds = new Dataset1 ();
ds.Tables.Add(dt);
CRTransaction objRpt = new CRTransaction();
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();

dt is can be datasource of gridview or any custom datatable.