Dear all,
I'm new to this platform and programming so please be kind! I've followed a tutorial on YouTube (cringe) and all has been going well with creating the Index and Create cshtml/cshtml.cs pages. I am able to view the records in the Index page, click on the button for a new entry and add one. I can then see that entry in the Index page. Now I'm trying to get the Edit page to work. The only difference between my SQL database and the one in the tutorial is that my ProjectID field is set as Identity Specification Yes, (Is Identity) Yes, Identity Increment 1 and Identity Seed 1. The field is the primary key and Allow Nulls is No. Data type is Int.
I have spent an entire day googling different solutions (including various solutions using DBNull) to this problem but have had zero success in correcting my issue. I have attached a screenshot of my error.
The error must be in the Edit script but I'm going to attach the Index script as well so you can see how the ProjectID field has been handled.
This is the Index script
using Microsoft.AspNetCore.Mvc.RazorPages; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.SqlClient; namespace ResearchPIFv0._2.Pages.PIF { public class IndexModel : PageModel { public List<PIFInfo> ListPIF = new List<PIFInfo>(); public void OnGet() { try { string connectionString = "Data Source=.\\RESEARCH;Initial Catalog=Research_PIF_Database;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); String sql = "SELECT * FROM TblPIF"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { PIFInfo pifinfo = new PIFInfo(); pifinfo.ProjectID = "" + reader.GetInt32(0); pifinfo.ActivityID = reader.GetString(1); pifinfo.Reference = reader.GetString(2); pifinfo.ProtocolNumber = reader.GetString(3); ListPIF.Add(pifinfo); } } } } } catch (Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); } } public class PIFInfo { public string ProjectID; public string ActivityID; public string Reference; public string ProtocolNumber; } } }
and this is the Edit script which is where the issue must lie:
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using static ResearchPIFv0._2.Pages.PIF.IndexModel; using System.Data.SqlClient; namespace ResearchPIFv0._2.Pages.PIF { public class EditModel : PageModel { public PIFInfo pifinfo = new PIFInfo(); public String errorMessage = ""; public String successMessage = ""; public void OnGet() { String ProjectID = Request.Query["ProjectID"]; try { String connectionString = "Data Source=.\\RESEARCH;Initial Catalog=Research_PIF_Database;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); String sql = "SELECT * FROM TblPIF WHERE ProjectID=@ProjectID"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@ProjectID", ProjectID); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { pifinfo.ProjectID = "" + reader.GetInt32(0); pifinfo.ActivityID = reader.GetString(1); pifinfo.Reference = reader.GetString(2); pifinfo.ProtocolNumber = reader.GetString(3); } } } } } catch (Exception ex) { errorMessage = ex.Message; } } public void OnPost() { pifinfo.ProjectID = Request.Form["ProjectID"]; pifinfo.ActivityID = Request.Form["ActivityID"]; pifinfo.Reference = Request.Form["Reference"]; pifinfo.ProtocolNumber = Request.Form["ProtocolNumber"]; //if (pifinfo.ProjectID.Length == 0) //{ // errorMessage = "ProjectID is required"; // return; //} try { String connectionString = "Data Source=.\\RESEARCH;Initial Catalog=Research_PIF_Database;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); String sql = "UPDATE TblPIF " + "SET ActivityID=@ActivityID, Reference=@Reference, ProtocolNumber=@ProtocolNumber WHERE ProjectID=@ProjectID"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@ActivityID", pifinfo.ActivityID); command.Parameters.AddWithValue("@Reference", pifinfo.Reference); command.Parameters.AddWithValue("@ProtocolNumber", pifinfo.ProtocolNumber); command.Parameters.AddWithValue("@ProjectID", pifinfo.ProjectID); command.ExecuteNonQuery(); } } } catch(Exception ex) { errorMessage=ex.Message; return; } Response.Redirect("/PIF/Index"); } } }
If anybody should be so kind as to help me out, I'd be most grateful.
Thank you very much.