Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 标识列错误_Sql_Asp.net - Fatal编程技术网

Sql 标识列错误

Sql 标识列错误,sql,asp.net,Sql,Asp.net,我制作了一个存储过程,用于通过前端在表中插入值。我在存储过程中有一个标识列作为ID。但每当我添加它时,它都会给出当identity_insert设置为OFF时,无法在表'BusinessUnit'中插入identity列的显式值。请参阅代码以供参考 ALTER PROCEDURE [dbo].[Add_BusinessUnit] -- Add the parameters for the stored procedure here @Id int, @Name nvarchar(100) AS

我制作了一个存储过程,用于通过前端在表中插入值。我在存储过程中有一个标识列作为ID。但每当我添加它时,它都会给出当identity_insert设置为OFF时,无法在表'BusinessUnit'中插入identity列的显式值。请参阅代码以供参考

ALTER PROCEDURE [dbo].[Add_BusinessUnit]
-- Add the parameters for the stored procedure here
@Id int,
@Name nvarchar(100)
AS

BEGIN

     INSERT INTO Career.BusinessUnit(Id,Name) values (@Id,@Name)
END
另请参见从前端添加的按钮单击

protected void btnAddBusiness_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_BusinessUnit";
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtBusinessUnitOther.Text.Trim();
        cmd.Connection = con;
        try
        {
            cmd.ExecuteNonQuery();
            // BindContrydropdown();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);//You Can Haave Messagebox here
        }
        finally
        {
            con.Close();
        }
    }
}

请帮助。

因为您正在手动设置标识列的值。 您的SP应该是这样的

ALTER PROCEDURE [dbo].[Add_BusinessUnit]
-- Add the parameters for the stored procedure here
@Name nvarchar(100)
AS

BEGIN

     INSERT INTO Career.BusinessUnit(Name) values (@Name)
END
你的c代码应该是

protected void btnAddBusiness_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_BusinessUnit";

        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtBusinessUnitOther.Text.Trim();
        cmd.Connection = con;
        try
        {
            cmd.ExecuteNonQuery();
            BindContrydropdown();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);//You Can Haave Messagebox here
        }
        finally
        {
            con.Close();
        }
    }
}
SQL Server过程
关于错误消息,您不了解什么?应自动设置
标识
列。如果要手动设置,必须在表上将identify insert设置为off,但应非常小心。不需要为identity列设置值。它将自动设置。@AmitKumar:那么我是否应该将它从SP和ID代码中删除?@Saba:是的。查看答案。当我查看下拉列表时,它不会被插入,而是在我刷新页面时被插入。但是,当我检查表时,会插入值。怎么办?@Saba:正如我所看到的,你是在按钮的点击事件中编写代码的,而不是在检查事件中。是的,我必须在按钮点击时添加文本框值。但是我无法在列表中看到,在插入此代码之后,您需要调用该函数,即绑定列表。在这个代码中
bindcontroldropdown()已注释,取消注释。这是针对国家/地区,而不是针对业务部门。这样行吗?你确定吗?
ALTER PROCEDURE [dbo].[Add_BusinessUnit]
@Id int  OUTPUT,
@Name nvarchar(100)
AS
BEGIN
  SET NOCOUNT ON;
     INSERT INTO Career.BusinessUnit(Name) 
     values (@Name)

     SET @Id = SCOPE_IDENTITY();
END

// Call procedure


DECLARE @ID INT;

EXECUTE [dbo].[Add_BusinessUnit] 'Mark', @ID OUTPUT

SELECT @ID; --<-- Variable will be populated with newly inserted ID value
// define connection and command, in using blocks to ensure disposal
using(SqlConnection conn = new SqlConnection(pvConnectionString ))
using(SqlCommand cmd = new SqlCommand("dbo.Add_BusinessUnit", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    // set up the parameters
    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
    cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;

    // set parameter values
    cmd.Parameters["@Name"].Value = Name;

    // open connection and execute stored procedure
    conn.Open();
    cmd.ExecuteNonQuery();

    // read output value from @Id
    int NewID = Convert.ToInt32(cmd.Parameters["@ID"].Value);
    conn.Close();
}