Beginning to use MySQL with C#
Posted by: Gerardo Antonio Rivera Rivera
Date: October 21, 2005 10:42AM

Hi All,

I have created a database called "infrastk" with a table called "tblUno". This table has a structure as


field, type(size)
cCod, varchar(6)
cDenom, varchar(27)

I try to append a record to the table "tblUno" by means of a C# program:

using System;
using System.Data;
using MySql.Data.MySqlClient;

class MainClass
public static void Main()

// Code to connect with database and create a dataset
// and fill the dataset with the data in the table "tblUno" --

MySqlConnection myConn = new MySqlConnection(
"user id=griv; password=mypw; database=infraestructura; server=localhost");
MySqlCommand myCommand = new MySqlCommand("select * from tblUno", myConn);
MySqlDataAdapter myDA = new MySqlDataAdapter(myCommand);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "dsUno");

// Code to add a row to the table dsUno in the dataset "dsUno" --

DataRow myRow = myDS.Tables["dsUno"].NewRow();
myRow["cCod"] = "ABC01";
myRow["cDenom"] = "Machine 1";

// Code to verify that the former code had success --

foreach (DataColumn column in myDS.Tables["dsUno"].Columns)
Console.WriteLine("{0}", column.Caption);
foreach(DataTable myTable in myDS.Tables){
foreach(DataRow myRenglon in myTable.Rows){
foreach (DataColumn myColumn in myTable.Columns)

// Code to update the table "tblUno" with "dsUno" --

MySqlCommandBuilder mBuild = new MySqlCommandBuilder(myDA);
myDA.Update(myDS, "dsUno");

*** It compiles without errors but when it runs, it sends the message shown bellow indicating a big

problem to update the table "tblUno".

Machine 1

Unhandled Exception: System.InvalidOperationException: Connection must be valid
and open
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, My
SqlRowUpdatingEventArgs args)
at MySql.Data.MySqlClient.MySqlDataAdapter.OnRowUpdating(RowUpdatingEventArgs
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMappi
ng tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at in c:\aplics\estudio\dotnet\cnnet_adonet\Main.cs:line 54

at MainClass.Main() in c:\aplics\estudio\dotnet\cnnet_adonet\Main.cs:line 15

Press any key to continue . . .

*** I will appreciate any kind of help to discover where am I wrong.

Have a nice weekend!


Edited 4 time(s). Last edit at 10/21/2005 12:21PM by Gerardo Antonio Rivera Rivera.

Options: ReplyQuote

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.