473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace PayrollManagement
  12. {
  13.     public partial class EmployeeRegistration : Form 
  14.     {
  15.         public EmployeeRegistration()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.  
  21.  
  22.  
  23.             System.Data.SqlClient.SqlConnection con;
  24.             System.Data.SqlClient.SqlDataAdapter da;
  25.             DataSet ds1;
  26.             int maxRows = 0;
  27.             int inc = 0;
  28.  
  29.  
  30.  
  31.         private void EmployeeRegistration_Load(object sender, EventArgs e)
  32.         {
  33.  
  34.                   con = new System.Data.SqlClient.SqlConnection();
  35.             ds1 = new DataSet();
  36.             con.ConnectionString = "Data Source=SANQUI-PC;Initial Catalog=PayrollProject;Integrated Security=True";
  37.             string sql = "Select * From Emp_Acc_Info";
  38.             da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
  39.             con.Open();
  40.             da.Fill(ds1, "Emp_Acc_Info");
  41.             maxRows = ds1.Tables["Emp_Acc_Info"].Rows.Count;        
  42.  
  43.         }
  44.         public void btnSave_Click(object sender, EventArgs e)
  45.         {
  46.            /* System.Data.SqlClient.SqlCommandBuilder cb;
  47.             cb = new System.Data.SqlClient.SqlCommandBuilder(da);
  48.             DataRow dRow = ds1.Tables["Emp_Acc_Info"].NewRow();
  49.             dRow[0] = textBox1.Text;
  50.             dRow[1] = textBoxFName.Text;
  51.             dRow[2] = textBoxMName.Text;
  52.             dRow[3] = textBoxLName.Text;
  53.             dRow[4] = textBoxAddress.Text;
  54.             if (radioButtonFemale.Checked)
  55.             {
  56.                 dRow[5] = "Female";
  57.             }
  58.             else if (radioButtonMale.Checked)
  59.             {
  60.                 dRow[5] = "Male";
  61.             }
  62.             dRow[6] = textBoxContact.Text;
  63.             dRow[7] = textBoxEmail.Text;
  64.             dRow[8] = textBoxDesc.Text;
  65.             ds1.Tables[""].Rows.Add(dRow);
  66.             maxRows = maxRows + 1;
  67.             inc = maxRows - 1;
  68.             da.Update(ds1, "Emp_Acc_Info");
  69.             con.Close();
  70.             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]
Nov 19 '11 #1
2 1590
yarbrough40
320 100+
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.
Nov 21 '11 #2
GaryTexmo
1,501 Expert 1GB
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 EmployeeRegistration 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.
Nov 21 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
16
by: Simon Wittber | last post by:
I've noticed that a few ASPN cookbook recipes, which are recent additions, use classic classes. I've also noticed classic classes are used in many places in the standard library. I've been...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
6
by: mailforpr | last post by:
Suppose you have a couple of helper classes that are used by 2 client classes only. How can I hide these helper classes from other programmers? Do you think this solution is a good idea?: class...
2
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.