How to debug multilanguage applications in Visual Studio .NET (317790)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q317790

SUMMARY

This article shows how to debug a multiproject, multilanguage solution that consists of a Microsoft Visual Basic .NET Web Service and a Microsoft C# class library that simulates the data tier.

back to the top

Requirements


The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 Professional (or Server), or Microsoft Windows XP Professional (or Server) with the .NET Framework installed.
This article assumes that you are familiar with the following topics:
  • The Visual Studio debugger and basic debugging concepts.
back to the top

Use the Enhanced Debugger in Visual Studio .NET Multilanguage Debugging

  1. Open Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, select Visual Basic Projects from Project Types. In Templates, select ASP.NET Web Application. In the Location textbox, replace "WebApplication1" with "HowToDebug". Click OK. We now have the shell of a Visual Basic .NET Web application in a solution named "HowToDebug". To make this a multilanguage solution, we'll add a Visual C# class library:

  4. In the Solution Explorer, right-click the HowToDebug solution, click Add, and then click New Project.
  5. Select Visual C# Projects. In Templates, select Class Library. In the Name text box, you should see "HowToClassLib". Click OK to add the project to the solution.
  6. Class1.cs should now be open in the Code Editor window. Add two namespace declarations at the top in order to have shorthand access to the classes in them:
    using System.Data;
    using System.Data.SqlClient;
    					
  7. Replace the public Class1() method with the following method that returns a DataSet from the Northwind database:
    public static DataSet GetProducts(string CategoryID)
    		{
    			DataSet ds = new DataSet();
    	SqlDataAdapter sqlDS = new SqlDataAdapter("select * from products where categoryid=" + CategoryID, new SqlConnection("server=localhost;database=northwind;uid=sa"));
    			sqlDS.Fill(ds);
    			return ds;
    		}
    					
  8. In the Solution Explorer, expand the HowToDebug project, right-click WebForm1.aspx, and then select View Designer.
  9. Press F4 to access the Properties window. Change the pageLayout property to FlowLayout.
  10. Click Web Form and type "CategoryID: ". Press CTRL+ALT+X to open the toolbox. In the toolbox, select Web Forms. Drag a text box and a button to the Web Form. Press F4 to access the Properties window for the button. Change the Text Property to "Get Products".
  11. Press Enter to create a new line on the Web Form. From the toolbox, drag a DataGrid control to the Web Form.
  12. Double-click the Button to create a Click event handler in the Web Form's codebehind class. This is where we will place the code to call the GetProducts method. Before we add the code to the handler, however, we need to add a reference to the C# class library.
  13. In the Solution Explorer, under HowToDebug, right-click References and click Add Reference.
  14. Click the Projects tab; HowToClassLib should be selected. Click Select and HowToClassLib should appear in the Selected Components list box. Click OK. You should now see HowToClassLib listed under References for the HowToDebug project.
  15. Add the following code to the Button1_Click event handler:
    DataGrid1.DataSource =   HowToClassLib.Class1.GetProducts(TextBox1.Text)
          DataGrid1.DataBind()
    					

Verify That It Works

  1. Press F5 to run the application in debug mode.
  2. When the page loads, type "2" in the CategoryID field and then click GetProducts.
  3. The DataGrid should fill with the results of the query.
  4. Close the browser to stop debugging and return to Visual Studio .NET.
back to the top

Next Steps

  1. In the Code Editor window, place your cursor on the Button1_Click event declaration. Press F9 to set a breakpoint.
  2. Click the Class1.cs tab. Set a breakpoint at the GetProducts method.
  3. Press F5 to run the application in debug mode.
  4. When the page loads, type "2" in the CategoryID field and then click GetProducts.
  5. The application will stop at the Button1_Click event handler. Press F11 to step into the code.
  6. Press F11 again. Note that the debugger seamlessly shifted to the C# method being called by the Visual Basic .NET event handler.
  7. Continue to press F11 until control is returned to the event handler.
  8. Press F5 to finish running the application. The DataGrid should fill with the results of the query.
back to the top

Complete Code Listing - WebForm1.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="HowToDebug.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body >

    <form id="Form1" method="post" runat="server">
<P>CategoryID: 
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Get Products"></asp:Button></P>
<P>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid></P>

    </form>

  </body>
</HTML>
				

back to the top

Complete Code Listing - WebForm1.aspx.vb

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGrid1.DataSource = HowToClassLib.Class1.GetProducts(TextBox1.Text)
        DataGrid1.DataBind()
    End Sub
End Class
				
back to the top

Complete Code Listing - Class1.cs

using System;
using System.Data;
using System.Data.SqlClient;

namespace HowToClassLib
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class Class1
	{
		public static DataSet GetProducts(string CategoryID)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter sqlDS = new SqlDataAdapter("select * from products where categoryid=" + CategoryID, new SqlConnection("server=localhost;database=northwind;uid=sa"));
			sqlDS.Fill(ds);
			return ds;
		}
	}
}
				
back to the top

REFERENCES

For more information about debugging, see the Microsoft Web Help file at the following Microsoft Web site:
back to the top

Modification Type:MinorLast Reviewed:3/3/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbLocalization kbDebug kbHOWTOmaster KB317790 kbAudDeveloper