daniel castellanos
daniel castellanos

Reputation: 51

Updating Database Value in windows forms

I am trying to do a function to update a value in a SQL database, this is my table enter image description here

enter image description here

This is the function

private void UpdateGanToDB(float entrada, string Id)
        {
            string string_entrada = entrada.ToString();
            string conString = Properties.Settings.Default.LocalDataBaseConnectionString;
            string command_string = "UPDATE Gan SET Ganan = @GetGan WHERE Id = @GetId";
            SqlConnection connection = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(command_string, connection);
            cmd.Parameters.AddWithValue("@GetGan", string_entrada);
            cmd.Parameters.AddWithValue("@GetId", Id);
            try
            {
                connection.Open();
                int row = cmd.ExecuteNonQuery();
                if (row > 0) {
                    MessageBox.Show("Upgrade successful");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }

I am getting the message "upgrade successful" but when I check the database I can't see the changes. When I run the text in the command_string with known values I can see the changes but not with my function

Edit: I added this code to the code above

int row = cmd.ExecuteNonQuery();
                if (row > 0) {
                    SqlCommand command = new SqlCommand("SELECT * FROM Gan ", connection);
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    System.Data.DataTable dataTable = new System.Data.DataTable();
                    adapter.Fill(dataTable);
                    MessageBox.Show(dataTable.Rows[0]["Ganan"].ToString());
                }

To see if it is updating the value and I get the excepted 200 (Original value was 0) But When I close the application and see the values in the local database I see the original value 0 I don't know why the update is not saving

enter image description here enter image description here

EDIT2: I found another post with a work around for this problem: Can I commit changes to actual database while debugging C# in Visual Studio?

Upvotes: 0

Views: 662

Answers (1)

Hesam Akbari
Hesam Akbari

Reputation: 1141

I think your code is correct and there is no problem

If you created database via Add\NewItem\Service-Base Database

I'm not sure, but I think After running your project, one copy of your original database will be included in the debug folder in your project and the update operation and also other operations run on that, not your original database

so, if you want to see a result, you should connect to that via View\ServerExplorer

meantime, after Change your Code and rebuild your Project, your debug database will be deleted and again one copy of your original database will be included in the debug folder

Upvotes: 2

Related Questions