Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create "Delete button for deleting grid columns from SQL Server database in C#"?

Tags:

c#

sql-server

I need a part of the code where I can delete a grid column or row from the database by clicking a delete button, and it should be done with C#.

I tried deleting it by MVVM architecture and I believe I wrote something wrong. Is there any other way to do this?

public void DeleteAuction()
{
     using (SqlConnection conn = new SqlConnection())
     {
         conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
         conn.Open();

         SqlCommand command = new SqlCommand("UPDATE AuctionTbl2 SET deleted = 1 WHERE id = @Id", conn);

         SqlParameter myParam = new SqlParameter("@Id", SqlDbType.Int, 11);
         myParam.Value = this.Id;

         command.Parameters.Add(myParam);

         int rows = command.ExecuteNonQuery();
     }
}

Now I made constructors, properties and fields, and that works okay. I tried to connect data from SQL Server to C#, that worked okay as well, but when I press the delete button it doesn't work. No errors, no nothing. Please help, I'm getting very desperate now.

[This is when delete is clicked, it should go in DB from 0 --> 1 for deleted[1]

Like this

like image 370
xVenum.dll Avatar asked Dec 09 '25 22:12

xVenum.dll


1 Answers

UPDATE: You should try this code, I have tested it personally at it is running like a charm. Do tell me if it gets you what you want.

public void DeleteAuction()
{
    try
    {
        bool isSuccess = false;
        DataTable dt = new DataTable(); 
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            conn.Open();
            SqlCommand command = new SqlCommand("UPDATE AuctionTbl2 SET deleted = 1 WHERE id = @Id", conn);
            command.Parameters.AddWithValue("@Id",Id);
            int rows = command.ExecuteNonQuery();
            if(rows>0)
            {
                MessageBox.Show("Deletion Successfull");
                dt = selectAuction();
                auctionDataGridView.DataSource = dt;
            }
            else
                MessageBox.Show("Deletion Unsuccessfull");
        }
    }
}
catch(Exception e)
{
    MessageBox.Show(e.Message);
}

//method to update the dataGridView after deletion
public DataTable selectAuction()
{
    try
    {
        DataTable table = new DataTable(); 
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            conn.Open();
            String sql = "select * from AuctionTbl2";          
            SqlCommand cmd = new SqlCommand(sql, con);         
            SqlDataAdapter adaptor = new SqlDataAdapter(cmd);                                         
            adaptor.Fill(table);    
        }               
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);                 
    }
    finally
    {
        con.Close();                 
    }
    return table;
}

NOTE: I have tried to use variables just like yours, but if there is one sneaky different variable name, then pardon me.

like image 132
Zain Arshad Avatar answered Dec 12 '25 11:12

Zain Arshad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!