title: Arithmetic overflow error converting varchar to data type numeric. Error in asp.net
body: <p>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.</p>
  
  <p>I have a code which works when is for <code>adding menu dynamically</code>. So, if the menu is in the Root, its <code>Parent_Mkey</code> will always be 0. All works fine but when I debug the below line</p>
  
  <pre><code>MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, strMkey, "WMS_Menu_Hdr", "MKEY", "MEH", StrPubBldg.ToString());
  </code></pre>
  
  <p>I get error as</p>
  
  <blockquote>
    <p>Arithmetic overflow error converting varchar to data type numeric. </p>
  </blockquote>
  
  <p>Here is my code below</p>
  
  <pre><code>public bool Save()
  {
      int strMkey;
      if (txt_menu_name.Value == "" &amp;&amp; txt_url_name.Text == "" &amp;&amp; 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;
      }
  }
  </code></pre>
  
  <p>Please suggest what is wrong here</p>