Hi!
Please could you have a look on my code. Its doesnt delete record. But still saying "Deleted". What happen?
private void btnDelete_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connstr);
comm = new SqlCommand();
conn.Open();
SqlParameter code = new SqlParameter("@cod", SqlDbType.NChar);
SqlParameter product = new SqlParameter("@prod", SqlDbType.VarChar);
SqlParameter price = new SqlParameter("@price", SqlDbType.NChar);
comm.Parameters.Add(code);
comm.Parameters.Add(product);
comm.Parameters.Add(price);
code.Value = txtcode.Text;
product.Value = txtproduct.Text;
price.Value = txtprice.Text;
comm.Connection = conn;
comm.CommandText = @"DELETE FROM company WHERE code='@cod'";
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Deleted!");
}
catch (Exception)
{
MessageBox.Show("Not deleted");
}
finally
{
conn.Close();
}
}
}
}
Ramesh MaruthiPosted Jul 22, 2014, 11:34 AM
question does the record exists in the database with that particular code value which your giving ?
And can you also try in this way as giving the parameter name and valueto gether:
SqlParameter code = new SqlParameter("@cod", txtcode.Text);
try
{
SqlConnection conn = new SqlConnection(connstr);
SqlCommand comm = new SqlCommand();
conn.Open();
SqlParameter code = new SqlParameter("@cod", SqlDbType.NChar);
SqlParameter product = new SqlParameter("@prod", SqlDbType.VarChar);
SqlParameter price = new SqlParameter("@price", SqlDbType.NChar);
comm.Parameters.Add(code);
comm.Parameters.Add(product);
comm.Parameters.Add(price);
code.Value = txtcode.Text;
product.Value = txtproduct.Text;
price.Value = txtprice.Text;
comm.Connection = conn;
comm.CommandText = @"DELETE FROM company WHERE code=@cod";
comm.ExecuteNonQuery();
MessageBox.Show("Deleted!");
}
catch (Exception)
{
MessageBox.Show("Not deleted");
}
finally
{
conn.Close();
}
VulpesPosted Jul 22, 2014, 11:29 AM
comm.CommandText = @"DELETE FROM company WHERE code='@cod'";
to this:
comm.CommandText = @"DELETE FROM company WHERE code=@cod";