我有一个文本框和日期时间,我可以输入一个整数。我希望将其存储在SQL Server数据库中。如果我什么也没输入,我会存储NULL
。
但是,如果文本框留空,则会收到错误输入字符串的格式不正确。
我该如何解决这个问题,并将空值纳入数据库?
代码如下:
public void Add_ItemSeasonalPrices(string ItemCode, DateTime FromDate, DateTime ToDate, decimal WholeSaleForFirstUnit, decimal WholeSaleForSecondUnit, decimal WholeSaleForThirdUnit,
decimal HalfWholeSaleForFirstUnit, decimal HalfWholeSaleForSecondUnit, decimal HalfWholeSaleForThirdUnit,
decimal DistributorForFirstUnit, decimal DistributorForSecondUnitt)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
SqlParameter[] param = new SqlParameter[22];
param[0] = new SqlParameter("@ItemCode", SqlDbType.NVarChar, 25);
param[0].Value = ItemCode;
param[1] = new SqlParameter("@FromDate", SqlDbType.DateTime);
param[1].Value = FromDate;
param[2] = new SqlParameter("@ToDate", SqlDbType.DateTime);
param[2].Value = ToDate;
param[3] = new SqlParameter("@WholeSaleForFirstUnit", SqlDbType.Decimal);
param[3].Value = WholeSaleForFirstUnit;
param[4] = new SqlParameter("@WholeSaleForSecondUnit", SqlDbType.Decimal);
param[4].Value = WholeSaleForSecondUnit;
param[5] = new SqlParameter("@WholeSaleForThirdUnit ", SqlDbType.Decimal);
param[5].Value = WholeSaleForThirdUnit;
param[6] = new SqlParameter("@HalfWholeSaleForFirstUnit", SqlDbType.Decimal);
param[6].Value = HalfWholeSaleForFirstUnit;
param[7] = new SqlParameter("@HalfWholeSaleForSecondUnit", SqlDbType.Decimal);
param[7].Value = HalfWholeSaleForSecondUnit;
param[8] = new SqlParameter("@HalfWholeSaleForThirdUnit", SqlDbType.Decimal);
param[8].Value = HalfWholeSaleForThirdUnit;
param[9] = new SqlParameter("@DistributorForFirstUnit", SqlDbType.Decimal);
param[9].Value = DistributorForFirstUnit;
param[10] = new SqlParameter("@DistributorForSecondUnit", SqlDbType.Decimal);
param[10].Value = DistributorForSecondUnit;
DAL.ExecuteCommand("Add_ItemSeasonalPrices", param);
DAL.close();
}
和btnSave中的此代码
Item.Add_ItemSeasonalPrices(txt_ItemCode.Text, Convert.ToDateTime(FromDate.Text), Convert.ToDateTime(ToDate.Text), Convert.ToDecimal(txt_WholeSaleForFirstUnit.Text), Convert.ToDecimal(txt_WholeSaleForSecondUnit.Text),
Convert.ToDecimal(txt_WholeSaleForThirdUnit.Text), Convert.ToDecimal(txt_HalfWholeSaleForFirstUnit.Text),
Convert.ToDecimal(txt_HalfWholeSaleForSecondUnit.Text), Convert.ToDecimal(txt_HalfWholeSaleForThirdUnit.Text),
Convert.ToDecimal(txt_DistributorForFirstUnit.Text), Convert.ToDecimal(txt_DistributorForSecondUnit.Text));
和这张表
CREATE TABLE [dbo].[ItemSeasonalPrices](
[ItemCode] [nvarchar](25) NOT NULL,
[FromDate] [date] NULL,
[ToDate] [date] NULL,
[WholeSaleForFirstUnit] [decimal](18, 3) NULL,
[WholeSaleForSecondUnit] [decimal](18, 3) NULL,
[WholeSaleForThirdUnit] [decimal](18, 3) NULL,
[HalfWholeSaleForFirstUnit] [decimal](18, 3) NULL,
[HalfWholeSaleForSecondUnit] [decimal](18, 3) NULL,
[HalfWholeSaleForThirdUnit] [decimal](18, 3) NULL,
[DistributorForFirstUnit] [decimal](18, 3) NULL,
[DistributorForSecondUnit] [decimal](18, 3) NULL,
[DistributorForThirdUnit] [decimal](18, 3) NULL,
[ExportForFirstUnit] [decimal](18, 3) NULL,
[ExportForSecondUnit] [decimal](18, 3) NULL,
[ExportForThirdUnit] [decimal](18, 3) NULL,
[RetailForFirstUnit] [decimal](18, 3) NULL,
[RetailForSecondUnit] [decimal](18, 3) NULL,
[RetailForThirdUnit] [decimal](18, 3) NULL,
[EndUserForFirstUnit] [decimal](18, 3) NULL,
[EndUserForSecondUnit] [decimal](18, 3) NULL,
[EndUserForThirdUnit] [decimal](18, 3) NULL,
[PriceDefault] [int] NULL
) ON [PRIMARY]
答案 0 :(得分:0)
您收到Format Exception
(输入字符串格式不正确),因为至少有一个文本框不包含有效小数。
您可以对所有变量使用Decimal.TryParse
而不是Convert.ToDecimal
。
decimal exportForFirstUnit;
decimal.TryParse(textBox1.Text, out exportForFirstUnit);
或者,您可能必须对这些字段进行一些验证,并至少检查文本框是否包含值。
另一个问题是你的逻辑是有缺陷的。请在代码中考虑以下几行:
param[1] = new SqlParameter("@FromDate", SqlDbType.DateTime);
param[1].Value = FromDate;
param[1].Value = DBNull.Value;
param[2] = new SqlParameter("@ToDate", SqlDbType.DateTime);
param[2].Value = ToDate;
param[2].Value = DBNull.Value;
您正确声明FromDate
和ToDate
,然后将SQL参数设置为具有文本框中的值,然后使用NULL
将此值设置为over,因此每个{{1}执行表的语句将始终不包含数据,而不是用户通过表单输入的数据。
作为附加说明,您的INSERT
似乎需要参考很多参数,最好将它放在一个类(SeasonalItems)中,这样可以更容易使用。
Add_ItemSeasonalPrices
然后您可以将其用作方法的参数,如下所示:
public class SeasonalItems
{
public decimal WholeSaleForFirstUnit { get; set; }
public decimal WholeSaleForSecondUnit { get; set; }
}
随着应用程序的增长,您可以进一步扩展该类以容纳更多单元,但我相信这就足够了。
答案 1 :(得分:0)
您应该考虑修改代码。
修改Add_ItemSeasonalPrices
定义以接受nullable
DateTime
值。
public void Add_ItemSeasonalPrices(string ItemCode, DateTime? FromDate, DateTime? ToDate, decimal WholeSaleForFirstUnit, decimal WholeSaleForSecondUnit, decimal WholeSaleForThirdUnit,
decimal HalfWholeSaleForFirstUnit, decimal HalfWholeSaleForSecondUnit, decimal HalfWholeSaleForThirdUnit,
decimal DistributorForFirstUnit, decimal DistributorForSecondUnit, decimal DistributorForThirdUnit,
decimal ExportForFirstUnit, decimal ExportForSecondUnit, decimal ExportForThirdUnit,
decimal RetailForFirstUnit, decimal RetailForSecondUnit, decimal RetailForThirdUnit,
decimal EndUserForFirstUnit, decimal EndUserForSecondUnit, decimal EndUserForThirdUnit, int PriceDefault)
{
.....
}
在btn_save
中,在转换为DateTime
Item.Add_ItemSeasonalPrices(txt_ItemCode.Text,
string.IsNullOrEmpty(FromDate.Text)? null: Convert.ToDateTime(FromDate.Text),
string.IsNullOrEmpty(ToDate.Text)? null: Convert.ToDateTime(ToDate.Text), Convert.ToDecimal(txt_WholeSaleForFirstUnit.Text), Convert.ToDecimal(txt_WholeSaleForSecondUnit.Text),
Convert.ToDecimal(txt_WholeSaleForThirdUnit.Text), Convert.ToDecimal(txt_HalfWholeSaleForFirstUnit.Text),
Convert.ToDecimal(txt_HalfWholeSaleForSecondUnit.Text), Convert.ToDecimal(txt_HalfWholeSaleForThirdUnit.Text),
Convert.ToDecimal(txt_DistributorForFirstUnit.Text), Convert.ToDecimal(txt_DistributorForSecondUnit.Text), Convert.ToDecimal(txt_DistributorForThirdUnit.Text),
Convert.ToDecimal(txt_ExportForFirstUnit.Text), Convert.ToDecimal(txt_ExportForSecondUnit.Text), Convert.ToDecimal(txt_ExportForThirdUnit.Text),
Convert.ToDecimal(txt_RetailForFirstUnit.Text), Convert.ToDecimal(txt_RetailForSecondUnit.Text), Convert.ToDecimal(txt_RetailForThirdUnit.Text),
Convert.ToDecimal(txt_EndUserForFirstUnit.Text), Convert.ToDecimal(txt_EndUserForSecondUnit.Text), Convert.ToDecimal(txt_EndUserForThirdUnit.Text),
PriceDefault);
最后但并非最不重要的是,代码覆盖了每个parameter
值。
param[1] = new SqlParameter("@FromDate", SqlDbType.DateTime);
param[1].Value = FromDate.HasValue? FromDate.Value : DBNull.Value;
//param[1].Value = DBNull.Value;
param[2] = new SqlParameter("@ToDate", SqlDbType.DateTime);
param[2].Value = ToDate.HasValue? ToDate.Value : DBNull.Value;
//param[2].Value = DBNull.Value;