Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;将数据类型nvarchar转换为数字时出错。”;_C#_Sql Server 2008 - Fatal编程技术网

C# &引用;将数据类型nvarchar转换为数字时出错。”;

C# &引用;将数据类型nvarchar转换为数字时出错。”;,c#,sql-server-2008,C#,Sql Server 2008,我想用组合框将记录插入数据库。组合框已连接到另一个表,这是错误: 将数据类型nvarchar转换为数字时出错 调用AddWithValue时,确保要传递的数据类型与列类型匹配。下面是一个可能的候选人: cmd.Parameters.AddWithValue("@Amount", txtAmount.Text); 在这一行中,您正在将文本字符串传递给显然需要数字值(金额)的对象。您应该首先将txtmount.Text解析为十进制,然后传递该值: decimal amount = decimal.

我想用组合框将记录插入数据库。组合框已连接到另一个表,这是错误:

将数据类型nvarchar转换为数字时出错


调用
AddWithValue
时,确保要传递的数据类型与列类型匹配。下面是一个可能的候选人:

cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);
在这一行中,您正在将文本字符串传递给显然需要数字值(金额)的对象。您应该首先将
txtmount.Text
解析为十进制,然后传递该值:

decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);

使用此代码,如果无法将
txtmount.Text
中的字符串解析为十进制,您仍然可能会遇到异常,但至少您会知道是哪个值导致了问题。您也可以/应该对其他值执行相同的操作,以确保它们与列类型匹配。

请尝试string.isNullorEmpty(txtAmount.text)

使用Int.ParseInt(txtmount.Text)将其保存为数字,或者使用Float.ParseFloat。我猜你没有给出一个模式。你发布了完全不同的代码,没有任何解释。
decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);
 private void button1_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "insert into slab (date,sober_visor,tesh,shift,group,heat_no,st_grade,thick,width,length,location,pcs,remarkes,slab_no) values (@date,@sober_vsor,@tesh,@shift,@group,@heat_no,@st_grade,@thick,@width,@length,@loction,@pcs,@slab_no);select scope_identity()";

            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@date", txt_date.Text);
            cmd.Parameters.AddWithValue("@sober_visor", com_sober_visor.ToString());
            cmd.Parameters.AddWithValue("@shift", txt_shift.Text);
            cmd.Parameters.AddWithValue("@heat_no", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@thick", txt_shift.Text);
            cmd.Parameters.AddWithValue("@width", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@length", txt_length.Text);
            cmd.Parameters.AddWithValue("@pcs", txt_pcs.Text);
            cmd.Parameters.AddWithValue("@st_grade", txt_st_gread.Text);
            cmd.Parameters.AddWithValue("@location", txt_loction.Text);
            cmd.Parameters.AddWithValue("@slab_no", txt_slab_no.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            txt_heat_no.Text = cmd.ExecuteScalar().ToString();

            con.Close();
            MessageBox.Show("تمت عملية الإضافة");
        }
    }
}
cmd.Parameters.AddWithValue("@ödenecektutar", Convert.ToDecimal(tutar1.Text.Substring(0, tutar.Text.Length - 1)));