im trying to make a reservation page in razor pages c#. If i make a reservation it will also appear on the page for the user. I made a button next to every reservation so you can delete the reservation. The reservation shows on the page only the date and the location. When I try to delete it from the page i get this error: PostgresException: 42883: operator does not exist: @ character varying And have no idea why. Here is the c# side:
public void OnPostRemove(ReservationModel reservation)
{
DateTime convdayid = Convert.ToDateTime(reservation.Dayid);
DeleteReservation(convdayid, reservation.Locationid);
}
public void DeleteReservation(DateTime convdayid, string Locationid)
{
var cs = Database.Database.Connector();
using var con = new NpgsqlConnection(cs);
con.Open();
var sql = "DELETE FROM reservation WHERE locationid = @Locationid AND dayid = @convdayid;";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("locationnid", Locationid);
cmd.Parameters.AddWithValue("dayid", convdayid);
cmd.Prepare();
cmd.ExecuteNonQuery();
}
And this is the html side if necessary
@foreach(var reservations in @Model.ShowReservation())
{
<form method="post">
<fieldset>
<div class="form-group">
<tr>
<img class="img-fluid" src="/Images/Reservation.png"/>
<td>
@Html.DisplayFor(m => reservations.Dayid)
<input type="hidden" name="Dayid" value="@Html.DisplayFor(m => reservations.Dayid)"/>
</td>
<td>
@Html.DisplayFor(m => reservations.Locationid)
<input type="hidden" name="Locationid" value="@Html.DisplayFor(m => reservations.Locationid)"/>
</td>
</tr>
<button type="submit" value="Submit" asp-page-handler="Remove" class="btn btn-dark btn-sm">X</button>
</div>
</fieldset>
</form>
<hr>
}
<hr>
Edit :
The error got solved, but for some reason the specific row doesnt get deleted from the database. Nothing happens. This is what I changed about my code.
public void OnPostRemove(ReservationModel reservation)
{
DateTime convdayid = Convert.ToDateTime(reservation.Dayid);
DeleteReservation(convdayid, reservation.Locationid);
}
public void DeleteReservation(DateTime convdayid, string Locationid)
{
var cs = Database.Database.Connector();
using var con = new NpgsqlConnection(cs);
con.Open();
var sql = "DELETE FROM reservation WHERE locationid = @locationid AND dayid = @dayid;";
using var cmd = new NpgsqlCommand(sql, con);
cmd.Parameters.AddWithValue("locationid", Locationid);
cmd.Parameters.AddWithValue("dayid", convdayid);
cmd.Prepare();
cmd.ExecuteNonQuery();
}
Its trying to parse the '@' as '=@'
DELETE FROM reservation WHERE locationid = @Locationid AND dayid = @convdayid;
Is literally being passed to the Db. You need to build that string so @LocationID and @convdaid are populated accordingly. Something like:
DELETE FROM reservation WHERE locationid = 34 AND dayid = 55;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.