Click here to Skip to main content
15,884,849 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
I have created an application that connects to an MS Access Database. But I have also used an another class that holds the database connection variables. it is as the following

C#
using System.Text;
using System.Data.OleDb;
using System.Data.DataTable;

namespace Lanka_Lab
{
    public class dbConnect
    {
        public OleDbConnection oleDbConnect;
        public OleDbDataAdapter oleDbAdapt;
        public OleDbCommand oleDbComm;
        

    }
}


But the UI that I want to connect to the database is in another class

C#
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;


namespace Lanka_Lab
{
    public partial class frmFulBldCuntDet : Form
    {
 
    }

}


My problem is now I want to set some values to the variables in the dbconnect class using the class that holds the UI.

In another words the variables are in the dbconnect class will be used whole over the project and I created that class as a general module in VB.Net to hold global variables.

I cannot find any errors when try to set values to the variables in the dbconnect class. Please help me!

Chiransj
Posted
Updated 17-Dec-12 23:45pm
v2
Comments
skydger 18-Dec-12 6:02am    
Have you tried to add some member of dbConnect type to frmFulBldCuntDet class?
Chiranthaka Sampath 18-Dec-12 6:20am    
The dbConnect class is used to simply store the OledbConnection, OledbCommand, OledbAdapter as global variables so that I never wanted to declare in every form.
Chiranthaka Sampath 18-Dec-12 6:13am    
Yes but the member list don't show any members of the dbConnect class!
skydger 18-Dec-12 15:57pm    
It is wierd.
Have you tried to create getters (and setters if needed) for those classes?
BillWoodruff 20-Feb-15 8:25am    
Why don't you just use: public static class dbConnect ...

Modify your class like this

SQL
public class dbConnect
{
    public OleDbConnection oleDbConnect;
    public OleDbDataAdapter oleDbAdapt;
    public OleDbCommand oleDbComm;

    private static dbConnect dbConn=null;
    public static dbConnect GetConnection()
    {
        if (dbConn == null)
        {
            dbConn = new dbConnect();
        }
        return dbConn;
    }

}


Now you will get the same instance of dbConnect throughout the project. The values you set for the variables will retain till application exit.

Use the following code wherever you want the instance of dbConnect.
dbConnect db = dbConnect.GetConnection();
 
Share this answer
 
Comments
Chiranthaka Sampath 22-Dec-12 1:30am    
Ok then where should I place the connection-string in this method? And also it gives me an error at dbConn = new dbConnect(); as "Cannot create an instance of abstract class or interface!" If you could please try to solve my problem! Thank you!
Akbar Ali Hussain 26-Dec-12 13:11pm    
Add one more public variable for connection string.
I don't see any issues on the code "dbConn = new dbConnect()". dbConn is neither abstract class nor interface in the code which I provided. Verify you are running the same code without any modification
SqlBulkCopy Bcp;

   public SqlConnection Con;
   public SqlCommand Cmd;
   public SqlDataReader Dr;
   public DataSet Ds;
   SqlDataAdapter Add;
   SqlTransaction Tran;
   //public event  SqlRowsCopied(  Object  Sender,  System.Data.SqlClient.SqlRowsCopiedEventArgs e);
   public ClsConnection()
   {
       Con = new SqlConnection();

       Con.ConnectionString = ConfigurationManager.ConnectionStrings["PropertyConnectionString"].ConnectionString;


       Con.Open();
   }



   public void cancel()
   {
       Cmd.Cancel();
   }
   public void class1(string SqlCon)
   {
       Con = new SqlConnection();
       Con.ConnectionString = SqlCon;
       Con.Open();
   }
   public bool OPen()
   {
       if (Con.State != ConnectionState.Open)
       {
           try
           {
               Con.Open();
               return true;
           }
           catch (Exception ex)
           {
               return false;
           }
       }
       else
           return true;

   }

   public void Bulkcopy(string Destination, DataTable DataSource, int NotifyAfter)
   {
       try
       {
           Bcp = new SqlBulkCopy(Con);
           Bcp.BulkCopyTimeout = 300;
           Bcp.DestinationTableName = Destination;
           Bcp.NotifyAfter = NotifyAfter;
           Bcp.WriteToServer(DataSource);
       }
       catch (Exception ex)
       {
           throw (ex);
       }
   }


   public int MaxValue(string Table, string Column, string DefaultValue)
   {
       return (MaxValue(Table, Column, DefaultValue, ""));
   }

   public int MaxValue(string Table, string Column, string DefaultValue, string Condition)
   {
       return (Convert.ToInt32(ExecuteScalar("Select isnull(max(" + Column + ")," + DefaultValue + ") from " + Table + (Condition == "" ? "" : " Where ") + Condition)));


   }


   public DataRow FillRow(string Sqlstr)
   {
       CommandText(Sqlstr);
       SqlDataAdapter Ad = new SqlDataAdapter();
       Ad.SelectCommand = Cmd;
       DataSet LDs = new DataSet();
       Ad.Fill(LDs, "Table");
       if (LDs.Tables["Table"].Rows.Count > 0)
           return ((LDs.Tables["Table"].Rows[0]));
       else
           return (null);

   }
   public void FillDataSet(DataSet LDataSet, string Sqlstr, string TableName)
   {
       CommandText(Sqlstr);
       SqlDataAdapter Ad = new SqlDataAdapter();

       Ad.SelectCommand = Cmd;
       Ad.Fill(LDataSet, TableName);
   }
   public void ClearDataSet(DataSet LDataSet)
   {
       LDataSet.Clear();
   }
   public void BeginTrans()
   {
       Tran = Con.BeginTransaction();
   }
   public void CommitTrans()
   {
       Tran.Commit();
   }
   public void RollBackTrans()
   {
       Tran.Rollback();
   }
   public void CommandText(string SqlStr)
   {
       Cmd = new SqlCommand();
       Cmd.CommandText = SqlStr;
       Cmd.Connection = Con;
       Cmd.Transaction = Tran;
   }
   public static bool ExecuteNonQuery(string QueryString, params SqlParameter[] arrParam)
   {
       string qry = "";
       SqlConnection Con = new SqlConnection();


       Con.ConnectionString = ConfigurationManager.ConnectionStrings["PropertyConnectionString"].ConnectionString;
       Con.Open();

           if (arrParam != null)
           {

               qry = QueryString;

               SqlCommand cmd = new SqlCommand(qry, Con);

               cmd.Parameters.AddRange(arrParam);

               cmd.ExecuteNonQuery();


               cmd.Dispose();
               Con.Close();

               return true;


           }


       return false;

   }

   public static string changeNameloop(int len)
   {

       System.Threading.Thread.Sleep(100);
       string _allowedChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
       //string _allowedChars = "0123456789";
       Random randNum = new Random();
       char[] chars = new char[len];
       int allowedCharCount = _allowedChars.Length;
       for (int i = 0; i < len; i++)
       {
           chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
       }

       return new string(chars);

   }
   public int ExecuteNonQuery(string SqlStr)
   {
       CommandText(SqlStr);
       return (Cmd.ExecuteNonQuery());
   }

   public object ExecuteScalar(string SqlStr)
   {
       CommandText(SqlStr);
       return (Cmd.ExecuteScalar());

   }

   public int ExecuteLineReader(string SqlStr)
   {
       CommandText(SqlStr);
       Dr = Cmd.ExecuteReader();
       if (Dr.Read())
           return (1);
       else
           return (0);

   }
   public DataTable FillTable(string Sqlstr, string TableName)
   {
       CommandText(Sqlstr);
       SqlDataAdapter Ad = new SqlDataAdapter();
       Ad.SelectCommand = Cmd;
       Ds = new DataSet();
       Ad.Fill(Ds, TableName);
       return (Ds.Tables[TableName]);
   }
   public DataTable ExecuteProc(string ProcName, params     object[] Parameter)
   {
       CommandText(ProcName);
       Cmd.CommandType = CommandType.StoredProcedure;
       int i = 0;
       while (i < Parameter.Length)
       {
           Cmd.Parameters.Add(Cmd.CreateParameter());
           Cmd.Parameters[i].ParameterName = Convert.ToString(Parameter[i]);
           Cmd.Parameters[i].Value = Parameter[i * 2 + 1];
           i = i + 1;
       }
       SqlDataAdapter Ad = new SqlDataAdapter();
       Ad.SelectCommand = Cmd;
       Ds = new DataSet();
       Ad.Fill(Ds, ProcName);
       return (Ds.Tables[ProcName]);
   }

   public void CloseReader()
   {
       Dr.Close();
   }

   public void Close()
   {
       if (Con.State != ConnectionState.Closed)
           Con.Close();

   }
 
Share this answer
 
Comments
CHill60 20-Feb-15 8:33am    
This post has nothing to do with the 2 year old question which already has an accepted solution. It is just an unexplained code dump

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900