Results 1 to 10 of 10

Thread: Urgent C# Global Connection Problem

  1. #1

    Thread Starter
    Lively Member AjayKumar's Avatar
    Join Date
    Nov 2003
    Location
    Noida
    Posts
    94

    Urgent C# Global Connection Problem

    I have problem in C#.I want to make a global connection for entire windows application in C#.net.
    I have this problem becoz there is no concept of adding modules in C#.net ,unlike vb.net which has a functionality of adding modules.

    So what can i do in C3.net.
    HI ITs exciting!!!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Urgent C# Global Connection Problem

    A module in VB.NET is purely a concept. When compiled it is implemented as a NotInheritable class with all its members declared as Shared. You can just as easily create a data access class in your C# apps that provides the same functionality. The C# equivalent of NotInheritable is sealed, which you may or may not choose to include in the declaration, and static is the equivalent of Shared. It is generally frowned upon to create a class that does not encapsulate some conceptual object, but if you think about the implementation carefully it shouldn't be too hard to create a data access class that adheres to OO guidelines.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Re: Urgent C# Global Connection Problem

    Hi

    i have one more question ,if instead i make my functions etc as static in my datalayer , what if i create object of this layer as static in my business layer and then access all variables or functions as i don't want to make object of this datalayer again and again in every form coz it is to be accessed globally in my project.

    plz help me whether it will affect any working or any drawback or problem or any other alternate good solution .

    Thanks
    Pankaj
    Last edited by PankajGupta; Sep 9th, 2005 at 04:43 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Urgent C# Global Connection Problem

    You don't need to create an instance of a class to call static members. They are members of the class, not of any particular instance. An example is MessageBox.Show. You don't have to create an instance of the MessageBox class every time you want to call it, but rather just call it using the class name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Urgent C# Global Connection Problem

    u can create a class and declare the connection as static.
    public static Sqlconnection scon

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Urgent C# Global Connection Problem

    Quote Originally Posted by PankajGupta
    Hi

    i have one more question ,if instead i make my functions etc as static in my datalayer , what if i create object of this layer as static in my business layer and then access all variables or functions as i don't want to make object of this datalayer again and again in every form coz it is to be accessed globally in my project.

    plz help me whether it will affect any working or any drawback or problem or any other alternate good solution .

    Thanks
    Pankaj
    Hmmm... I think I missed the point of this post. Yes, you can create a single instance of your data layer and assign it to a static variable so that it can be accesses from everywhere in your application. Global variables should be handled with care and only used when necessary, but after about two seconds careful consideration I would say that this is a legitimate use. It may actually be preferable, though, to make your data layer a Singleton class, and give it a static method that returns that single instance.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Re: Urgent C# Global Connection Problem

    Hi

    create a single instance of my data layer and assign it to a static variable so that it can be accesses from everywhere in my application(For that agang i have to use a class and declare that membet as static etc).

    but your point (It may actually be preferable, though, to make your data layer a Singleton class, and give it a static method that returns that single instance).I agree with that also , but could you plz tell me the difference between these two situation.


    and one more question that whatif i made constructor of my data layer as static and then just call any static method via classname.methodname.
    Here also i get rid of that situation of creating any instance from any method or assigning it to any other static member .I am also giving my class structure below..

    public sealed class DataService

    {
    public static OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter();//Global Adapter
    public static DataSet MyDataset = new DataSet();//Globaldataset
    public static OleDbConnection MyConnection = new OleDbConnection();
    public static OleDbDataReader MyDataReader ;
    private static bool IsConnected ;
    public static string MyConnectionString;
    private static int NavigationCounter = 1;//This variable is user for navigation Control only
    public static ArrayList ArrGlobal = new ArrayList();//This is used to fill Current values of current record
    public static Form CurrentForm = new Form ();//This Variable represent Current form
    public static string TableName ;//For Holding Current TableName
    public static OleDbCommand MyCommand=new OleDbCommand();

    //Constructor-----------------------
    static DataService()
    {
    try
    {
    MyConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Masu;Data Source=TRAFFIC";
    MyConnection.Open();
    IsConnected = true;
    MyConnectionString = MyConnection.ConnectionString;
    MyCommand.Connection = MyConnection;
    }

    catch (System.Exception ex)
    {
    MessageBox.Show (ex.Message);
    IsConnected = false;
    }

    }
    //-------------------------------

    public static DataSet FillDataSet(string Sql,string StrTableName)
    {

    DataSet myLocaldataset=new DataSet() ;

    try
    {
    MyCommand.CommandText =Sql;
    MyDataAdapter.SelectCommand=MyCommand;
    MyDataAdapter.Fill(myLocaldataset,StrTableName) ;
    MyDataset=myLocaldataset;
    TableName=StrTableName;
    return myLocaldataset;
    }

    catch ( System.Exception ex)
    {
    MessageBox.Show (ex.Message);
    return myLocaldataset;
    }

    }

    //-----------------------------------

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Urgent C# Global Connection Problem

    The difference between creating an instance of your data layer object and assigning it to a static variable and creating a singleton class is that with a singleton class you cannot create more than one instance, whereas in the first scenario you could create more instances and assign them to other variables. The idea of a class with all static members is going back to what I was talking about in the first place. It is the equivalent of a VB.NET module.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Thumbs up Re: Urgent C# Global Connection Problem

    Thank's a ton

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Urgent C# Global Connection Problem

    Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width