How can i read only one record, when i want all records its fine but when i need a specific record with @id, my while loop jums out ?
[HttpGet]
public ActionResult DeleteArticle(int ProductID)
{
int id = ProductID;
if (ModelState.IsValid)
{
string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.Open();
//delete from database
using (SqlCommand command = new SqlCommand("DELETE FROM MyTable WHERE id = @id", connection))
{
command.Parameters.AddWithValue("@id", id);
command.ExecuteNonQuery();
}
//read imagePathe from Database from database
using (SqlCommand command = new SqlCommand("SELECT * FROM MyTable WHERE id = @id", connection))
{
command.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) // --> here it skips while loop ????
{
string ImagePath = (string)reader["DbImagePath"];
}
}
}
}
return RedirectToAction("Index", "Admin");
}
while (reader.Read()) { }will break when there are no more records to read. Since your first command deletes the record identified byid, there will never be anything to read with that id.