I am using a gridview i that adding the row with text box and adding the data which is successfully done.. But when i am update a field Email it is showing the error.. Violation of UNIQUE KEY constraint while updating field
function: Update_U_Anonymoususerdetails
int InsId=0, UserId=0,UserInstLink=0,UserCreditRecord=0;
SqlConnection Cn = new SqlConnection(Conn);
Cn.Open();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label Id = GridView1.Rows[i].FindControl("lblId") as Label;
TextBox Name = GridView1.Rows[i].FindControl("txtName") as TextBox;
TextBox MName = GridView1.Rows[i].FindControl("txtMName") as TextBox;
TextBox LName = GridView1.Rows[i].FindControl("txtLName") as TextBox;
TextBox Degree = GridView1.Rows[i].FindControl("txtDegree") as TextBox;
TextBox Title = GridView1.Rows[i].FindControl("txtTitle") as TextBox;
TextBox Email = GridView1.Rows[i].FindControl("txtEmail") as TextBox;
TextBox Institution = GridView1.Rows[i].FindControl("txtInstitution") as TextBox;
if (Name.Text.Trim() != null && Name.Text.Trim() !="")
{
int AId = 0;
if (Id != null)
AId = Convert.ToInt32(Id.Text);
else
AId = 0;
SqlCommand Cmd = new SqlCommand("UpdateOtherAuthors", Cn);
Cmd.CommandType = CommandType.StoredProcedure;
SqlParameter InID = new SqlParameter("@uiID", SqlDbType.Int);
InID.Direction = ParameterDirection.Output;
Cmd.Parameters.Add(InID);
Cmd.Parameters.AddWithValue("@Name", Name.Text);
Cmd.Parameters.AddWithValue("@MName", MName.Text);
Cmd.Parameters.AddWithValue("@LName", LName.Text);
Cmd.Parameters.AddWithValue("@Degree", Degree.Text);
Cmd.Parameters.AddWithValue("@Title", Title.Text);
Cmd.Parameters.AddWithValue("@Email", Email.Text);
Cmd.Parameters.AddWithValue("@Institution", Institution.Text);
Cmd.Parameters.AddWithValue("@Id", AId);
Cmd.Parameters.AddWithValue("@CaseId", CaseId);
Cmd.ExecuteNonQuery();
UserId = int.Parse(Cmd.Parameters["@uiID"].Value.ToString());//getting error in this line first
}
if (Institution.Text.Trim() != null && Institution.Text.Trim() != "")
{
SqlCommand cmd = new SqlCommand("AddInstitution", Cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter oParam = new SqlParameter("@Institution", Institution.Text.ToString());
SqlParameter InID = new SqlParameter("@InID",SqlDbType.Int);
oParam.Direction = ParameterDirection.Input;
InID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(oParam);
cmd.Parameters.Add(InID);
cmd.ExecuteNonQuery();
InsId = int.Parse(cmd.Parameters["@InID"].Value.ToString());
}
UserInstLink= UserInstutionLink(UserId, InsId);// I am getting the error in this line
UserCreditRecord = UserCreditRecordID(UserInstLink, Name.Text, MName.Text, LName.Text, Degree.Text, Title.Text);
AssignContentManagementPrivileges(UserId,CaseId);
CreateCaseCredits(InsId, CaseId, UserCreditRecord);
}
Cn.Close();
store procedure i am using
CREATE PROCEDURE [dbo].[UpdateOtherAuthors]
(
@Name nvarchar(50),
@MName nvarchar(50),
@LName nvarchar(50),
@Degree nvarchar(50),
@Title nvarchar(50),
@Email nvarchar(50),
@Institution nvarchar(50),
@Id int,
@CaseId int,
@uiID int output
)
AS BEGIN
Declare @SameId nvarchar(50)
if(@Id='0')
begin
INSERT INTO tbl_AuthorDetails VALUES(@Name,@Degree,@Title,@Email,@Institution,@CaseId,@LName,@MName)
Select @Id=COUNT(Email) from [User] where Email=@Email
if(@Id=0)
begin
exec CreateOtherAuthorsAccount @Name,@LName,@MName,@Degree,@Email
end
end
else
begin
set @SameId=(Select (Email) from tbl_AuthorDetails where Id=@Id)
if(@SameId = @Email)
Update tbl_AuthorDetails set Name=@Name,Degree=@Degree,Title=@Title,Email=@Email,Institution=@Institution,LName=@LName,MName=@MName where Id=@Id
else
begin
INSERT INTO tbl_AuthorDetails VALUES(@Name,@Degree,@Title,@Email,@Institution,@CaseId,@LName,@MName)
Update tbl_AuthorDetails set CaseId=NULL where Id=@Id
exec CreateOtherAuthorsAccount @Name,@LName,@MName,@Degree,@Email
end
end
END
set @uiID= (Select ID from [User] where Email=@Email)
RETURN @uiID
calling the function:
objGetBaseCase.Update_U_Anonymoususerdetails(GridView1, Convert.ToInt32(Request.QueryString[0]));
It is because a record with same
emailalready exists in the database. And the database has a constraint on the email field that each entry in this field should be unique in the table you are inserting data into.EDIT: If this data is provided by a user, then you need to catch this exception and show a message to the user “A record with same email address already exists in the system.” And allow him/her to re-enter data for the email field.