无法将数据类型nvarchar转换为int错误 [英] Cannot Convert Datatype nvarchar into int error

查看:88
本文介绍了无法将数据类型nvarchar转换为int错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我的插入按钮代码:

Hi,
This is my Insert Button Code:

private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
      cmd.CommandText = "Ins_Upd_Students";//Ins_Upd_Students is my stored procedure
                cmd.Parameters.AddWithValue("@Sno", textBox1.Text);
                cmd.Parameters.AddWithValue("@Sname", textBox2.Text);
                cmd.Parameters.AddWithValue("@Class", textBox3.Text);
                cmd.Parameters.AddWithValue("@Fees", textBox4.Text);
                con.Open();
                int affectedRecord=cmd.ExecuteNonQuery();
                MessageBox.Show("Executed");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }



我将所有表列都声明为varchar(50)类型.
插入时它显示错误,因为无法将数据类型nvarchar转换为int .
任何人都可以给我打电话,为什么我会收到该错误以及如何纠正该错误.
非常感谢



I declared all my table column as varchar(50) type.
At the time of insertion it shows the error as cannot convert data type nvarchar into int.
Can any one tel me why I''m getting that error and how to rectify that error.
Thank you so much

推荐答案

转换为整数.
Convert to integer.
cmd.Parameters.AddWithValue("@Sno", Convert.ToInt32(textBox1.Text));



或使用



or use

cmd.Parameters.Add("@Sno", SqlDbType.Int);
cmd.Parameters("@Sno").Value = sno;


您会在Stored Procedure中的某些字段中提到INT.也请在存储过程中将所有内容都更改为varchar.
You would have mentioned INT for some fields in Stored Procedure. Please change all to varchar in Stored Procedure too.


问题出在存储过程内部,您很可能使用varchar参数,但是您试图将值放入int列中.检查存储过程的所有参数是否与表中的列完全相同.
The problem is inside the stored procedure, You''re most likely using a varchar parameter but you''re trying to put the value into an int column. Check that all the parameters of the stored procedure are exactly the same datatype as the columns in your table.


这篇关于无法将数据类型nvarchar转换为int错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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