Hi, all
I started programming in C#. I have problem: I created "Projects" table with 2 rows which are programmed in
get; set;
class: ID, PROJECTS_NAME. I have already done with editing values in this table:
private void dataGridView4_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string name_project = dataGridView4.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
string id = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();
MySqlConnection connection = new MySqlConnection("datasource = localhost; port = 3306; username = root; password = ");
MySqlCommand cmd = new MySqlCommand("UPDATE projekt1.projects SET PROJECTS_NAME = @PROJECTS_NAME WHERE ID = @ID", connection);
cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = int.Parse(id);
cmd.Parameters.Add("@PROJECTS_NAME", MySqlDbType.VarChar).Value = name_project;
try
{
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
SQL Columns in projects Table:
PROJECT_NAME VARCHAR
ID INT
And it works fine. But i have a problem with inserting new row and saving automatically in database.
What I have tried:
I've tried with RowValidated Event:
private void dataGridView4_RowValidated(object sender, DataGridViewCellEventArgs e)
{
string name_project = dataGridView4.Rows[e.RowIndex].Cells[1].Value.ToString();
string id = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();
MySqlConnection connection = new MySqlConnection("datasource = localhost; port = 3306; username = root; password = ");
MySqlCommand cmd = new MySqlCommand("INSERT INTO projekt1.projects(PROJECT_NAME) VALUES(@NPROJECT_NAME);", connection);
cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = int.Parse(id);
cmd.Parameters.Add("@PROJECT_NAME", MySqlDbType.VarChar).Value = name_project;
try
{
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("ok");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
But it didn't work because of
System.FormatException: "Invalid input string format."
Can someone please explain how to solve it? I've searched something but i don't know how to deal with. Greetings