更新字段时违反UNIQUE KEY约束

时间:2011-06-01 12:51:04

标签: c# asp.net

我正在使用gridview i添加带有文本框的行并添加成功完成的数据..但是当我更新字段时,电子邮件显示错误..更新字段时违反UNIQUE KEY约束< / p>

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

调用函数:

objGetBaseCase.Update_U_Anonymoususerdetails(GridView1, Convert.ToInt32(Request.QueryString[0])); 

2 个答案:

答案 0 :(得分:3)

这是因为数据库中已存在具有相同email的记录。并且数据库对电子邮件字段有一个约束,即该字段中的每个条目在您要插入数据的表中应该是唯一

编辑:如果此数据由用户提供,则您需要捕获此例外并向用户显示消息“已有相同电子邮件地址的记录存在于系统中。“并允许他/她重新输入电子邮件字段的数据。

答案 1 :(得分:2)

要解决此问题,您可以检查电子邮件是否已存在:

IF NOT EXISTS (SELECT * FROM tbl_AuthorDetails where Email = @Email)
BEGIN
    Update  tbl_AuthorDetails set Name=@Name,Degree=@Degree,Title=@Title,Email=@Email,Institution=@Institution,LName=@LName,MName=@MName where Id=@Id
END