Search This Blog

Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Monday, May 30, 2011

Debugging made easy - Part1

We will briefly discuss 3 things here -
1) Run to Cursor
2) Set Next Statement
3) Insert Tracepoint

1) Run to Cursor - While debugging, if you right click on any statement you can see many options. One of them is Run To Cursor.  It can be used as a one time breakpoint. Like when u want to execute a few rows in a go. You can right click on line where u want ur execution to come and select Run to cursor.

2) Set Next Statement - While debugging when you right click you see another option - Set Next Statement. This command jumps the program counter directly to the line under cursor. You can give a line above or below the current execution step. A major difference between Run to Cursor and Set Next statement is that the former executes all the statements till the point while the latter skips all the lines and directly executes next statement. Because of this behavior, we can take the control inside if-else or loop etc. I usually use it when I get a UE (Unexpected Error) and want to inspect where exactly it came from by setting the first statement of method as Next statement. You can see details at http://msdn.microsoft.com/en-us/library/09yze4a9(v=vs.85).aspx

3)  Insert Tracepoint - You can insert tracepoint as you insert breakpoints. The difference between both of them is that while a breakpoint breaks the execution flow, a trace point does a specified action and/or breaks the execution flow. Tracepoints can be used for the same purpose as Trace is used, without modifying the code, but they work only in "Debug" mode. For more details http://msdn.microsoft.com/en-us/library/232dxah7.aspx

Some tips - Placing bookmarks in VS

Many a times we find ourselves in a situation where we need to scroll up and down a page and in between we get lost... To make our lives simpler we have bookmarks in Visual Studio Environment.

We can place bookmarks on the lines where we would like to re-visit, delete them, delete all of them, jump to next or previous bookmarks within same page or within the whole solution. Visual Studio also provides a toolbar so that it can be done just in one click.


1) To place a bookmark or to remove a bookmark.
2) To navigate to previous bookmark (within the solution)
3) To navigate to next bookmark (within the solution)
4) To navigate to previous bookmark (within the folder)
5) To navigate to next bookmark (within the folder)
6) To navigate to previous bookmark (within the page)
7) To navigate to next bookmark (within the page)
8) To delete all bookmarks.

Their are also keyboard shortcuts available :-
1) To place a bookmark or to remove a bookmark  Ctrl + B, T
2) To navigate to previous bookmark (within the solution)  Ctrl + B, P
3) To navigate to next bookmark (within the solution)  Ctrl + B, N
4) To delete all bookmarks.  Ctrl + B, C

Cheers!

Tuesday, September 28, 2010

Writing into an excel file

You need to add reference to  Com component - Microsoft.excel.

Excel.ApplicationClass app = new Excel.ApplicationClass();

Excel.Workbook wk = app.Workbooks.Open("F:\\MyExcel.xls", 0, false, 5,"", "", true, Excel.XlPlatform.xlWindows, "\t", true, false,0, true,true,false);

Excel.Sheets sheets = wk.Worksheets;

Excel.Worksheet sheet1 = (Excel.Worksheet)sheets.get_Item(1);

try{
for (int i = 1; i <= dtEmployee.Rows.Count; i++)  //dtEmployee is a datatable containing data
{
        for (int j = 1; j <= dtEmployee.Columns.Count; j++)
         {
                sheet1.Cells[i, j] = dtEmployee.Rows[i-1][j-1];
          }
}
}

catch{
}

finally{

app.Workbooks.Close();
app.Quit();
}

Friday, September 10, 2010

Creating User Control

To create a user control. Right click on Project->Add New Item-> User Control. Name it, say, myUserControl

A page with .ascx extension is control. Use it to place your controls.

To add it to ur page. Go to your webpage and open it in design mode. Drag and drop the usercontrol on it. When you got to Source view you will see that a new <%Register ... %>  attribute is added to your code. And a new control is added like
<uc1:myUserControl ID="myUserControl1" runat="server" />

You can access it in code using its ID like other server controls.

To send data to user control from your page
TextBox txtuc1 = (TextBox)myUserControl1.FindControl("TextBox1");
txtuc1.Text = "I am user control";
 
To send data from user control to page
use Response.Redirect and pass data in querystring, just like you do redirect normally.

Creating a custom control

Go to Projects and click on Add new project = Select ASP.NET server Control and give it a name, say, myCustomControl

A class is created which inherits WebControl. Here u can write methods and set properties. Properties are set in ViewState as their value might get lost between postbacks otherwise.

public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["Text"] = value;
}
}
 
You can access the public properties and methods in ur project (will see later).
Now build this application, so a dll is available to u
 
To add custom control to Project
The dll of myCustomControl must be present in bin folder. (use addExistingItem)
Then u need to register the Custom Control like this
<%@ Register Assembly="myCustomControl" namespace="myCustomControl" TagPrefix="myCC"/>
 
To use the customcontrol in aspx
<myCC:ServerControl1 ID="CC1" runat="server" /> (you can specify height width etc
 
To use it in codebehind, just use like any other server control
CC1.Text or any other property or method.