When creating an account, values cannot be inserted and error shows that column (PatronID
), which is the primary key, does not allow nulls. Hence, the INSERT
fails
Have tried going into the SQL Server table designer to change "Is Identity", but I am unable to save the changes
public int add()
{
string strConn = ConfigurationManager.ConnectionStrings["BABAFOODSConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("INSERT INTO Patron (PatronName, PatronEmail, PatronPassword)" +
"OUTPUT INSERTED.PatronID " +
"VALUES(@name, @email, @password)", conn);
cmd.Parameters.AddWithValue("@name", PatronName);
cmd.Parameters.AddWithValue("@email", PatronEmail);
cmd.Parameters.AddWithValue("@password", PatronPassword);
conn.Open();
int id = (int) cmd.ExecuteScalar();
conn.Close();
return id;
}
At the ExecuteScalar
, I get this error:
System.Data.SqlClient.SqlException: 'Cannot insert the value NULL into column 'PatronID', table 'BABAFOODS.dbo.Patron'; column does not allow nulls. INSERT fails.'