将varchar值'System.Collections.Generic.List`1 [System.Int32]'转换为数据类型int时转换失败。错误即将到来 [英] Conversion failed when converting the varchar value 'System.Collections.Generic.List`1[System.Int32]' to data type int. error is coming

查看:61
本文介绍了将varchar值'System.Collections.Generic.List`1 [System.Int32]'转换为数据类型int时转换失败。错误即将到来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请解决此错误。

我的代码是

Hello, Please solve this error.
my code is

con.Open();
        using (SqlCommand cmd = new SqlCommand("select ID from Vendor_gatepass where GatePass_Status='Close' and Vendor_GatePass='" + Gatepass.Text + "'", con))
        {
            SqlDataReader dr1 = cmd.ExecuteReader();
            List<int> list = new List<int>();
            if (dr1.HasRows)
            {
                while (dr1.Read())
                {
                    list.Add(Convert.ToInt32(dr1[0]));
                }
            }
            Session["ID"] = list;

            dr1.Close();


            string query = "update Product_Details set [GatePass status]='Close' where ID in ('"+Session["ID"]+"')";
            SqlCommand comdd = new SqlCommand(query, con);
            comdd.ExecuteNonQuery();
            con.Close();
    }}

推荐答案

您是否已调试代码并检查值是什么 dr1 [0] ,你从那里得到了错误。



以及你的查询



Have you debug your code and check what the value is coming in dr1[0], you got the error from there.

and also your query

string query = "update Product_Details set [GatePass status]='Close' where ID in ('"+Session["ID"]+"')";





我认为您需要使用IN原因更新数据,因此您需要将您的id列表转换为逗号分隔字符串,以便将其应用于 WHERE ID IN('... 。')原因。


和修改后的查询看起来像这样..







I think you need to update data with using IN cause, so you need to convert your list of id to coma separated string for applying it in WHERE ID IN ('....') cause.

and modified query looks like this..


string query = "update Product_Details set [GatePass status]='Close' where ID in ("+ string.Join(",", (List<int>)Session["ID"]) +")";</int>


调试代码并检查行

Debug your code and check the line
list.Add(Convert.ToInt32(dr1[0]));



同时将会话[ID] CAST转换为int,同时传递给Update查询。



希望有所帮助:)


Also CAST your Session["ID"] into int While passing to the Update query.

Hope it helps :)


这篇关于将varchar值'System.Collections.Generic.List`1 [System.Int32]'转换为数据类型int时转换失败。错误即将到来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆