Below is the code to insert the value into mysql database using datagridview. But the select command works. This does not happen because I get an error: "Column name" cannot be empty. "
This error does not appear if I use ms access database. Can anyone help me on this. is there any other way to do this.
private void button1_Click(object sender, EventArgs e)
{
try
{
dataGridView1.Visible = true;
BindingSource bs = new BindingSource();
table = new DataTable();
bs.DataSource = table;
this.dataGridView1.DataSource = bs;
MySqlConnection conn = new MySqlConnection(db);
conn.Open();
string s = "select *from user";
cmd = new MySqlCommand(s, conn);
da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand(s, conn);
MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(@username ,@password)", conn);
insertcommand.Parameters.Add("username",MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add("password", MySqlDbType.VarChar, 50, "password");
da.InsertCommand = insertcommand;
MySqlCommand updatecommand = new MySqlCommand("update user set username=@username,password=@password where (username=@username)", conn);
updatecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
updatecommand.Parameters.Add("@password", MySqlDbType.VarChar, 50, "password");
da.UpdateCommand = updatecommand;
MySqlCommand deletecommand = new MySqlCommand("delete from user where username=@username", conn);
deletecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
da.DeleteCommand = deletecommand;
da.Fill(table);
conn.Close();
}
catch (Exception err) { MessageBox.Show(err.Message); }
}
private void button2_Click(object sender, EventArgs e)
{ da.Update(table);
}

source
share