简体   繁体   English

ado.net从varchar到int的转换失败

[英]Conversion failed from varchar to int, ado.net

I'm working with sql databases via ado.net in c# and I'm trying to pass the following update command: 我正在通过C#中的ado.net使用sql数据库,并且正在尝试传递以下更新命令:

cmd.CommandText = @"UPDATE VehicleContract SET regNr='@reg', assoc_id='@assort', percentage='@perc', vehicleType='@type', trailerNr='@trailer' WHERE contractId='@id'";
cmd.Parameters.AddWithValue("@id", id);

id is in int. id是int。 I read it as an int. 我以int形式阅读。 I even do a int.Parse(txtbox.text) and everything is fine. 我什至做一个int.Parse(txtbox.text),一切都很好。 But then when I insert all the values in the boxes, press submit. 但是,当我在框中插入所有值时,请按提交。 I get the conversion error saying that it can't convert '@id' varchar to int... it makes no sense 我收到转换错误,说它无法将'@id'varchar转换为int ...这没有任何意义

Is there any specific thing I'm not doing right? 有什么我做不正确的事情吗? Need any more details? 需要更多细节吗?

The whole code:
if (tbid.Text == "" || tbper.Text == "" || tbas.Text == "" || tbvt.Text == "")
            {
                MessageBox.Show("All fields must be filled ");
                return;
            }
            if (tbreg.Text == "" && tbtn.Text == "")
            {
                MessageBox.Show("Fill at least one from: Registration nr or trailer nr");
                return;
            }
            int id = 0;
            string reg = "";
            int assort = 0;
            int perc = 0;
            string type = "";
            int trailer = 0;
            try
            {
                id = int.Parse(tbid.Text);
                Console.Write(id);
                if (tbreg.Text != "")
                {
                    reg = tbreg.Text;
                }
                assort = int.Parse(tbas.Text);
                perc = int.Parse(tbper.Text);
                type = tbvt.Text;
                if (tbtn.Text != "")
                {
                    trailer = int.Parse(tbtn.Text);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("id, assoc, perc and trailer nr must be integers ");
                return;
            }
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            con.ConnectionString = "Data Source=.;initial Catalog=Lab5;integrated security=true";
            con.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = @"UPDATE VehicleContract SET regNr='@reg', assoc_id='@assort', percentage='@perc', vehicleType='@type', trailerNr='@trailer' WHERE contractId='@id'";
            cmd.Parameters.AddWithValue("@id", id);
            if (reg == "")
            {
                cmd.Parameters.AddWithValue("@reg", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@reg", reg);
            }
            cmd.Parameters.AddWithValue("@assort", assort);
            cmd.Parameters.AddWithValue("@perc", perc);
            cmd.Parameters.AddWithValue("@type", type);
            if (trailer == 0)
            {
                cmd.Parameters.AddWithValue("@trailer", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@trailer", trailer);
            }
            cmd.Connection = con;
            SqlDataReader sdr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(sdr);

            childgrid.DataSource = dt;
            sdr.Close();
            con.Close();

When you use WHERE contractId='@id' in a query you are comparing the int contractId column with the string '@id' value. 在查询中使用WHERE contractId='@id' ,您正在将int contractId列与字符串'@id'值进行比较。

You need to use WHERE contractId=@id . 您需要使用WHERE contractId=@id

So remove single quotes around @id and other parameters. 因此,请删除@id和其他参数周围的单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM