Click here to Skip to main content
14,732,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 28-Feb-19 5:02am

1 solution

You have to use
UserAddedRow
insted of
RowValidated


 private void dataGridView4_UserAddedRow(object sender, DataGridViewRowEventArgs e)
     {
         string name_project = e.Row.Cells.Cells[1].Value.ToString();
         int id = Convert.ToInt64(e.Row.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 = 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);
      }   
}
   
v2
Comments
Member 10696161 28-Feb-19 13:33pm
   
I've tried by UserAddedRow event:

private void dataGridView4_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {

            string name_project= e.Row.Cells[1].Value.ToString();
            int id = (int)Convert.ToInt64(e.Row.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 = 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);
            }
        }


but have exception in below
System.NullReferenceException: The object reference has not been set to the instance of the object. System.Windows.Forms.DataGridViewCell.Value.get returned null.”
Richard Deeming 5-Mar-19 14:18pm
   
Try removing the following two lines:
int id = (int)Convert.ToInt64(e.Row.Cells[0].Value.ToString());

cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = id;

You're not inserting the ID, so you don't need a parameter for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900