-2

I am using Visual Studio 2019 Winforms C# .NET Framework and in the Winforms project, there is a textbox and a button.

When I type a parameter name in the textbox and click the button, I want to delete the row from the table called "Serial_Key".

private void button1_Click(object sender, EventArgs e)
{
    string mainconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
    SqlConnection sqlconn2 = new SqlConnection(mainconn);
    string sqlquery = "select * from [dbo].[Serial-Keys] where Serial_Key=@Serial_Key";
    sqlconn2.Open();
    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn2);
    sqlcomm.Parameters.AddWithValue("@Serial_Key", SerialKeyBox.Text);
    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    sqlcomm.ExecuteNonQuery();
    if (dt.Rows.Count > 0)
    {
        Cursor.Current = Cursors.WaitCursor;
        try
        {
            
        }
        catch (Exception)
        {

        }
        Cursor.Current = Cursors.Default;
    }
    else
    {
        MessageBox.Show("Invalid Serial Key!");
        label7.Text = "Serial Key Rejected...";
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
TERIHAX
  • 237
  • 2
  • 14

2 Answers2

1

Change your select statement to a delete statement and remove all the DataAdaptor stuff as thats only required if you are querying records.

private void button1_Click(object sender, EventArgs e)
{
    string mainconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
    SqlConnection sqlconn2 = new SqlConnection(mainconn);
    // Use a Delete statement, not a select
    string sqlquery = "delete from [dbo].[Serial-Keys] where Serial_Key = @Serial_Key";
    sqlconn2.Open();
    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn2);
    // Construct the parameter yourself with the correct datatype and precision
    sqlcomm.Parameters.Add(new SqlParameter("@Serial_Key", SqlDbType.VarChar, 32) { Value = SerialKeyBox.Text });
    // Remove all the DataAdaptor stuff
    // ExecuteNonQuery returns the rows affected
    int numberOfRecords = sqlcomm.ExecuteNonQuery();
    if (numberOfRecords > 0)
    {
        // Any code to run on an effective delete
    }
    else
    {
        MessageBox.Show("Invalid Serial Key!");
        label7.Text = "Serial Key Rejected...";
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
1

If you install Dapper then your code becomes very simple:

private async void button1_Click(object sender, EventArgs e)
{
  using(var c = new SqlConnection(_connStr))
    await c.ExecuteAsync("DELETE FROM dbo.[Serial-Keys] WHERE serial_key = @sk", new { sk = SerialKeyBox.Text });
}

And, bonus, it doesn't jam your UI while it runs queries

I recommend you put that string mainconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString; into a class level variable called _connStr instead, to help tidy things up

http://dapper-tutorial.net (no affiliation)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • the problem is I don't want any DLL files with it. But anyway `@Dale K` fixed my problem. – TERIHAX Aug 01 '20 at 08:39
  • "I don't want any DLL files with it" - better not use .net framework then; need to start work on writing everything from scratch, database drivers, graphics drivers, the lot because you're using System.Data.dll right now even... Or, [embed the DLLs into the EXE](https://stackoverflow.com/questions/189549/embedding-dlls-in-a-compiled-executable) if you're looking for some way of creating a self contained single exe, but want to leverage code that is already written – Caius Jard Aug 01 '20 at 08:51
  • `@Caius Jard` Like I mean no third-party dlls. ONLY microsoft – TERIHAX Aug 01 '20 at 13:01
  • Because Microsoft never use third party DLLs? – Caius Jard Aug 01 '20 at 14:13
  • I mean the built-in dlls – TERIHAX Aug 02 '20 at 06:05