Classes in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan sanqui
    New Member
    • Nov 2011
    • 1

    Classes in C#

    hi i'm new to the c# language and im having some trouble figuring what to do,
    i wanted to put my database connections inside classes and have methods but im having trouble starting it, i have this code

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace PayrollManagement
    {
        public partial class EmployeeRegistration : Form 
        {
            public EmployeeRegistration()
            {
                InitializeComponent();
            }
    
               
          
         
                System.Data.SqlClient.SqlConnection con;
                System.Data.SqlClient.SqlDataAdapter da;
                DataSet ds1;
                int maxRows = 0;
                int inc = 0;
            
            
    
            private void EmployeeRegistration_Load(object sender, EventArgs e)
            {
     
                      con = new System.Data.SqlClient.SqlConnection();
                ds1 = new DataSet();
                con.ConnectionString = "Data Source=SANQUI-PC;Initial Catalog=PayrollProject;Integrated Security=True";
                string sql = "Select * From Emp_Acc_Info";
                da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
                con.Open();
                da.Fill(ds1, "Emp_Acc_Info");
                maxRows = ds1.Tables["Emp_Acc_Info"].Rows.Count;        
    
            }
            public void btnSave_Click(object sender, EventArgs e)
            {
               /* System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                DataRow dRow = ds1.Tables["Emp_Acc_Info"].NewRow();
                dRow[0] = textBox1.Text;
                dRow[1] = textBoxFName.Text;
                dRow[2] = textBoxMName.Text;
                dRow[3] = textBoxLName.Text;
                dRow[4] = textBoxAddress.Text;
                if (radioButtonFemale.Checked)
                {
                    dRow[5] = "Female";
                }
                else if (radioButtonMale.Checked)
                {
                    dRow[5] = "Male";
                }
                dRow[6] = textBoxContact.Text;
                dRow[7] = textBoxEmail.Text;
                dRow[8] = textBoxDesc.Text;
                ds1.Tables[""].Rows.Add(dRow);
                maxRows = maxRows + 1;
                inc = maxRows - 1;
                da.Update(ds1, "Emp_Acc_Info");
                con.Close();
                MessageBox.Show("Entry Added");*/
    this works just fine
    but i wanted to put them my database connections and the save method inside a class, any suggestions and help on how to do it, thank you guys!

    [EDIT: e-mail address removed]
    Last edited by Stewart Ross; Nov 19 '11, 03:41 PM. Reason: Added code tags; removed e-mail address
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    you should add a directory/folder called "APP_CODE". write your class in it's own .cs file and put it in this folder. make sure that you assign a namespace to the class and make your methods public. Then all you need to do is import the namespace in your other classes and call the connection methods that you need.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      I'm not entirely sure how to answer... you say you want to put your database connections into another class, so have you tried just doing so? Make another class called something like SqlDbManager or something like that which has a member variable for your connection. Either pass the connection string via a constructor or have a method called Connect, which takes the connection string as a parameter. Then provide methods to operate on your connection and give you the results you're looking for. Then you can instantiate a new member object in your EmployeeRegistr ation form and use that for your data retrieval. You could also create it in your main form and pass it to all the sub forms that will use that data base connection.

      Give it a try and let me know if you have troubles.

      Comment

      Working...