CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Openconnection Issue

    Hi Friends,
    i have written code for openconnection.show that if connection is really open it should simple return true.or if connection does not open.then it should return falso.but i am getting following errors.
    The best overloaded method match for 'MakeConnection.Form1.OpenConnection(ref System.Data.OleDb.OleDbConnection)' has some invalid arguments D:\chash\MakeConnection\MakeConnection\Form1.cs
    (2)Argument '1' must be passed with the 'ref' keyword
    (3)No enclosing loop out of which to break or continue
    Code:
     
    [
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data.OleDb ;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace MakeConnection
    {
        public partial class Form1 : Form
         {
    
           OleDbConnection conn = new OleDbConnection();
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {   
             if ( ! OpenConnection(conn)== true) 
                    {
                   MessageBox.Show("Connection is Not Open");
                   break;
                      }
    
            }
            private bool OpenConnection(ref OleDbConnection conn)
            {
                try
                {
                    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Imp\abc.mdb");
                    conn.Open();
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }
            }
    
        }
    }
    Last edited by xtab; July 26th, 2010 at 07:11 AM.

  2. #2
    Join Date
    Jul 2010
    Posts
    8

    Re: Openconnection Issue

    Use as OpenConnection(ref conn)==….
    This ref is not very useful too, as objects are always passed as reference. Practically, the objects pointer is here passed as reference here.
    Advice…
    Never ever call connection open methods in form load events. As slow connection will make the user unhappy, and may click the calling menu multiple times, creating a lot more forms.

  3. #3
    Join Date
    Jul 2010
    Posts
    82

    Re: Openconnection Issue

    And What are you doing in OpenCOnnection() - Using Conn.ConnectionState == adStateOpen should let you know whether its a valid open connecton or not. If thats what your OpenConnection () does that, it should work. Can you post the code?

    Make sure you are returning true if Open , and false in all other cases.

    EDIT: Ok, I did not see the code fully. You must check the connectionstate and see if its OPEN. Its clearly NOT since its returning False.

    www.cuteassistant.com - Increase your productivity! Be more successful in life!
    Last edited by CuteAssistant; July 14th, 2010 at 05:47 PM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    If you want to check the connection state of the OleDbConnection object, then check the OleDbConnection.State property.

    Code:
    bool IsConnectionOpen( OleDbConnection conn )
    {
      return conn.State != ConnectionState.Closed;
    }
    Now that I've answered how to check the status, I wonder why you need to check if a connection is opened?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    Quote Originally Posted by xtab View Post
    Simple Said.i want Such type of function.if Connection is really open then this function must be return true.and if connection is not open
    then it must be return false
    I understand what you wrote and what you are trying to accomplish.

    What I am asking is WHY.

    If you are planning on making a connection and keeping the connection opened long enough to have to check if it is still open, that is probably the wrong approach.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    xtab, do you read my previous reply?
    Quote Originally Posted by Arjay View Post
    If you are planning on making a connection and keeping the connection opened long enough to have to check if it is still open, that is probably the wrong approach.

  7. #7
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Re: Openconnection Issue

    hellow now code is Working fine .additional i have made one openconnection .i want to write this function in module.but can anybody tell me???from where i get module . when i see in the project menu option of visual studio 2005. i did not get add module option only class option is there .so let me know please.
    Code:
    
    publicbool OpenConnection()
    {
    try
    {
    // OleDbConnection conn = new OleDbConnection();
    conn = newOleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\chash\MakeConnection\MakeConnection\bin\Debug\xx.mdb");
    conn.Open();
    if (conn.State != ConnectionState.Open)
    {
    MessageBox.Show("Connection is Not Open");
    returnfalse;
    }
    else
    {
    return conn.State == ConnectionState.Open;
    }
    }
    
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    returnfalse;
    }
    }
    and at each form.i want to check the connection like that .let me know anybody.
    Code:
    
    privatevoid Form1_Load(object sender, EventArgs e)
    {
    if (!OpenConnection()) 
    {
    MessageBox.Show("Connection is Not Open");
    }
    filllistbox();
    
    }


    i have written closeconnection() also .so that after doing database manipulation.close it .immediately.
    If you are planning on making a connection and keeping the connection opened long enough to have to check if it is still open, that is probably the wrong approach.
    why it is not possible ???Open the connection when the application starts and use that connection throughout the whole forms.let me know please.
    Code:
    private void CloseConnection()
               {
                try
                 {
    
                if (conn.State  == ConnectionState.Open )
                        {
                    conn.Close() ;
                         }
                    conn.Dispose(); 
                    
                   }
                 catch(Exception ex)
                    {
                  MessageBox.Show(ex.Message);
                  conn.Close(); 
                   
                    }
                  }
    Last edited by xtab; August 3rd, 2010 at 07:49 AM.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    Quote Originally Posted by xtab View Post
    [SIZE=3]why it is not possible ???Open the connection when the application starts and use that connection throughout the whole forms.let me know please.
    What do you hope to gain by doing this? Do you expect performance gains?

  9. #9
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Re: Openconnection Issue

    What do you hope to gain by doing this? Do you expect performance gains?
    what is the problem if we doing that way ???f we make any MakeConnection and want to use this Function throughout the application!!!!!.
    Last edited by xtab; August 4th, 2010 at 07:12 AM.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    Quote Originally Posted by xtab View Post
    what is the problem if we doing that way ???f we make any MakeConnection and want to use this Function throughout the application!!!!!.
    There are several reasons for not doing it this way:

    1) You run the risk that the connection gets dropped, so you have to include code that test whether the connection is opened.
    2) You add additional load on the database with extra connections being held open
    3) The data providers are efficient (i.e. providers such as OleDbConnection) and most if not all use a thread pooling mechanism to manage connections. The data provider internally will decide whether to really close a connection or keep one (or more) open based on how often the client code hits the database. Because of this caching mechanism Opening/Closing the connection for each database hit is rarely a performance hit.

    The bottom line is the recommended approach is to Open/Close the connection everytime you need to connect to the database and let the data provider do its job internally.

    If you follow this approach, then you only need to write code as follows:

    Code:
    using( var conn = new OleDbConnection( ... ) )
    {
      conn.Open( );
    
      using( var cmd = new OleDbCommand( "...", conn ) )
      {
         // command code goes here
      }
    }
    The above code will open the db connection, execute the command (using a data adapter if you wish), and then close the connection. The OleDbConnection data provider will internally decide whether to cache the connection or truly close it.

  11. #11
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Re: Openconnection Issue

    Can SomeOne tell me ?How should I write DataBase Path in the settings area af the properties ?let me know please.When tried to Click on settings nothing happens.so let me know How should I assign DataBase path there.

    Thx in Advance.
    Attached Images Attached Images  

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    Quote Originally Posted by xtab View Post
    Can SomeOne tell me ?How should I write DataBase Path in the settings area af the properties ?let me know please.When tried to Click on settings nothing happens.so let me know How should I assign DataBase path there.

    Thx in Advance.
    Please create a new post for this unrelated question.

  13. #13
    Join Date
    Jul 2010
    Location
    Watch Window
    Posts
    87

    Question Re: Openconnection Issue

    Can someone tell me ?why it says invalid Identefier.let me know please.
    thx in advance.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Openconnection Issue

    Quote Originally Posted by xtab View Post
    Can someone tell me ?why it says invalid Identefier.let me know please.
    thx in advance.
    Can you follow directions? thx in advance.

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

Featured