首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ExecuteScalar();使用scope_identity()生成"System.InvalidCastException:指定的类型转换无效“

ExecuteScalar();使用scope_identity()生成"System.InvalidCastException:指定的类型转换无效“
EN

Stack Overflow用户
提问于 2010-12-11 04:49:45
回答 5查看 24.6K关注 0票数 19

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

根据许多答案和示例,这应该可以完美地工作!..but它给了我这个错误:

代码语言:javascript
复制
Exception Details: System.InvalidCastException: Specified cast is not valid.

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

这是我的linkbutton方法代码:

代码语言:javascript
复制
   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();
            }
        }
    }

奇怪的编辑当我直接在visual studio的sql server express中运行它时,查询运行得很好!

这个奇怪问题的奇怪解释

alexb的第一个解决方案:

我做到了:)我的sql server2008返回打包到‘System.Decimal’中的对象。尝试使用System.Convert.ToInt32而不是cast。

代码语言:javascript
复制
Example : int NewBrandId = System.Convert.ToInt32(comm.ExecuteScalar());

我的第二个解决方案:

没有使用"SELECT scope_identity();“,而是使用了"SELECT BrandId FROM Brands WHERE BrandId = @@Identity;”,它为您提供了更多的控制,但我个人更倾向于第一种解决方案

注意:"SELECT @@Identity;“也可以正常工作

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-12-11 04:54:58

comm.ExecuteScalar()到底返回了什么?也许它会返回打包到object中的longdouble

记不清了,但似乎我在1-2年前就遇到过一些事情。

票数 7
EN

Stack Overflow用户

发布于 2011-04-04 04:25:21

如果可以更改SQL,一种更简单的方法是

代码语言:javascript
复制
SELECT CONVERT(int, SCOPE_IDENTITY())
票数 39
EN

Stack Overflow用户

发布于 2013-10-30 22:15:19

正如Dimi所说,Identity是一个SQL数字(.NET小数)。这应该是可行的:

代码语言:javascript
复制
decimal decimalBrandId = (decimal)comm.ExecuteScalar(); 
int NewBrandId = (int)decimalBrandId;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4413178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档