如何修复:将nvarchar值'hex mec'转换为数据类型int时转换失败。 [英] How to fix: conversion failed when converting the nvarchar value 'hex mec' to data type int.

查看:83
本文介绍了如何修复:将nvarchar值'hex mec'转换为数据类型int时转换失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行页面时出现问题,即使我没有在.cs上出现任何错误。

'Hex Mec'是Supplier表中第一个带有数据类型varchar的CompanyName(即使错误说'转换nvarchar值'。我正在尝试根据从此参考链接获取的另一个下拉列表中的选择填充一个Asp.net下拉列表



我有很多堆栈跟踪源错误指示我跟踪的地方。

我也在使用datasettableadapters进行编码,并欢迎任何人的建议(恳求,因为这是我正在研究的论文)。



我尝试过的事情:



我不是一个非常优秀的程序员喜欢以最简单的方式寻求帮助。我尝试使用解析但似乎没有任何工作(大胆的字符是我认为它的地方我正在尝试根据另一个下拉列表中的选择填充一个Asp.net下拉列表,我的代码是:



The problem occurs when I run the page even though I am not getting any errors on .cs.
'Hex Mec' is the first CompanyName from Supplier table with a datatype varchar (even though the error says 'converting the nvarchar value'. I'm trying to Populate One Asp.net Dropdown based on Selection in Another Dropdown taken from this reference link

I have a lot of stack trace where the source error is instructing me to trace.
I'm also doing codes using datasettableadapters and would welcome anyone's suggestion (pleading, for this is a thesis I'm working on).

What I have tried:

I'm not a very good coder and would like to seek help in the simplest way. I have tried using parse but nothing seems to be working (the bold characters are where I think it's not working). I'm trying to Populate One Asp.net Dropdown based on Selection in Another Dropdown and my code is:

protected void ddSuppliers_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SupplierID = Convert.ToInt32(ddSuppliers.SelectedValue);
        SqlConnection con = new SqlConnection(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[RawMaterialMF] WHERE SupplierID=@SupplierID", con);
        cmd.Parameters.Add("@SupplierID", SqlDbType.Int).Value = SupplierID;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
ddRawMats.DataTextField = "ItemName";
        ddRawMats.DataValueField = "ItemName"; 
        ddRawMats.DataBind();
        ddRawMats.Items.Insert(0, new ListItem("Select Raw Materials", "0"));





此下拉列表也出现在:



where this dropdown also appears in:

protected void btnProceed_Click(object sender, EventArgs e)
    {
        ddSuppliers.Enabled = false;
        mvPDetails.ActiveViewIndex = 0;

        //getting ItemID using the ItemName from the dropdown list
        DataSetTableAdapters.PurchaseDetailsTableAdapter datItem = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        DataTable tblPurchase = new DataTable();
        tblPurchase = datItem.GetDataByItem(ddRawMats.Text);
        int Item = Convert.ToInt32(tblPurchase.Rows[0]["ItemID"].ToString());

        //getting SupplierID
        DataSetTableAdapters.PurchaseDetailsTableAdapter datSuppliers = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        DataTable tblSuppliers = new DataTable();
        tblSuppliers = datSuppliers.GetDataBySupplier(ddSuppliers.SelectedValue);
        int Supplier = Convert.ToInt32(tblSuppliers.Rows[0]["SupplierID"].ToString());

        //getting Price using the ItemName from the dropdown
        DataSetTableAdapters.RawMaterialMF1TableAdapter datPrice = new DataSetTableAdapters.RawMaterialMF1TableAdapter();
        DataTable tblPrice = new DataTable();
        tblPrice = datPrice.GetDataByPrice(Item);
        int Price = Convert.ToInt32(tblPrice.Rows[0]["Price"]);
        int total = Price * Convert.ToInt32(txtQty.Text.ToString());

        double qty;
        qty = Convert.ToDouble(txtQty.Text);

        //
        DataSetTableAdapters.PurchaseDetailsTableAdapter datInsert = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        datInsert.InsertPDetails(null, Item, qty, total, txtRemarks.Text, Supplier);


        gvPurchase.DataBind();
    }

推荐答案

您希望从Hex Mec转换为整数时,您期望获得什么价值?计算机显然不可能对你想要的东西做出任何猜测。
Exactly what value do you expect to get from trying to convert "Hex Mec" to an integer? It is obviously impossible for the computer to make any guesses as to what you want.


如果你的列应该是一个整数ID字段,为什么要将它转换为字符串,为了将它转换回整数?

如果它不是一个整数 - 并且如果表中的数据是hex mec则不是 - 那么它可能应该是,所以这样的问题不会发生。



我们不能说做这个并解决问题 - 它与你的数据和数据源绑定得太紧,既不是我们有权访问。

所以,它取决于你。

在函数的第一行放一个断点,并运行你的代码调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:debugging!
If your column is supposed to be an integer ID field, why are you converting it to a string, in order to convert it back to an integer?
And if it isn't an integer - and it isn't if the data in your table is "hex mec" - then it probably should be, so problems like this don't occur.

We can't say "do this" and fix the problem - it's too tightly bound to your data and data source, neither of which we have any access to.
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我们看不到你是如何填充DropDownList的。

通常,绑定到表的DropDownLists需要两个信息:表示所选值的键(一个键,因为它必须允许明确地检索表中的特定行),以及用于显示的文本选定的值。这些信息分别由 DataValueField DataTextField 属性控制。因此,当您在列表中选择Hex Mec时, SelectedValue 属性返回的值是数据库表中元素的主键。

您可以在其参考页面上看到 DropDownList 用法的更多信息:

DropDownList类 [ ^ ]

亲切。
We cannot see how your are populating your DropDownList.
Generally, DropDownLists bound to a table need two informations: the key representing the selected value (a key because it must allow to retrieve a specific row in the table unambiguously), and the text used to display the selected value. These informations are controlled with DataValueField and DataTextField properties, respectively. So that when you select "Hex Mec" in the list, the value returned by the SelectedValue property is the primary key of the element in the database table.
You can see more on DropDownList usage on its reference page:
DropDownList Class[^]
Kindly.


这篇关于如何修复:将nvarchar值'hex mec'转换为数据类型int时转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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