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; } } }}
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.
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.
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.
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.
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.
[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?
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.
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.