2016-04-16 6 views
2

テキストボックスとdatetimeは整数を入力できます。私はこれをSQL Serverデータベースに保存したいと思います。何も入力しなければ、NULLを保存します。 ただし、テキストボックスを空白のままにすると、入力文字列が正しい形式でないというエラーが表示されます。テキストボックスから整数フィールドにnullを挿入する方法

どうすればこの問題を回避でき、dbを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

を使用すると、コードを簡略化してポイントにする必要があります。多くの人がこのコードをすべて読んでもらうことはありません。 – FLICKER

答えて

0

あなたはFormat Exception(入力文字列が正しいではありませんでした受信、次のように

コードがありますこれは、少なくとも1つのテキストボックスに有効な小数点が含まれていないためです。

すべての変数にConvert.ToDecimalではなくDecimal.TryParseを使用できます。

decimal exportForFirstUnit; 
decimal.TryParse(textBox1.Text, out exportForFirstUnit); 

また、これらのフィールドにいくつかの検証を行い、少なくともテキストボックスに値が含まれているかどうかを確認する必要があります。

もう1つの問題は、ロジックに欠陥があることです。あなたはNULLで、この値ovveride、その後、正しくFromDateToDateを宣言して、テキストボックスの値を持つようにSQLパラメータを設定

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; 

、したがって、あなたが実行した各INSERT for文:あなたのコードでこれらの行を考えてみましょうユーザーのフォームに入力したデータではなく、データには常に表が含まれます。

Add_ItemSeasonalPricesには多くのパラメータが含まれているように見えますが、これをクラス(SeasonalItems)で実行すると、作業がはるかに簡単になる場合があります。

public class SeasonalItems 
{ 
    public decimal WholeSaleForFirstUnit { get; set; } 
    public decimal WholeSaleForSecondUnit { get; set; } 
} 

そして、以下に示すように、あなたは、メソッドのパラメータとしてこれを使用することができます。

public void AddItemSeasonalPrices(SeasonalItems items) 
{ 

} 

あなたは、アプリケーションが大きくなるにつれてより多くのユニットを収容するために、さらにクラスを拡張することもできますが、私はこれを信じます今は十分だろう。

0

コードを変更することを検討してください。

Add_ItemSeasonalPricesの定義を受け入れてnullableDateTimeの値を変更します。

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; 
関連する問題