使用文本框在数据库中插入布尔值 [英] Insert Boolean in a database using textbox

查看:87
本文介绍了使用文本框在数据库中插入布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试使用文本框插入布尔值,现在出现一个错误,提示字符串未被识别为有效的布尔值".因为我在文本框中添加了0.

以下是我的业务层中用于插入值的方法

 公共  void  insertNewProduct(产品产品)
        {
            使用(SqlConnection conn =  SqlConnection(ConnectionString))
            {
                SqlCommand cmd =  SqlCommand(" ,conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.NVarChar," ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.Int));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.Int));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.NVarChar," ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.Money));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.SmallInt));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.SmallInt));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.SmallInt));
                cmd.Parameters [" ].Value = products.ProductName;

                cmd.Parameters.Add( SqlParameter(" ,SqlDbType.Bit));
                cmd.Parameters [" ].Value = products.ProductName;

                尝试
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                捕获(异常exp)
                {
                    抛出  ApplicationException(exp.Message);
                }


            } 




现在下面是我的客户表格

  public  ProductsBL productBL;
         public  ArrayList prodList;
        公共 Form1()
        {
            InitializeComponent();
             productBL =新的ProductsBL();
        } 




 私有  void  btnInsert_Click(对象发​​件人,EventArgs e)
        {
    Products产品=  Products(Convert.ToString(txtProdName.Text),
                                    Convert.ToInt32(txtSupplID.Text),Convert.ToInt32(txtCatID.Text),txtQtyPerUnit.Text,Convert.ToDouble(txtUnitPrice.Text),Convert.ToInt16(txtUnitInStock.Text),Convert.ToInt16(txtUntOnOrder.Text),转换.ToInt16(txtReOrderLevel.Text),Convert.ToBoolean(txtDiscontinue.Text));
            productBL.insertNewProduct(产品);
        } 

解决方案

是的,我同意Koushik,您正在尝试将0转换为布尔值,将无法正常工作.
为什么不对true或false字段使用复选框,这对于UI端更有意义.
然后,您可以将布尔值字段设置为复选框isChecked值.
另一方面,如果您仍然想使用文本框,则可以尝试编写类似
的内容

<br />
    (txtDiscontinue.Text =="0" )? false:true<br />



这将处理您的请求,但这不是最好的方法,因为文本框可以接受广泛的值

似乎在更新查询中的某个时刻,期望布尔值,但它得到一个字符串值.

如果尝试在数据库中保存"true"或"false"值,则应将字符串值转换为布尔值.

convert.ToBoolean("true")
(or) 
convert.ToBoolean("flase")



someField.text的值很可能是"true"或"false".


hi guys am trying to insert a boolean value using a textbox, now i get an error that says "String was not recognized as a valid Boolean." because i added 0 in textbox.

Below is a method in my business layer to insert the values

public void insertNewProduct(Products products)
        {
            using(SqlConnection conn=new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("procInsertNewProduct", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@productName", SqlDbType.NVarChar, 40));
                cmd.Parameters["@productName"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@suppID", SqlDbType.Int));
                cmd.Parameters["@suppID"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@catID", SqlDbType.Int));
                cmd.Parameters["@catID"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@qtyPerUnit", SqlDbType.NVarChar, 20));
                cmd.Parameters["@qtyPerUnit"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@unitPrice", SqlDbType.Money));
                cmd.Parameters["@unitPrice"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@unitsInStock", SqlDbType.SmallInt));
                cmd.Parameters["@unitsInStock"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@unitsOnOrder", SqlDbType.SmallInt));
                cmd.Parameters["@unitsOnOrder"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@reOrderLevel", SqlDbType.SmallInt));
                cmd.Parameters["@productName"].Value = products.ProductName;

                cmd.Parameters.Add(new SqlParameter("@disco", SqlDbType.Bit));
                cmd.Parameters["@productName"].Value = products.ProductName;

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    throw new ApplicationException(exp.Message);
                }


            }




Now below is my client Form

public ProductsBL productBL;
        public ArrayList prodList;
        public Form1()
        {
            InitializeComponent();
             productBL = new ProductsBL();
        }




private void btnInsert_Click(object sender, EventArgs e)
        {
    Products products = new Products(Convert.ToString( txtProdName.Text),  
                                    Convert.ToInt32(txtSupplID.Text),  Convert.ToInt32(txtCatID.Text), txtQtyPerUnit.Text, Convert.ToDouble(txtUnitPrice.Text), Convert.ToInt16(txtUnitInStock.Text), Convert.ToInt16(txtUntOnOrder.Text), Convert.ToInt16(txtReOrderLevel.Text), Convert.ToBoolean(txtDiscontinue.Text));
            productBL.insertNewProduct(products);
        }

解决方案

Yeah i agree with Koushik, you are trying to convert 0 to a boolean,it wont work.
Why not use a checkbox for the true or false field ,it makes more sense for the UI end.
Then you can just set your boolean field to the checkboxs isChecked value.
On the other hand if you still want to go with the textbox,you could try and write something like

<br />
    (txtDiscontinue.Text =="0" )? false:true<br />



This will handle your request,but this is not the best way as a textbox can accept a wide range of value


It seems that at some point in your update query, an boolean value is expected, but it gets a string value.

If you try to save a "true" or "false" value in the database, you should convert the string value to a boolean value.

convert.ToBoolean("true")
(or) 
convert.ToBoolean("flase")



It''s most likely that the value of someField.text will be "true" or "false".


这篇关于使用文本框在数据库中插入布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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