执行标量();使用scope_identity()生成“System.InvalidCastException:指定的强制转换无效”

发布于 2024-10-06 18:19:58 字数 2694 浏览 2 评论 0原文

我有一个接受各种数据的表单(通过文本框和复选框列表),并且在单击事件时,它们将所有数据插入表中并选择scope_identity,然后将其存储在变量中以在插入复选框列表项时使用它 使用循环进入另一个表,

根据许多答案和示例,

Exception Details: System.InvalidCastException: Specified cast is not valid.

Line 66:             int NewBrandId = (int)comm.ExecuteScalar(); 

这应该可以完美工作!..但是它给了我这个错误:这是我的链接按钮方法代码:

   protected void lnkbtnUploadAndSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MOODbCenterConnection"].ConnectionString);

        SqlCommand comm = new SqlCommand("INSERT INTO Brands (BrandName, BrandLogo, BrandWebsite, IsBrandVisible) VALUES (@Name, @Logo, @Website, @IsVisible); SELECT scope_identity();", conn);

        comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar, 50);
        comm.Parameters["@Name"].Value = txtbxBrandName.Text;

        comm.Parameters.Add("@Logo", System.Data.SqlDbType.Text);
        comm.Parameters["@Logo"].Value = fileuploadLogo.PostedFile.FileName;

        comm.Parameters.Add("@Website", System.Data.SqlDbType.Text);
        comm.Parameters["@Website"].Value = txtbxWebsite.Text;

        comm.Parameters.Add("@IsVisible", System.Data.SqlDbType.Bit);
        comm.Parameters["@IsVisible"].Value = chkbxIsPublished.Checked;
        conn.Open();


        int NewBrandId = (int)comm.ExecuteScalar(); 

        conn.Close();

        foreach (ListItem li in chkbxlstCuisines.Items)
        {
            if (li.Selected)
            {
                conn.Open();

                SqlCommand comm2 = new SqlCommand("INSERT INTO BrandCuisines (CuisineId, BrandId) VALUES (@CuisineId, @NewBrandId)", conn);

                comm2.Parameters.Add("@CuisineId", System.Data.SqlDbType.Int);
                comm2.Parameters["@CuisineId"].Value = li.Value;

                comm2.Parameters.Add("@NewBrandId", System.Data.SqlDbType.Int);
                comm2.Parameters["@NewBrandId"].Value = NewBrandId;
                comm2.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

编辑奇怪的是,当我直接在 sql server express 中运行它时,查询工作正常视觉工作室!

对这个奇怪问题的奇怪解释

alexb 的第一个解决方案:

我做到了:) 我的 sql server 2008 返回 System.Decimal 打包到“对象”中。 尝试使用 System.Convert.ToInt32 而不是演员。

Example : int NewBrandId = System.Convert.ToInt32(comm.ExecuteScalar());

我的第二个解决方案:

而不是使用“选择 scope_identity();”我用了“SELECT BrandId 来自品牌 WHERE BrandId = @@Identity;”它为您提供更多 控制但我个人倾向于 第一个解决方案

注意:“选择@@Identity;”也会 工作得很好

谢谢大家,我希望它能帮助其他人!

I've have a form which accept various data (through textboxes and a checkboxlist) and on the click event they insert all the data into a table and selects the scope_identity then store it in a variable to use it in the insertion of the checkboxlist items using a loop into another table

according to many answers and examples this should work perfectly!..but it gives me this error :

Exception Details: System.InvalidCastException: Specified cast is not valid.

Line 66:             int NewBrandId = (int)comm.ExecuteScalar(); 

Here's my linkbutton method code :

   protected void lnkbtnUploadAndSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MOODbCenterConnection"].ConnectionString);

        SqlCommand comm = new SqlCommand("INSERT INTO Brands (BrandName, BrandLogo, BrandWebsite, IsBrandVisible) VALUES (@Name, @Logo, @Website, @IsVisible); SELECT scope_identity();", conn);

        comm.Parameters.Add("@Name", System.Data.SqlDbType.NVarChar, 50);
        comm.Parameters["@Name"].Value = txtbxBrandName.Text;

        comm.Parameters.Add("@Logo", System.Data.SqlDbType.Text);
        comm.Parameters["@Logo"].Value = fileuploadLogo.PostedFile.FileName;

        comm.Parameters.Add("@Website", System.Data.SqlDbType.Text);
        comm.Parameters["@Website"].Value = txtbxWebsite.Text;

        comm.Parameters.Add("@IsVisible", System.Data.SqlDbType.Bit);
        comm.Parameters["@IsVisible"].Value = chkbxIsPublished.Checked;
        conn.Open();


        int NewBrandId = (int)comm.ExecuteScalar(); 

        conn.Close();

        foreach (ListItem li in chkbxlstCuisines.Items)
        {
            if (li.Selected)
            {
                conn.Open();

                SqlCommand comm2 = new SqlCommand("INSERT INTO BrandCuisines (CuisineId, BrandId) VALUES (@CuisineId, @NewBrandId)", conn);

                comm2.Parameters.Add("@CuisineId", System.Data.SqlDbType.Int);
                comm2.Parameters["@CuisineId"].Value = li.Value;

                comm2.Parameters.Add("@NewBrandId", System.Data.SqlDbType.Int);
                comm2.Parameters["@NewBrandId"].Value = NewBrandId;
                comm2.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

Edit Strangely the query works fine when I run it directly in the sql server express of the visual studio!

THE Weird Explanation For This Weird Problem

First Solution by alexb :

I did it :) My sql server 2008 returns
System.Decimal packed into 'object'.
Try to use System.Convert.ToInt32
instead of cast.

Example : int NewBrandId = System.Convert.ToInt32(comm.ExecuteScalar());

Second Solution by me :

instead of using "SELECT
scope_identity();" I used "SELECT
BrandId FROM Brands WHERE BrandId =
@@Identity;" it gives you even more
controlm but I personally prefare the
first solution

N.B : "SELECT @@Identity;" would also
work just fine

Thanks guys and I hope it help others!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡淡の花香 2024-10-13 18:19:58

如果您可以更改 SQL,则更简单的方法是

SELECT CONVERT(int, SCOPE_IDENTITY())

If you can change the SQL, an even easier way would be to

SELECT CONVERT(int, SCOPE_IDENTITY())
一念一轮回 2024-10-13 18:19:58

comm.ExecuteScalar() 到底返回什么?
也许它会返回打包到 object 中的 longdouble
记不太清楚了,但好像我一两年前就遇到过一些事情。

What exactly comm.ExecuteScalar() returns?
Maybe it returns long or double packed into object.
can't remember exactly, but seems like I had faced something 1-2 years ago.

π浅易 2024-10-13 18:19:58

正如 Dimi 所说,Identity 是一个 SQL 数字(.NET 十进制)。这应该有效:

decimal decimalBrandId = (decimal)comm.ExecuteScalar(); 
int NewBrandId = (int)decimalBrandId;

As Dimi said, Identity is a SQL Numeric (.NET Decimal). This should work:

decimal decimalBrandId = (decimal)comm.ExecuteScalar(); 
int NewBrandId = (int)decimalBrandId;
心病无药医 2024-10-13 18:19:58

SCOPE_IDENTITY() 的文档中,

Return Types

numeric(38,0) 

一个简单的解决方案如下

DECLARE @ReturnValue INT;

/*
   Do your insert stuff here.
*/

SELECT @ReturnValue = SCOPE_IDENTITY();
SELECT @ReturnValue;

From the documentation for SCOPE_IDENTITY()

Return Types

numeric(38,0) 

So an easy solution would be the following

DECLARE @ReturnValue INT;

/*
   Do your insert stuff here.
*/

SELECT @ReturnValue = SCOPE_IDENTITY();
SELECT @ReturnValue;
少年亿悲伤 2024-10-13 18:19:58

尝试在插入之前添加SET NOCOUNT ON;

try adding SET NOCOUNT ON; before the insert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文