Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
,if i delete a single row in the data grid view mean that deleted row  will remove from the grid view, but i have this error "ExecuteNonQuery: CommandText property has not been initialized"  


What I have tried:

       string update="";
       DateTime now = DateTime.Now;
       private void delete()
       {
           try
           {
               if ((gvClass.Rows.Count > 0) || gvClass.CurrentCell.ColumnIndex < 1)
               {
                   search = Convert.ToString(gvClass.CurrentRow.Cells[0].Value.ToString());
               }
               if (MessageBox.Show("Do you want to Delete?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
               {
               }
               else
               {
                   con.Open();
                   string query1 = "select * from tblClass  WHERE  ID LIKE '" + search + "' and status=1  order by ID ";
                   SqlCommand cmd1 = new SqlCommand(query1, con);
                   SqlDataReader dr1 = cmd1.ExecuteReader();
                   if (dr1.Read())
                   {
                       string f = dr1["ID"].ToString();
                       if (search == f)
                       {
                          update    = "update tblClass set status=0,delete_time=convert(datetime,'" + now + "', 103) where  ID='" + search + "' ";
                       }
                   }

                   dr1.Close();
                   cmd1.ExecuteNonQuery();

                   con.Close();

                   con.Open();
                   cmd = new SqlCommand(update, con);
                   cmd.ExecuteNonQuery();
                   con.Close();
                   MessageBox.Show("Deleted SuccessFully");
                   view();


               }
           }
           catch (Exception ex)
           {
               throw ex;

           }

       }


private void view()
       {
           string view ="";
           con.Open();
           SqlDataAdapter da =new SqlDataAdapter("select * from tblClass where status =1",con);
           DataSet ds = new DataSet ();
           this.gvClass.Rows.Clear();
           gvClass.Rows.Clear();
           da.Fill(ds);
           DataTable dt =ds.Tables[0];

           if (dt.Rows.Count != 0)
           {
               gvClass.Rows.Add(dt.Rows.Count);
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   for (int i1 = 0; i1 < gvClass.Rows.Count - 0; i1++)
                   {
                       gvClass.Rows[i1].Cells[0].Value = (i1 + 1).ToString();
                   }
                   gvClass.Rows[i].Cells["Id"].Value = Convert.ToString(dt.Rows[i]["Id"].ToString());
                   gvClass.Rows[i].Cells["Name"].Value = Convert.ToString(dt.Rows[i]["Name"].ToString());

               }
           }
               con.Close();

           }
Posted
Updated 20-Sep-16 3:58am
v3
Comments
Vjay Y 20-Sep-16 7:38am    
As every problem comes with solution, carefully look at the exception. It is stating that 'CommandText property has not been initialized' which means the command text is passed as null or empty value.

I hope the error will be on this set of codes
Quote:
if (search == f)
{
update = "update tblClass set status=0,delete_time=convert(datetime,'" + now + "', 103) where ID='" + search + "' ";
}
}
dr1.Close();
cmd1.ExecuteNonQuery();
con.Close();
con.Open();
cmd = new SqlCommand(update, con);
cmd.ExecuteNonQuery();

_______________________________________________________________________________________

If the Condition (search == f) doesnt get satisfy then the update variable will be empty
so when you try to execute an empty string it will lead to the error you have mentioned in the post.

Move all the codes inside the condition and try.

caution: Never concatenate the sql statements, it will lead to sql injectio[^]n attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 
If you have AllowUserToAddRows property enabled on your DataGridView then you might be accidently deleting the empty row at the bottom of the DataView which is a placeholder for the next user created row. Try disabling this option if not required, otherwise try using code like this.

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    if(!row.IsNewRow)
       dataGridView1.Rows.Remove(row);
}

SqlConnection con = new SqlConnection(ConString);
//create and initialise connection object properly.

open and close SQL connection and DataReader object properly.

if ((rdr != null))
                            { rdr.Close(); }

                            if (con.State == ConnectionState.Open)
                            { con.Close(); }

carefully look at the exception and your logic. and CommandText property initialized properly.
 
Share this answer
 
v3

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