Below is the code to insert value into mysql database, using datagridview. But the selectcommand is working.It is not happening since i get error stating "Column 'username' cannot be null ".
This error does not pop up if i use ms access database. Can anyone help me on this. is there any other method to do so.
private void button1_Click(object sender, EventArgs e)
{ //Even using functions we can easily update the datagridview
//Select_function();
try
{ //The container which displays the details.
dataGridView1.Visible = true;
//The binding object which binds the datagridview with backend.
BindingSource bs = new BindingSource();
//The datatable through which data is exported to datagridview
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);
//There is issue in below sytax of insert command.
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;
//Demonstrates update command
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;
//Demonstration of delete Command
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);
}