How to prevent a particular user (from All users) from inserting the same data in a column
I have a table with 4 columns, and 3 existing users (user1, user2 and user3). I want to prevent each user from inserting the same data for a particular column.
Id
|
UserMail
|
MeetingName
|
MeetingDate
|
1
|
Josh@gmail.com
|
Elders Forum Meeting
|
28-Aug-21 06:00 PM
|
2
|
Malcom123@exxon.com
|
Elders Forum Meeting
|
29-Aug-21 01:16 PM
|
3
|
Helen24@gmail.com
|
Youth Meeting
|
30-Aug-21 08:15 AM
|
4
|
Helen24@gmail.com
|
Elders Forum Meeting
|
30-Aug-21 01:00 PM
|
5
|
Malcom123@exxon.com
|
|
|
For example, user1 inserts into Meeting Name Elders Forum Meeting, user2 and user3 does the same thing. If any user tries to insert the same Meeting Name, it should prevent it from inserting.
In the above table, Malcom123@exxon.com cannot insert Elders Forum Meeting again because he already has that Meeting Name attached to his account.
My code used prevents all users from inserting the same Meeting Name if it already exists in the database. But I want to prevent a user only if he or she already has the same Meeting Name in the database i.e a user cannot insert the same Meeting Name twice. Then if a user wants to insert the same Meeting Name, that user can add years to the Meeting Name (example: “Elders Forum Meeting 2021”)
Here is my code
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
SqlDataReader dr;
using (SqlCommand cmd = new SqlCommand())
{
cmd.Parameters.Clear();
cmd.CommandText = "SELECT * FROM Table WHERE UserMail=@ UserMail OR MeetingName=@MeetingName";
cmd.Parameters.AddWithValue("@UserMail ", mailtxtbx.Text);
cmd.Parameters.AddWithValue("@MeetingName", meetingtxtbx.Text);
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
}
if (dr.HasRows)
{
dvMessage.Visible = true;
Div1.Visible = false;
lblMessage.Text = "Cannot Register Same Meeting Name";
lblMessage.ForeColor = System.Drawing.Color.Red;
meetingtxtbx.Text = "";
meetingtxtbx.Focus();
}
else
{
}