Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Beginning with MySQL and C#
Message
From
21/10/2005 14:42:44
 
 
To
All
General information
Forum:
ASP.NET
Category:
ADO.NET
Title:
Beginning with MySQL and C#
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP4
Network:
Windows 2000 Server
Database:
MySQL
Miscellaneous
Thread ID:
01061227
Message ID:
01061227
Views:
53
Hi All,

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

follows:

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=andromeda; 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";
		myDS.Tables["dsUno"].Rows.Add(myRow);

		// 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)
					Console.WriteLine(myRenglon[myColumn]);
			}
		}

		// 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":
cCod
cDenom
ABC01
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
 value)
   at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMappi
ng tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at ManejoDatos.uno() 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!

griv
Next
Reply
Map
View

Click here to load this message in the networking platform