3 Replies - 522 Views - Last Post: 03 January 2013 - 09:44 AM Rate Topic: -----

#1 NejcZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-December 12

Data won't save to database, C#

Posted 03 January 2013 - 08:48 AM

Hi, with your previus help, I managed to get most of the things sorted out. now there is a problem with my DataGridView. When I update row in DataGridView, it stays there until i close the program, so I suppose, changes are only made to loaded DataTable. I searched alot for the solution on the internet, but nothing worked.

Here is the update function (only updating one row at the moment):
void update(int row)
        {
            DataGridViewCell cell = dataGridView2.CurrentCell as DataGridViewCell;
            if (cell.IsInEditMode)
            {
                string query;
                sqlcon.Open();
                query = "update tabelaZaposlenih set ime='" + dataGridView2.Rows[row].Cells[1].EditedFormattedValue + "'";
                sqlcmd = new SqlCommand(query, sqlcon);
                // i added this line afterwards, but no joy...
                sqlcmd.Parameters.AddWithValue("ime", dataGridView2.Rows[row].Cells[1].EditedFormattedValue);
                sqlcmd.ExecuteNonQuery();
                sqlcon.Close();
                MessageBox.Show("Podatki uspešno posodobljeni", "Podatki uporabnika");
            }
        }



What am I doing wrong this time? :) Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Data won't save to database, C#

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Data won't save to database, C#

Posted 03 January 2013 - 09:21 AM

Are you using a file based database?
Was This Post Helpful? 0
  • +
  • -

#3 trichardson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 9
  • Joined: 02-December 12

Re: Data won't save to database, C#

Posted 03 January 2013 - 09:42 AM

1. I would remove line #11 since you are not using a stored procedure to update the database.
2. Make sure
void update (int)
is actually being called when the time comes (perhaps through an event?).
3. "When I update row in DataGridView, it stays there until i close the program, so I suppose, changes are only made to loaded DataTable." What do you mean by "stays there?"
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,845
  • Joined: 08-June 10

Re: Data won't save to database, C#

Posted 03 January 2013 - 09:44 AM

You're using parameters incorrectly, for one thing. You're doing string concatenation as well as adding parameters. What you should be doing is something more like this:

var imeValue = dataGridView2.Rows[row].Cells[1].EditedFormattedValue;
query = "update tabelaZaposlenih set ime=@ime";
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.Parameters.AddWithValue("@ime", imeValue);


That's a more correct and safe query. Whether or not that will make your code work, I don't know, because you could have connection problems, or other issues. But it's the correct way to use parameters in a query.

View Posttrichardson, on 03 January 2013 - 10:42 AM, said:

1. I would remove line #11 since you are not using a stored procedure to update the database.


One has nothing to do with the other. Parameters aren't just for stored procedures, they're the right way to do any query that needs input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1