Re: Updating A Table With A DataSet
Posted by: Hauke Betz
Date: September 20, 2006 12:38AM

Hello,

Are you sure, your dataset is filled correctly (where is the command "mySqlDataAdapter.Fill(dataSetFormats), you may use an empty dataset with the same name as the dataset populated by the other function)

To see how DataSets/DataTables/DataAdapters work, you may want to make a small WinApp with a Form and a DataGridView with the following code (I use e DataTable and just deleted the code not necessary for this demo):

public partial class frmTestForm : Form
{
private DataTable dtbData = new DataTable();
private MySqlDataAdapter dbaData = new MySqlDataAdapter ();
private MySqlCommand dbcSelect, dbcInsert, dbcUpdate, dbcDelete;

.
.
.

private void frmTestForm _Load(object sender, EventArgs e)
{
// Configure the MySQLCommands
dbcSelect = new MySqlCommand("SELECT FID,Name FROM Media_Formats", mySqlConnection);
dbcInsert = new MySqlCommand("INSERT INTO Media_Formats (Name) VALUES (?tmpName)", mySqlConnection);
dbcUpdate = new MySqlCommand("UPDATE Media_Formats SET Name=?tmpName WHERE FID=?tmpFID", mySqlConnection);
dbcDelete = new MySqlCommand("DELETE FROM Media_Formats WHERE FID=?tmpFID", mySqlConnection);

// configure the DataAdapter
dbaData.SelectCommand = dbcSelect;
dbaData.InsertCommand = dbcInsert;
dbaData.UpdateCommand = dbcUpdate;
dbaData.DeleteCommand = dbcDelete;

// Fill the DataTable and GridView
dbaData.Fill(dtbData);
this.dgrData.AutoGenerateColumns = false;
this.dgrData.Columns["colID"].DataPropertyName = "fid";
this.dgrData.Columns["colName"].DataPropertyName = "Name";
this.dgrData.DataSource = dtbData;

// Define the Parameters for the SQL-Statement
dbcInsert.Parameters.Add("?tmpName", MySqlDBType.VarChar, 100, "Name");
dbcInsert.Parameters.Add("?tmpName", MySqlDBType.VarChar, 100, "Name");
dbcUpdate.Parameters.Add("?tmpID",MySqlDBType.Int, 32,"fid");
dbcDelete.Parameters.Add("?tmpID",MySqlDBType.Int, 32,"fid");
}

.
.
.

private bool SaveData()
{
// Called by the button 'btnSave_Click()'
try
{
dbaData.Update(dtbData);
return true;
}
catch (MySqlException ExMySQL)
{
MessageBox.Show(ExMySQL.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
catch (Exception exOther)
{
MessageBox.Show(exOther.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
}


Greets,

Hauke



Edited 1 time(s). Last edit at 09/20/2006 12:38AM by Hauke Betz.

Options: ReplyQuote


Subject
Written By
Posted
September 18, 2006 03:46PM
Re: Updating A Table With A DataSet
September 20, 2006 12:38AM


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.