C SHARP

(Home)

Top 8 Resources
Resource CSharpFriends.com
Resource VB.Net and C# Comparison
Resource Developing Windows Applications in C# - Stored Procedures
Resource DevCity.Net - Visual Inheritance
Resource FarelBoy.com
Resource SQL Connection Code
Resource CodeSmith
Resource Ned Comp Hosting - DataRelations
Quick Reference
   
Code
Combo Box/ Storedprocedure 

 

Add declarations below to the form class 

SqlConnection Con ;
SqlCommand cmd, cmdSp;
SqlDataAdapter da;
DataSet ds;
DataColumn parentCol;
DataColumn childCol;
DataRelation relEmpTer;
DataView dv;

 

============================= Stored procedure in Northwind =======================

CREATE PROCEDURE getEmpName  AS

Select EmployeeId, FirstName+' '+LastName as FirstName from Employees

GO




private void Form1_Load(object sender, System.EventArgs e)
{
Con = new SqlConnection("User ID=;password=;Initial Catalog=Northwind;Server=Corporate1; integrated security = sspi");

cmd = new SqlCommand();
cmdSp = new SqlCommand();
da = new SqlDataAdapter();
ds = new DataSet("myDs");

Con.Open();
cmd.Connection = Con;
cmdSp.Connection = Con;
cmdSp.CommandType = CommandType.StoredProcedure;
cmdSp.CommandText="getEmpName";
da.SelectCommand = cmdSp;
da.Fill(ds, "Emp");
comboBox1.DataSource = ds.Tables["Emp"];
comboBox1.DisplayMember = ds.Tables["Emp"].Columns["FirstName"].ToString();
comboBox1.ValueMember = ds.Tables["Emp"].Columns["EmployeeID"].ToString(); 
Con.Close();
}

:ATL LIBRARY - Example

To create the example C# .NET Class Library:

------------------------------------------------------------------

1) Open Visual Studio .NET 2003.

2) Open the File menu, click New, and select Project.

3) In "Project Types", click "C# Projects", and then click to select "Class Library".

4) Specify MyCSClassLib for the name, and click the OK button.

5) A "Class1.cs" editor window will be opened; close this window.

6) Open Solution Explorer, right-click Class1.cs and select Delete.  Click the OK button when you are prompted that it will be permanently deleted.

7) Open the Project menu and select "Properties".

8) In the MyCSClassLib Property Pages dialog, click "Configuration Properties" and then click "Build".

9) Set "Register for COM Interop" to True and then click the OK button to close the properites dialog.

10) Open Solution Explorer again, right-click the MyCSClassLib project (not the "Solution..."), and select Add and "Add Class".

11) Specify "MyClass.cs" for the name and click the Open button.  This will open a MyClass.cs editor window for the class.

12) At the top of this file, under the "using System;" statement, add the following statement:

 

using System.Runtime.InteropServices;

 

13) Change the MyClass constructor to look like the following:

 

                        public MyClass()

                        {

                                    myHeight = 1;

                                    myWidth = 1;

 

                                    myTestString = "This is a string value";

 

                                    CalcArea();

                        }

 

14) Add the following code to the MyClass class body:

 

                        private double myHeight;

                        private double myWidth;

                        private double myArea;

 

                        public double Height

                        {

                                    get

                                    {

                                                return myHeight;

                                    }

                                    set

                                    {

                                                myHeight = value;

                                                CalcArea();

                                    }

                        }

 

                        public double Width

                        {

                                    get

                                    {

                                                return myWidth;

                                    }

                                    set

                                    {

                                                myWidth = value;

                                                CalcArea();

                                    }

                        }

 

                        public void CalcArea()

                        {

                                    myArea = myWidth * myHeight;

                        }

 

                        // John, here's an example of exporting a string.

                        private string myTestString;

                        public string  TestString

                        {

                                    get

                                    {

                                                return myTestString;

                                    }

                                    set

                                    {

                                                myTestString = value;

                                    }

                        }

 

15) Build the solution (Build menu, select "Rebuild Solution").

 

16) Access this from VB6 by choosing References from the Project menu, and then placing a check mark next to "MyCSClassLib", adding a button to the Form1, and adding the following code into

 

the Command1_Click event handler:

 

    Dim obj As MyCSClassLib.MyClass

    Set obj = New MyCSClassLib.MyClass

   

    obj.Height = 25.4

    obj.Width = 72.3

   

    MsgBox "For height = " + CStr(obj.Height) + " and width = " + CStr(obj.Width) + ", the area = " + CStr(obj.Area)

   

    MsgBox "The TestString from MyCSClassLib.MyClass = " + obj.TestString

 

17) Run the vb6 app by pressing the F5 key.