指定演员表无效。

时间:2015-02-28 08:31:30

标签: asp.net sql-server linq-to-sql

protected void RegisterUser(object sender,EventArgs e) 
{
    int userid = 0;
    var s = (from a in dc.Insert_User(txtUsername.Text.Trim(), txtPassword.Text.Trim(), txtEmail.Text.Trim())
                 select a.Column1).SingleOrDefault();

   // gvtest.DataSource = s;
   // gvtest.DataBind();
    userid = Convert.ToInt32(s);
    string message = string.Empty;
    switch (userid)
    {
        case -1:
            message = "Username already exists.\\nPlease choose a different username.";
            break;
        case -2:
            message = "Supplied email address has already been used.";
            break;
        default:
            message = "Registration successful.\\nUser Id: " + userid.ToString();
            break;
    }
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}

当我输入新的记录以进行注册时,我在查询时获得指定的强制转换无效错误。记录也插入数据库中。

这是存储过程

CREATE PROCEDURE [dbo].[Insert_User]
  @Username NVARCHAR(20),
  @Password NVARCHAR(20),
  @Email NVARCHAR(30)
AS
BEGIN
  SET NOCOUNT ON;
  IF EXISTS(SELECT UserId FROM Users WHERE Username = @Username)
  BEGIN
        SELECT -1 -- Username exists.
  END
  ELSE IF EXISTS(SELECT UserId FROM Users WHERE Email = @Email)
  BEGIN
        SELECT -2 -- Email exists.
  END
  ELSE
  BEGIN
        INSERT INTO [Users]
                 ([Username]
                 ,[Password]
                 ,[Email]
                 ,[CreatedDate])
        VALUES
                 (@Username
                 ,@Password
                 ,@Email
                 ,GETDATE())

        SELECT SCOPE_IDENTITY() -- UserId                 
 END
END

但从程序

返回-1或-2时没有这样的错误

1 个答案:

答案 0 :(得分:0)

我说最可能的答案是,由于IDENTITY是一个数字(32),因此对于Int32而言,回来的数据可能太大了。如果标识列是uniqueidentifier,您可能会成功将其转换为GUID类型。