update delete table data in wpfcode
Loading
update delete table data in wpfcode
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted May 12, 2023, 3:24 AM EDT
To update or delete table data in WPF using code, you need to perform the following steps:
Naimish MakwanaRank #324 · 7.7K reputationPosted May 12, 2023, 6:46 AM EDT
To update or delete table data in WPF (Windows Presentation Foundation) code, you can use ADO.NET or an Object-Relational Mapping (ORM) tool such as Entity Framework. Here is an example using ADO.NET:
private void UpdateOrDeleteData(bool isUpdate, int id, string newValue) { string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"; string commandText = isUpdate ? "UPDATE TableName SET Column1=@Value1 WHERE ID=@ID" : "DELETE FROM TableName WHERE ID=@ID"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.AddWithValue("@ID", id); if (isUpdate) { command.Parameters.AddWithValue("@Value1", newValue); } connection.Open(); command.ExecuteNonQuery(); connection.Close(); } }To update data, call the method with
isUpdateset totrue, and provide theidof the row to update and the new value forColumn1:UpdateOrDeleteData(true, 1, "NewValue");To delete data, call the method with
isUpdateset tofalseand provide theidof the row to delete:UpdateOrDeleteData(false, 1, null);Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 11:21 PM EDT
You can update and delete table data in WPF code:
To update table data in WPF code, you can use the following steps:
// Get the data row that you want to update. DataGridViewRow row = dataGridView1.Rows[0]; // Set the values of the data row's properties to the new values. row.Cells["Name"].Value = "John Doe"; row.Cells["Address"].Value = "123 Main Street"; // Call the DataGridView.Update() method. dataGridView1.Update();Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 11:22 PM EDT
To delete table data in WPF code, you can use the following steps:
Here is an example of the code:
// Get the data row that you want to delete. DataGridViewRow row = dataGridView1.Rows[0]; // Call the DataGridView.Rows.Remove() method. dataGridView1.Rows.Remove(row);You can modify this code to fit your specific needs. For example, you could delete multiple rows at once, or you could delete all rows in the DataGridView.
David Brown28 reputationPosted Jul 11, 2023, 10:10 AM EDT
Install the necessary NuGet packages:
System.Data.SqlClientfor connecting to SQL Server.System.Configuration.ConfigurationManagerfor accessing the connection string from the configuration file.Add a connection string to your App.config or Web.config file:
Create a class to handle database operations:
using System; using System.Configuration; using System.Data.SqlClient; public class DatabaseHelper { private static string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; public static void UpdateData(string tableName, string columnToUpdate, string newValue, string condition) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = $"UPDATE {tableName} SET {columnToUpdate} = @newValue WHERE {condition}"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@newValue", newValue); command.ExecuteNonQuery(); } } } public static void DeleteData(string tableName, string condition) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = $"DELETE FROM {tableName} WHERE {condition}"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.ExecuteNonQuery(); } } } }In your WPF code, you can call the methods from the DatabaseHelper class to update or delete data:
DatabaseHelper.UpdateData("YourTableName", "ColumnName", "NewValue", "Condition"); DatabaseHelper.DeleteData("YourTableName", "Condition");Replace "YourServer" with the server name, "YourDatabase" with the database name, "YourTableName" with the table name, "ColumnName" with the column to update, "NewValue" with the new value, and "Condition" with the condition for update or delete operation.