Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using VS2010 to connect to a SQLServer 2000 instance on a remote machine.
I am using a OLEDB connection string that just will not open. I'm wondering if anyone can shed some light on why this connection just won't open. Any help is greatly appreciated. Thank you!
Code below.

C#
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
    {
        if (ConnString().State == ConnectionState.Closed)
        {
            ConnString().Open();                   
            MessageBox.Show("ConnString.State:" + ConnString().State);
        }
        //Execute SQL
        OleDbCommand InsertCommand = GetInsertDealSheetCommand();
        InsertedID = Convert.ToInt32(InsertCommand.ExecuteScalar());
        //Return inserted Identity
        MessageBox.Show("DealSheet Successfully Added");
    }
    catch (Exception ex) 
    {
        MessageBox.Show("error: " + ex.Message); ;
    }
}

public void OpenConnectionString()
{           
    try
    {
        ConnString().Open();

        if (ConnString().State == ConnectionState.Open)
        {
            MessageBox.Show("opened");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message); 
    }
}
Posted
Updated 5-Dec-11 22:44pm
v3
Comments
Reiss 6-Dec-11 4:50am    
Are you getting any error messages - if so update the question to add the details.
Jennifer McCartthy 6-Dec-11 6:14am    
I'm not getting any errors at all. Just my message boxes telling me the connection state is closed.

Can you switch it to use a SqlConnection[^] - This sould be more efficient

Your connection string would then be Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;1

1Taken from www.connectionstrings.com[^]
 
Share this answer
 
v2
Comments
Jennifer McCartthy 6-Dec-11 6:14am    
Hi Reiss,

Thank you for your help.

Initially I wrote the app with a SQLConnection only to find that it is not compatible with SQLServer 2000 when using VS2010. I would love more than anything to use a SQL connection.

Thanks again for your help, its very much appreciated.

Kind Regards
Jen
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

C#
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() ; // added
        if (connString.State == ConnectionState.Closed) //changed
        {
            connString.Open();// changed                   
            MessageBox.Show("ConnString.State:" + connString.State);//changed
        }
        //Execute SQL
        OleDbCommand InsertCommand = GetInsertDealSheetCommand(); // check this method to be sure you don't create a new connection instance
        InsertedID = Convert.ToInt32(InsertCommand.ExecuteScalar());
        //Return inserted Identity
        MessageBox.Show("DealSheet Successfully Added");
    }
    catch (Exception ex) 
    {
        MessageBox.Show("error: " + ex.Message); ;
    }
}
 
public void OpenConnectionString()
{           
    try
    {
        OleDbConnection connString = ConnString() ; // added
        connString.Open(); //changed
 
        if (connString.State == ConnectionState.Open)
        {
            MessageBox.Show("opened");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message); 
    }
}
 
Share this answer
 
Comments
Jennifer McCartthy 6-Dec-11 14:39pm    
Hi Reiss,

Thank you again for your help, I greatly apprreciate it.
the changes you've made have helped me out but I'm now getting a rather contradictory couple of message boxes.

In the btnSaveDealSheet_Click event the connection state message box happily tells me the connection state is now open, but after trying to execute the Insert command the catch block throws me an error saying that the it cannot execute the command as the connection state is closed.

I'm at a bit of a loss on this one to be honest. Any suggestions...?

Thanks again for your help, I really do appreciate

Kind Regards
Jen
Reiss 7-Dec-11 3:26am    
The only thing I can suggest is that you update your question (using the improve question widget) to post the code for the method GetInsertDealSheetCommand() as that must be causing the issue

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