Click here to Skip to main content
15,891,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void button4_Click(object sender, EventArgs e)
        {
            if (bar_code1DataGridView.SelectedRows.Count > 0)
            {

                DataGridViewRow dr = bar_code1DataGridView.SelectedRows[0];
                if (!string.IsNullOrEmpty(dr.Cells["dataGridViewTextBoxColumn1"].Value.ToString()))
                {

                    String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=barcode;Integrated Security=True";
                    using (SqlConnection openCon = new SqlConnection(cs))
                    {


                        using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
                        {

                            // querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;
                            querySaveStaff.Parameters.AddWithValue("@id", 0);
                            
                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            openCon.Close();

                        }

                    }

                    using (SqlConnection openCon = new SqlConnection(cs))

                    {

                        string saveStaff = "SELECT * FROM dbo.bar_code1 ";
                        openCon.Open();
                        using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))
                        {
                            querySaveStaff.Fill(barcodeDataSet.bar_code1);
                            bar_code1DataGridView.DataSource = barcodeDataSet.bar_code1;
                            bar_code1DataGridView.Update();
                            bar_code1DataGridView.Refresh();
                        }

                    }
                }
            }
        }<pre lang="text">


What I have tried:

Whats wrong? I can not delete the row with button click
Posted
Updated 25-Jan-18 23:10pm

Probably, it's that your ID value is fixed:
C#
using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
    {
    querySaveStaff.Parameters.AddWithValue("@id", 0);
Once you delete that row, you can't delete it again.
And as for your commented out code:
C#
querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;
That's not necessarily going to work either.
Instead, use int.TryParse at the top of your method to check and validate the user input, then use the converted value as the parameter.
C#
int id;
if (!int.TryParse(idTextBox.Text, out id))
   {
   ... report problem to user ... 
   return;
   }
...
querySaveStaff.Parameters.AddWithValue("@id", id);

But please, sort out your variable names! "querySaveStaff" deleting data? Not an obvious bit of self documentation!
 
Share this answer
 
private void button4_Click(object sender, EventArgs e)
       {
           int id;
           if (bar_code1DataGridView.SelectedRows.Count > 0)
           {

               DataGridViewRow dr = bar_code1DataGridView.SelectedRows[0];
               if (!int.TryParse(idTextBox.Text, out id))
               {

               String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=barcode;Integrated Security=True";
                   using (SqlConnection openCon = new SqlConnection(cs))
                   {


                       using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
                       {

                           // querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;
                           querySaveStaff.Parameters.AddWithValue("@id", id);

                           openCon.Open();
                           querySaveStaff.ExecuteNonQuery();
                           openCon.Close();

                       }




               }



               using (SqlConnection openCon = new SqlConnection(cs))

                   {

                       string saveStaff = "SELECT * FROM dbo.bar_code1 ";
                       openCon.Open();
                       using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))
                       {
                           querySaveStaff.Fill(barcodeDataSet.bar_code1);
                           bar_code1DataGridView.DataSource = barcodeDataSet.bar_code1;
                           bar_code1DataGridView.Update();
                           bar_code1DataGridView.Refresh();
                       }

                   }
               }


                       MessageBox.Show("Dont Work ");
                   return;


           }
       }
 
Share this answer
 
Comments
Goran Bibic 26-Jan-18 4:11am    
This dont work
Karthik_Mahalingam 26-Jan-18 4:33am    
which is the id column in the table?
are you getting any error ?
Goran Bibic 26-Jan-18 13:30pm    
No have error, id is zero location
Maciej Los 11-Mar-18 16:07pm    
This is not an answer. Please, delete it to avoid down-voting. To post comment, use "Have a Question or Comment" widget.

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