I think I have spotted your error - you call the
ConnString()
method multiple times creating a new connection instance each time.
Your code should be
public string RDDTConnectionString = @"Provider=sqloledb;Data Source=MyServer;Initial Catalog=MyDataBase;Integrated Security=SSPI;";
public OleDbConnection ConnString()
{
OleDbConnection connString = new OleDbConnection();
connString.ConnectionString = RDDTConnectionString;
return connString;
}
private void btnSaveDealSheet_Click(object sender, EventArgs e)
{
try
{
OleDbConnection connString = ConnString() ;
if (connString.State == ConnectionState.Closed)
{
connString.Open();
MessageBox.Show("ConnString.State:" + connString.State);
}
OleDbCommand InsertCommand = GetInsertDealSheetCommand();
InsertedID = Convert.ToInt32(InsertCommand.ExecuteScalar());
MessageBox.Show("DealSheet Successfully Added");
}
catch (Exception ex)
{
MessageBox.Show("error: " + ex.Message); ;
}
}
public void OpenConnectionString()
{
try
{
OleDbConnection connString = ConnString() ;
connString.Open();
if (connString.State == ConnectionState.Open)
{
MessageBox.Show("opened");
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}