大家好我试图使用文本框插入一个布尔值,现在我得到一个错误,表示"字符串未被识别为有效的布尔值。"因为我在文本框中添加了0。
以下是我的业务层中插入值
的方法public void ADD_CUSTOMER(string Customer_Code, string Customer_Nmae, string Address, string Country, string City, string Phone, string Fax, string Mobile,
string Email, string Note, byte[] Cust_Imaige, decimal Credit, decimal Debit, decimal Balance, bool Statas, string criterion)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
SqlParameter[] param = new SqlParameter[16];
param[0] = new SqlParameter("@Customer_Code", SqlDbType.NVarChar, 25);
param[0].Value = Customer_Code;
param[1] = new SqlParameter("@Customer_Nmae", SqlDbType.NVarChar, 100);
param[1].Value = Customer_Nmae;
param[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 100);
param[2].Value = Address;
param[3] = new SqlParameter("@Country", SqlDbType.NVarChar, 100);
param[3].Value = Country;
param[4] = new SqlParameter("@City", SqlDbType.NVarChar, 100);
param[4].Value = City;
param[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
param[5].Value = Phone;
param[6] = new SqlParameter("@Fax", SqlDbType.NVarChar, 100);
param[6].Value = Fax;
param[7] = new SqlParameter("@Mobile", SqlDbType.NVarChar, 100);
param[7].Value = Mobile;
param[8] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
param[8].Value = Email;
param[9] = new SqlParameter("@Note", SqlDbType.NVarChar, 100);
param[9].Value = Note;
param[10] = new SqlParameter("@Cust_Imaige", SqlDbType.Image);
param[10].Value = Cust_Imaige;
param[11] = new SqlParameter("@Credit", SqlDbType.Decimal);
param[11].Value = Credit;
param[12] = new SqlParameter("@Debit", SqlDbType.Decimal);
param[12].Value = Debit;
param[13] = new SqlParameter("@Balance", SqlDbType.Decimal);
param[13].Value = Balance;
param[14] = new SqlParameter("@Statas", SqlDbType.Bit);
param[14].Value = Statas;
param[15] = new SqlParameter("@criterion", SqlDbType.NVarChar, 50);
param[15].Value = criterion;
DAL.ExecuteCommand("ADD_CUSTOMER", param);
DAL.close();
}
现在下面是我的客户表格
private void btn_Add_Click(object sender, EventArgs e)
{
try
{
byte[] Cust_Imaige;
if (pictureBox1.Image == null)
{
Cust_Imaige = new byte[0];
Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
txt_Country.Text, txt_City.Text,txt_Phone.Text,txt_Fax.Text,txt_Mobail.Text,
txt_Email.Text, txt_Note.Text, Cust_Imaige,Convert.ToDecimal(txt_Credit.Text),
Convert.ToDecimal(txt_Debit.Text),Convert.ToDecimal(txt_Balance.Text),Convert.ToBoolean(txt_Statas.Text), "without_Image");
MessageBox.Show("added","add",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MemoryStream ms = new MemoryStream();
pictureBox2.Image.Save(ms, pictureBox2.Image.RawFormat);
Cust_Imaige = ms.ToArray();
Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
txt_Country.Text, txt_City.Text, txt_Phone.Text, txt_Fax.Text, txt_Mobail.Text,
txt_Email.Text, txt_Note.Text, Cust_Imaige, Convert.ToDecimal(txt_Credit.Text),
Convert.ToDecimal(txt_Debit.Text), Convert.ToDecimal(txt_Balance.Text), Convert.ToBoolean(txt_Statas.Text), "with_Image");
MessageBox.Show("add","",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch
{
return;
}
finally
{
btn_Add.Enabled = false;
btn_New.Enabled = true;
}
}
答案 0 :(得分:3)
Convert.ToBoolean(txt_Statas.Text)
无法使用1
和0
您应该使用Checkbox
并返回Checkbox.Checked
或者如果你想保留TextBox
那么
将Convert.ToBoolean(txt_Statas.Text)
替换为txt_Statas.Text == "1" ? true : false
答案 1 :(得分:0)
我猜一个复选框更适合那个,因为复选框的值是一个布尔值。
无论如何,如果您想继续使用TextBox,请执行以下操作。
txt_Statas.Text == "1"
您更改的代码。
Customer.ADD_CUSTOMER(txt_Customer_Code.Text, txt_Customer_Nmae.Text, txt_Address.Text,
txt_Country.Text, txt_City.Text, txt_Phone.Text, txt_Fax.Text, txt_Mobail.Text,
txt_Email.Text, txt_Note.Text, Cust_Imaige, Convert.ToDecimal(txt_Credit.Text),
Convert.ToDecimal(txt_Debit.Text), Convert.ToDecimal(txt_Balance.Text), txt_Statas.Text == "1", "with_Image");
答案 2 :(得分:0)
通过
转换为布尔值时Convert.ToBoolean(txt_Statas.Text)
例程需要"True"
或"False"
形式的参数,而不是"0"
或"1"
:
https://msdn.microsoft.com/en-us/library/86hw82a3(v=vs.110).aspx
在你的情况下只是把
txt_Statas.Text == "1"
而不是Convert.ToBoolean(txt_Statas.Text)
。
答案 3 :(得分:0)
使用扩展方法创建一个通用实用程序类:
internal static class ConvertExtension {
public static bool ToBoolean(this object value) {
if (value == null or DBNull.Value.Equals(value)) return false;
if (value is bool) return (bool)value;
string obj = value.ToString(CultureInfo.InvariantCulture);
return !(string.IsNullOrEmpty(obj)
|| string.Equals(obj, "0")
|| string.Equals("false", obj, StringComparison.OrdinalIgnoreCase));
}
}
<强>使用强>
将Convert.ToBoolean(txt_Statas.Text)
修改为txt_Statas.Text.ToBoolean()