将varchar转换为数据类型numeric时发生算术溢出错误.asp.net中出现错误

本文关键字:错误 溢出 asp net 转换 varchar 数据类型 numeric | 更新日期: 2023-09-27 18:25:07

我知道可能有几个引用了我提到的错误,但我认真地尝试了一下,但它对我不起作用。

我有一个适用于adding menu dynamically的代码。因此,如果菜单在根目录中,其Parent_Mkey将始终为0。所有工作都很好,但当我调试下面的时

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

我得到错误作为

将varchar转换为数据类型numeric时发生算术溢出错误。

这是我在下面的代码

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;
    }
}

请在这里建议出了什么问题

将varchar转换为数据类型numeric时发生算术溢出错误.asp.net中出现错误

问题是您的PARENT_MKEYnumeric,但您插入的'0'varchar

我认为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') 

更新

插入varchar时,您的ORDER_BY字段也是numeric。因此,您需要考虑一个表示select max(order_by) +10 order_by from WMS_Menu_Hdr where PARENT_MKEY=@PARENT_MKEY的数字,或者将ORDER_BY的类型更改为varchar