I am working on a project(not public) and I am trying to update my Database code using ADO.NET. I have written working code for insert, retrieve all, retrieve by ID, and retrieve by status, the one I can't seem to figure out is the Update. I've done quite a bit of searching and the code I have is representative of the information I have found and tried adapting to my program. I am just using garbage data in order to try and see the update before using any sort of tangible data.
Below is my query code inside a Class dedicated for queries.
public Ticket UpdateTicket(int id, string status, int customerId, int helpDeskStaffId, string problemDesc, string resolution, string followUpRequired, string followUpComplete, DateTime ticketDate, DateTime resolvedDate)
{
Ticket ticket = new Ticket(id, status, customerId, helpDeskStaffId, problemDesc, resolution, followUpRequired, followUpComplete, ticketDate, resolvedDate);
SqlCommand cmdInsert;
SqlConnection conn = null;
try
{
string connectString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
conn = new SqlConnection(connectString);
"UPDATE Ticket SET Status = @Status, HelpDeskStaffId = @HelpDeskStaffId, ProblemDesc = @ProblemDesc, Resolution = @Resolution, FollowUpRequired = @FollowUpRequired, FollowUpComplete = @FollowUpComplete, TicketDate = @TicketDate, ResolvedDate = @ResolvedDate, CustomerId = @CustomerId WHERE TicketId=@id";
conn.Open();
cmdInsert = new SqlCommand(sql2, conn);
cmdInsert.Parameters.AddWithValue("@id", id);
cmdInsert.Parameters.AddWithValue("@Status", status);
cmdInsert.Parameters.AddWithValue("@HelpDeskStaffId", helpDeskStaffId);
cmdInsert.Parameters.AddWithValue("@ProblemDesc", problemDesc);
cmdInsert.Parameters.AddWithValue("@Resolution", resolution);
cmdInsert.Parameters.AddWithValue("@FollowUpRequired", followUpRequired);
cmdInsert.Parameters.AddWithValue("@FollowUpComplete", followUpComplete);
cmdInsert.Parameters.AddWithValue("@TicketDate", ticketDate);
cmdInsert.Parameters.AddWithValue("@ResolvedDate", resolvedDate);
cmdInsert.Parameters.AddWithValue("@CustomerId", customerId);
}
catch (SqlException ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
if (conn != null)
conn.Close();
}
return ticket;
}
No data is being update inside the row correlating to the ID being entered.
TicketUtil ticketUtil = new TicketUtil();
Ticket ticket = ticketUtil.UpdateTicket(6, "Open", 1, 3, "Broken Pencils", "Buy New One", "No", "No", DateTime.Today, new DateTime(1753, 1, 1));
My end goal is to be able to hardcode an update using the code above this line, and then use that with some console prompts to allow for updating of the information. However, without even being able to hardcode the solution, I cant even think about the user input version yet.
cmdInsert.ExecuteNonQuery()? If you don't execute the expected behaviour is no execution after all.