Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and MySql Connector

Status
Not open for further replies.

0ddball

Technical User
Nov 6, 2003
208
GB
I'm getting a weird error that I'm note entirely sure I'm supposed to be getting.

I know that's not much help but the highlights are below.

The error I'm getting is "Connection must be valid and open" (yet it just filled the data set from the same connection).

The error is occuring when I call Update(dSet); - presumably because the MySqlCommandBuilder has fallen over or something similar.

Gosh - that's a garbled post - please wade through it and help me :)

Code:
MySqlConnection dCon = null;
		MySqlCommand dCom = null;
		MySqlDataAdapter dAda = null;
		DataSet dSet = null;

		public WebsiteManager()
		{
			InitializeComponent();

			dCon = DatabaseConnectionManager.dCon;

			dAda = new MySqlDataAdapter("SELECT websiteId, name, uri, rating, notes FROM websites", DatabaseConnectionManager.Connection_String);
			MySqlCommandBuilder mscb = new MySqlCommandBuilder(dAda);

			dSet = new DataSet();
			dAda.FillSchema(dSet, SchemaType.Source);
			dAda.Fill(dSet);

			dgWebsites.DataSource = dSet.Tables[0];
		}

		private void dgWebsites_RowLeave(object sender, DataGridViewCellEventArgs e)
		{
			dAda.Update(dSet);
		}

Code:
System.InvalidOperationException was unhandled
  Message="Connection must be valid and open"
  Source="MySql.Data"
  StackTrace:
       at MySql.Data.MySqlClient.MySqlCommand.CheckState()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommandBuilder.GenerateSchema()
       at MySql.Data.MySqlClient.MySqlCommandBuilder.OnRowUpdating(Object sender, MySqlRowUpdatingEventArgs args)
       at MySql.Data.MySqlClient.MySqlDataAdapter.OnRowUpdating(RowUpdatingEventArgs value)
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
       at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
       at TriptychContacts.WebsiteManager.dgWebsites_RowLeave(Object sender, DataGridViewCellEventArgs e) in C:\Documents and Settings\Oddball\My Documents\Visual Studio 2005\Projects\TriptychContacts\TriptychContacts\WebsiteManager.cs:line 41
       at System.Windows.Forms.DataGridView.OnRowLeave(DataGridViewCellEventArgs e)
       at System.Windows.Forms.DataGridView.OnRowLeave(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)
       at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
       at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewDataErrorContexts context, Boolean forCurrentCellChange, Boolean forCurrentRowChange)
       at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
       at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.DataGridView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TriptychContacts.Program.Main() in C:\Documents and Settings\Oddball\My Documents\Visual Studio 2005\Projects\TriptychContacts\TriptychContacts\Program.cs:line 17
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


Yet another unchecked rambling brought to you by:
Oddball
 
When you call the DataAdapter.Fill() method, the DataAdapter will always return the connection in the same state that it got it in, in this case it was closed. Some thoughts on it:


I would guess from your error (though I don't know because I don't use DataAdapters), is that the .Update method doesn't explicitly open the connection. Have you tried opening the connection (then closing it afterward) in the dgWebsites_RowLeave method?

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top