Ranthalion
February 17th, 2005, 03:42 PM
Hi, I was wondering if anyone could help me with a problem I'm having. I'm trying to connect to a remote database and just submit some select queries. It seems to work fine after it gets going, but I always get an exception stating that the Connection was unexpectedly terminated when I submit the first query. If I just submit the query again, it all works fine after that. Any ideas on what's going on?
Here is my source code:
//some of the needed variables
private MySqlConnection conn;
private DataTable data;
private MySqlDataAdapter da;
//skip some unimportant stuff
private void connectBtn_Click(object sender, System.EventArgs e)
{
if (conn != null)
conn.Close();
string connStr = String.Format("server={0};user id={1}; password=2}; database=ranthali_osc1; pooling=false",server.Text, userid.Text, password.Text );
try
{
this.Cursor = Cursors.WaitCursor;
this.databaseExplorerStatusStripPanel.Text= "Connecting to Database.";
conn = new MySqlConnection( connStr );
conn.Open();
this.databaseExplorerStatusStripPanel.Text = "Connected to " + server.Text;
this.Cursor = Cursors.Arrow;
}
catch (MySqlException ex)
{
this.databaseExplorerStatusStripPanel.Text = "Unable to connect.";
MessageBox.Show( "Error connecting to the server: " + ex.Message );
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
data = new DataTable();
da = new MySqlDataAdapter(Query.Text.ToString(), conn);
//MessageBox.Show(da.ToString());
da.Fill(data);
dataGrid.DataSource = data;
}
catch (MySqlException ex)
{
MessageBox.Show("Error: " + ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (conn!=null)
{
conn.Close();
}
}
By the way, I'm coding in C# on XP. I'm connecting to a mySQL database hosted by my webhosting provider at Acehost.net. Anyway, any ideas you all have would be great. I've been trying to figure this problem out for a couple of weeks now, and I'm stuck...
-Ranthalion
Here is my source code:
//some of the needed variables
private MySqlConnection conn;
private DataTable data;
private MySqlDataAdapter da;
//skip some unimportant stuff
private void connectBtn_Click(object sender, System.EventArgs e)
{
if (conn != null)
conn.Close();
string connStr = String.Format("server={0};user id={1}; password=2}; database=ranthali_osc1; pooling=false",server.Text, userid.Text, password.Text );
try
{
this.Cursor = Cursors.WaitCursor;
this.databaseExplorerStatusStripPanel.Text= "Connecting to Database.";
conn = new MySqlConnection( connStr );
conn.Open();
this.databaseExplorerStatusStripPanel.Text = "Connected to " + server.Text;
this.Cursor = Cursors.Arrow;
}
catch (MySqlException ex)
{
this.databaseExplorerStatusStripPanel.Text = "Unable to connect.";
MessageBox.Show( "Error connecting to the server: " + ex.Message );
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
data = new DataTable();
da = new MySqlDataAdapter(Query.Text.ToString(), conn);
//MessageBox.Show(da.ToString());
da.Fill(data);
dataGrid.DataSource = data;
}
catch (MySqlException ex)
{
MessageBox.Show("Error: " + ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (conn!=null)
{
conn.Close();
}
}
By the way, I'm coding in C# on XP. I'm connecting to a mySQL database hosted by my webhosting provider at Acehost.net. Anyway, any ideas you all have would be great. I've been trying to figure this problem out for a couple of weeks now, and I'm stuck...
-Ranthalion