Click here to Skip to main content
16,017,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have 2 forms, Form A has text boxes which adds data to the Mysql database.

Form B includes a flexgrid.

When I edit a row in the flexgrid it opens Form A but when I save, it creates another record. I want it to update existing record. On my save button I have

C#
private void lnkSave_Click(object sender, EventArgs e)
        {
             string constring = "datasource=127.0.0.1;Port=3306;Database=bpos;username=root;password=Pass";
            string Query = "insert into bpos.patchtest (Description,Employee,MemberID,Results,Date) values('" + this.txtDescription.Text + "','" + this.cmbEmployee.Text + "','" + this.txtRefNo2.Text + "','" + Results + "','" + this.metroDateTime1.Text + "') ;";
    
            MySqlConnection conDataBase = new MySqlConnection(constring);
             MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
              MySqlDataReader myReader;
             try
             {
               conDataBase.Open();
              myReader = cmdDataBase.ExecuteReader();
               MessageBox.Show("Saved");
               
               while (myReader.Read())
                {

                 }

              }
             catch (Exception ex)
               {

                   MessageBox.Show(ex.Message);
               }
              }


I don't want to create an Update button. Is it possible to Update using above save button?

What I have tried:

I tried using an update button but I really don't want to use another button.
Posted
Updated 4-Oct-16 8:07am
Comments
[no name] 4-Oct-16 14:24pm    
"Is it possible", yes it is possible.

1 solution

Yes - but first don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

You can modify rows just by issuing an UPDATE query instead of an INSERT: INSERT always creates a new row, UPDATE only ever changes existing rows.
The rough syntax is:
SQL
UPDATE MyTable SET MyColumn = MyNewValue, MyOtherColumn = MyOtherNewValue WHERE MyROwIDColumn=TheIDOfTheColumnToChange


BTW do yourself a favour: INSERT and UPDATE commands do not return any row data - so using a DataReader is pointless. Instead, these type of commands should be issued with ExecuteNonQuery - which returns the number of rows inserted or changed instead.

And seriously: Go through all your code and get rid of string concatenation - it is really dangerous!
 
Share this answer
 
Comments
Member 10627757 4-Oct-16 14:24pm    
I am totally new to c# and I am following youtube videos and that is the way he has it done. Thanks for your advice and I will read up on it. When you say Never concatenate strings could you give me an example of what it shoud look like
OriginalGriff 4-Oct-16 14:31pm    
Don't follow YouTube videos - most of them are produced by people who know very little more than you do, and they normally miss out huge chunks you really do need! Get a book, or better go on a course - both will introduce everything you need in a structured manner. And do all the exercises: you "fix" things in your mind better by doing than you reading.
Have a look at this: it explains why and how to parameterize queries.

http://www.codeproject.com/Articles/837599/Using-Csharp-to-connect-to-and-query-from-a-SQL-da
Member 10627757 4-Oct-16 14:49pm    
UPDATE MyTable SET MyColumn = MyNewValue, MyOtherColumn = MyOtherNewValue WHERE MyROwIDColumn=TheIDOfTheColumnToChange



Will update add data to the Mysql database if record does not exist. This form is also used to insert new data.

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