Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
when i click delete button then shows error enable to cast an data object
so please solve this error

What I have tried:

C#
private void DeleteData_OnClick(object sender, RoutedEventArgs routedEventArgs)
{
    var selectedRow = MyDataGrid.SelectedItem;
    BookingRequest request = selectedRow as BookingRequest;
    long id = request.ID;
    con.Open();
    string query = "Delete from BookingRequest where ID ='"+ id +"'";
    SqlCommand sqlcmd = new SqlCommand(query, con);
    sqlcmd.ExecuteNonQuery();
    MessageBox.Show("Data deleted successfully");
    con.Close();
    Load();
}
Posted
Updated 4-Apr-19 8:15am
v2
Comments
Suvendu Shekhar Giri 30-Mar-19 4:46am    
Please share the piece of relevant code so that we can help.
Richard MacCutchan 30-Mar-19 5:32am    
We cannot solve it since you have not provided any useful information. Please provide the code that causes the error and the exact error message.
NaeemSardar.ifish 30-Mar-19 8:19am    
best of luck happy coding

C#
query = "Delete from BookingRequest where ID ='"+ id +"'";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
private void DeleteData_OnClick(object sender, RoutedEventArgs routedEventArgs)
          {
              var selectedRow = MyDataGrid.SelectedItem;
              BookingRequest request = selectedRow as BookingRequest;
              long id = request.ID;
              con.Open();
              string query = "Delete from BookingRequest where ID ='"+ id +"'";
              SqlCommand sqlcmd = new SqlCommand(query, con);
              sqlcmd.ExecuteNonQuery();
              MessageBox.Show("Data deleted successfully");
              con.Close();
              Load();
                 }
 
Share this answer
 
Comments
Richard MacCutchan 30-Mar-19 8:51am    
A few things:
1. You should use parameterised queries for SQL in order to avoid SQL injection.
2. Do not put out a success message without testing the result of the actual SQL command.
3. And although you have still not shown the error message, I suspect you cannot cast a DataGridRow to a BookingRequest.
4. Please do not post supplementary information as a solution, use the Improve question link above to update your question.

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