简体   繁体   English

将varchar转换为数值数据类型的算术溢出错误。 asp.net中的错误

[英]Arithmetic overflow error converting varchar to data type numeric. Error in asp.net

I know there might be several reference to the error which I mentioned in question, but seriously I tried and it didn't worked for me. 我知道可能有多次提到我所提到的错误的信息,但我认真地尝试了一下,但对我没有用。

I have a code which works when is for adding menu dynamically . 我有一个代码,该代码在adding menu dynamically So, if the menu is in the Root, its Parent_Mkey will always be 0. All works fine but when I debug the below line 因此,如果菜单位于Root中,则其Parent_Mkey始终为0。一切正常,但是当我调试以下行时

MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, strMkey, "WMS_Menu_Hdr", "MKEY", "MEH", StrPubBldg.ToString());

I get error as 我得到错误

Arithmetic overflow error converting varchar to data type numeric. 将varchar转换为数值数据类型的算术溢出错误。

Here is my code below 这是我的下面的代码

public bool Save()
{
    int strMkey;
    if (txt_menu_name.Value == "" && txt_url_name.Text == "" && HidSelNode.Value != "0")
    {
        strMode = "M";
        strMkey = Convert.ToInt32(HidSelNode.Value);
    }
    else
    {
        strMode = "A";
        strMkey = 0;
    }
    if (1 == 1)
    {
        SqlConnection ObjPriCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlConn"].ToString());
        ObjPriCon.Open();

        StringBuilder StrPubBldg = new StringBuilder();
        XmlWriter xw = XmlWriter.Create(StrPubBldg);
        xw.WriteStartElement("DocumentElement");
        xw.WriteStartElement("WMS_Menu_Hdr");
        xw.WriteElementString("DELETE_FLAG", "N");
        if (strMode == "M")
        {
            xw.WriteElementString("MENU_NAME", txt_selnode.Text);
            xw.WriteElementString("URL", txt_selurl.Text);
        }
        else
        {
            string result;
            string strquery = "select max(order_by) +10 order_by from WMS_Menu_Hdr where PARENT_MKEY=@PARENT_MKEY";
            SqlCommand ObjPriCmd = new SqlCommand(strquery, ObjPriCon);
            if (HidSelNode.Value == "0")
            {
                ObjPriCmd.Parameters.AddWithValue("@PARENT_MKEY", HidSelNode.Value);
                result = ObjPriCmd.ExecuteScalar().ToString();
            }
            else
            {
                ObjPriCmd = new SqlCommand("select ISNULL(a.order_by, max(b.order_by) +10) next_orderby " +
                                  " from WMS_Menu_Hdr b," +
                                 "(select max(order_by) +10 order_by from WMS_Menu_Hdr  where PARENT_MKEY= '" + HidSelNode.Value + "') a " +
                                 " where mkey='" + HidSelNode.Value + "' group by a.order_by", ObjPriCon);
                strquery = ObjPriCmd.ExecuteScalar().ToString();
            }
            xw.WriteElementString("MENU_NAME", txt_menu_name.Value);
            xw.WriteElementString("URL", txt_url_name.Text);
            xw.WriteElementString("CREATION_DATE", System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
            xw.WriteElementString("PARENT_MKEY", HidSelNode.Value);
            xw.WriteElementString("ORDER_BY", strquery);
        }

        xw.WriteEndElement();
        xw.Close();

        MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, strMkey, "WMS_Menu_Hdr", "MKEY", "MEH", StrPubBldg.ToString());
    }
    if (MainEnqMkey.Equals(0))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "alert('Some Error Occured While Saving Data !!')", true);
        return false;
    }
    else
    {
        return true;
    }
}

Please suggest what is wrong here 请在这里提出问题

Problem is that your PARENT_MKEY is numeric , but you inserting '0' which is varchar . 问题是您的PARENT_MKEYnumeric ,但是您插入的是'0' ,即varchar

I believe that SQL should look like ( '0' => 0 ): 我认为SQL应该看起来像( '0' => 0 ):

insert into WMS_Menu_Hdr (MKEY,DELETE_FLAG,MENU_NAME,URL,CREATION_DATE,PARENT_MKEY,ORDER_BY) values(44,'N','WMS Root','test.aspx','01-12-2015 04:51:40',0,'select max(order_by) +10 order_by from WMS_Menu_Hdr where PARENT_MKEY=@PARENT_MKEY') 

Update. 更新。

Also your ORDER_BY field is numeric when you inserting varchar . 当您插入varchar时,您的ORDER_BY字段也是numeric So you need either to think of a number that will represent select max(order_by) +10 order_by from WMS_Menu_Hdr where PARENT_MKEY=@PARENT_MKEY or change type of ORDER_BY to varchar 因此,您需要考虑一个代表select max(order_by) +10 order_by from WMS_Menu_Hdr where PARENT_MKEY=@PARENT_MKEY或将ORDER_BY类型更改为varchar

暂无
暂无

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

相关问题 在var.net中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in asp.net 将varchar转换为数值数据类型的算术溢出错误。 尝试从数据库读取时并非每次都会发生 - Arithmetic overflow error converting varchar to data type numeric. when trying to read from a database DOES NOT happen every time 将数据类型varchar转换为数字时出错。 2014年1月17日 - Error converting data type varchar to numeric. 1-17-2014 “将数据类型 nvarchar 转换为数字时出错。” - “Error converting data type nvarchar to numeric.” 在SQL Server 2008中将varchar转换为数值数据类型时发生算术溢出错误 - An arithmetic overflow error occurred while converting varchar to numeric data type in SQL Server 2008 将数据类型varchar转换为数字时出错。 我使用了varchar数据类型,并尝试插入18-24值 - Error converting data type varchar to numeric. I used varchar data type and trying to insert 18-24 value 流利的NHibernate在插入/保存时导致“将数字转换为数据类型数字的算术溢出错误” - Fluent NHibernate cause an “Arithmetic overflow error converting numeric to data type numeric” on insert/save c# 中的 ExecuteNonQuery() 抛出异常 - 将数字转换为数据类型数字的算术溢出错误 - ExecuteNonQuery() from c# throwing exception - arithmetic overflow error converting numeric to data type numeric SQLException:将数据类型varchar转换为数值时出错 - SQLException: Error converting data type varchar to numeric 错误System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM