获取错误:无法在cmnd.ExecuteNonQuery();

时间:2018-03-19 05:54:56

标签: asp.net

我通过相同的按钮插入和更新记录。但我希望主键(req_no)仅在插入操作期间自动生成,而不是在更新时生成。怎么做 ?  获取错误:无法将参数值从String转换为Decimal at cmnd.ExecuteNonQuery(); 这是代码:

    public partial class Service_master : System.Web.UI.Page
       {

    OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["constring"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {


        Session["UserName"] = "laxmi";
        this.TextBox9.Text = DateTime.Today.ToString("dd/MMM/yyyy");
        lb1.Text = Session["UserName"].ToString();
          if (!IsPostBack)
        {
            BindDropDownList();
            BindDropDowns();
            Bindservcd();


        }
    }


       private void Autogenrate()
        {
           int r;

            try
            {
            con.Open();
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            string sqlcmd = "Select max(req_no)as req_no from service_master";
            OleDbCommand cmd = new OleDbCommand(sqlcmd, con);
            OleDbDataReader dr = cmd.ExecuteReader();
           if (dr.Read())
        {

            string d = dr[0].ToString();
            if (d == "")
            {
            TextBox1.Text = "1" ;
            }
                else
            {

                r = Convert.ToInt32(dr[0]);
                r = r + 1;
                TextBox1.Text = r.ToString();
            }
        }
                con.Close();

            }
                   catch (Exception ex)
                        {
                       WebMsgBox.Show("null is not allowed");
                            }

        }
 public bool uniquereq_no(string req_no)
{
        string strreqno;
        string querye = "select count(req_no) as req_no from service_master where req_no='"+ this.TextBox1.Text.ToString()+"'" ;
        OleDbCommand cmnd = new OleDbCommand(querye, con);
        OleDbDataReader dr;
        dr = cmnd.ExecuteReader();
        while (dr.Read())
        {
            try
            {
                strreqno = dr["req_no"].ToString();
                return (strreqno != "0");
                if (strreqno != "0")
                {
                    WebMsgBox.Show("already exists");
                    //errlblemail.Text = "email already exist";
                    return false;
                }

            }
            catch (Exception e)
            {
                string message = "error";
                message += e.Message;
            }
            finally
            {
                dr.Close();
            }
        }

        return true;


    }
  protected void Button1_Click(object sender, EventArgs e)
    {
        string UserName = "UserName";
        Session["UserName"] = lb1.Text;

        TextBox TextBox1 = (TextBox)FindControl("TextBox1");
        TextBox TextBox9 = (TextBox)FindControl("TextBox9");
        TextBox TextBox2 = (TextBox)FindControl("TextBox2");
        TextBox TextBox3 = (TextBox)FindControl("TextBox3");
        TextBox TextBox4 = (TextBox)FindControl("TextBox4");
        DropDownList DropDownList3 = (DropDownList)FindControl("DropDownList3");
        DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
        TextBox TextBox5 = (TextBox)FindControl("TextBox5");
        TextBox TextBox6 = (TextBox)FindControl("TextBox6");
        DropDownList DropDownList2 = (DropDownList)FindControl("DropDownList2");
        TextBox TextBox7 = (TextBox)FindControl("TextBox7");
        TextBox TextBox8 = (TextBox)FindControl("TextBox8");


        {
            con.Open();
            OleDbCommand cmnd = con.CreateCommand();
            cmnd.CommandType = CommandType.StoredProcedure;
            if (uniquereq_no(TextBox1.Text)== true)
            {
                cmnd.CommandText = "upd_ser_mas";
                cmnd.Parameters.Add("xreq_no", OleDbType.Numeric, 8).Value = 
                  Convert.ToString(this.TextBox1.Text);
                cmnd.Parameters.Add("xreq_dt", OleDbType.Date).Value = 
           Convert.ToDateTime(TextBox9.Text);
                cmnd.Parameters.Add("xreq_by", OleDbType.VarChar, 7).Value = Convert.ToString(Session["UserName"]);
                cmnd.Parameters.Add("xser_cd", OleDbType.VarChar, 16).Value = Convert.ToString(TextBox3.Text);
                cmnd.Parameters.Add("xserv_desc", OleDbType.VarChar, 40).Value = Convert.ToString(TextBox4.Text);
                cmnd.Parameters.Add("xserv_grp_cd", OleDbType.VarChar, 3).Value = Convert.ToString(DropDownList3.SelectedItem.Value);
                cmnd.Parameters.Add("xbase_uom_cd", OleDbType.VarChar, 3).Value = Convert.ToString(DropDownList1.SelectedItem.Value);
                cmnd.Parameters.Add("xsac_cd", OleDbType.VarChar, 16).Value = Convert.ToString(TextBox5.Text);
                cmnd.Parameters.Add("xser_long_desc", OleDbType.VarChar, 100).Value = Convert.ToString(TextBox6.Text);
                cmnd.Parameters.Add("xtax_ind", OleDbType.Char, 1).Value = Convert.ToString(DropDownList2.SelectedItem.Value);
                cmnd.Parameters.Add("xactive_ind", OleDbType.Char, 1).Value = Convert.ToString(TextBox7.Text);
                cmnd.Parameters.Add("xdel_ind", OleDbType.Char, 1).Value = Convert.ToString(TextBox8.Text);
                WebMsgBox.Show("Data Successfully Updated");
            }
            else
            {
                Autogenrate();
                cmnd.CommandText = "ins_ser_mas";
                cmnd.Parameters.Add("xreq_no", OleDbType.Numeric, 8).Value = Convert.ToString(this.TextBox1.Text);
                cmnd.Parameters.Add("xreq_dt", OleDbType.Date).Value = Convert.ToDateTime(TextBox9.Text);
                cmnd.Parameters.Add("xreq_by", OleDbType.VarChar, 7).Value = Convert.ToString(Session["UserName"]);
                cmnd.Parameters.Add("xser_cd", OleDbType.VarChar, 16).Value = Convert.ToString(TextBox3.Text);
                cmnd.Parameters.Add("xserv_desc", OleDbType.VarChar, 40).Value = Convert.ToString(TextBox4.Text);
                cmnd.Parameters.Add("xserv_grp_cd", OleDbType.VarChar, 3).Value = Convert.ToString(DropDownList3.SelectedItem.Value);
                cmnd.Parameters.Add("xbase_uom_cd", OleDbType.VarChar, 3).Value = Convert.ToString(DropDownList1.SelectedItem.Value);
                cmnd.Parameters.Add("xsac_cd", OleDbType.VarChar, 16).Value = Convert.ToString(TextBox5.Text);
                cmnd.Parameters.Add("xser_long_desc", OleDbType.VarChar, 100).Value = Convert.ToString(TextBox6.Text);
                cmnd.Parameters.Add("xtax_ind", OleDbType.Char, 1).Value = Convert.ToString(DropDownList2.SelectedItem.Value);
                cmnd.Parameters.Add("xactive_ind", OleDbType.Char, 1).Value = Convert.ToString(TextBox7.Text);
                cmnd.Parameters.Add("xdel_ind", OleDbType.Char, 1).Value = Convert.ToString(TextBox8.Text);
                WebMsgBox.Show("The data for request number" + TextBox1.Text + "is saved");
            }
            cmnd.ExecuteNonQuery();
            con.Close();
        }

        }

2 个答案:

答案 0 :(得分:1)

OleDbType.Numeric的{​​{1}}等效数据类型为OleDbType enumeration reference。您需要对此行使用System.Decimaldecimal.Parse进行十进制转换,因为Convert.ToDecimal返回字符串值:

TextBox1.Text

或者您可以使用更安全的cmnd.Parameters.Add("xreq_no", OleDbType.Numeric, 8).Value = decimal.Parse(this.TextBox1.Text); 方法:

decimal.TryParse

答案 1 :(得分:0)

您的代码中的少数内容可以更正

1)无需使用ExecuteReader。请改用ExecuteScalar。因为只返回一个值。 (在下面第2点之后提到的SQL中)

2)更改了sql查询以执行+1部分代码。 (如果您的数据库支持ISNull)。否则你的数据库中可能会有类似的东西。

//SQL query will find max and will do +1
var sqlcmd = "Select IsNull(max(req_no),0) + 1 as req_no from service_master";
var cmd = new OleDbCommand(sqlcmd, con);
var outval  = cmd.ExecuteScalar();
       //now outval will have value
TextBox1.Text = outval.ToString();

3)您正在为参数分配string值,但分配的类型不同。

cmnd.Parameters.Add("xreq_no", OleDbType.Numeric, 8).Value = Convert.ToString(this.TextBox1.Text);

TextBox1.Text本身是string,您不必使用Convert.ToString。或者使用正确的转换,例如Convert.ToInt32等,因为您的参数是OledbType.Numeric等等

另外,为什么必须使用FindControl?删除FindControl

TextBox TextBox1 = (TextBox)FindControl("TextBox1");

TextBox1可以直接引用。