2017-06-09 3 views
1

Beginner in .net:DataGridを使用してExcelシートを表示しています.Nullレコードがある場合、このエラーメッセージが表示されます "System.FormatException: 。日時有効」」DataGridからデータテーブルにデータ型を変換する際にエラーが発生しました。

コード:

for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
    { 

     command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text); 
     command.Parameters.AddWithValue("@Day_of_the_Week", Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text)); 
     command.Parameters.AddWithValue("@Hours_Total", GridView1.Rows[i].Cells[2].Text); 
     command.ExecuteNonQuery(); 
     command.Parameters.Clear(); 
    } 
+0

、私たちに 'GridView1.Rowsの価値を伝える[I ] .Cells [1] .Text'を実行し、GridView1.Rows [i] .Cells [1] .Text'がDateTimeでない場合に何をしたいか教えてください。 –

+0

有効でない形式で日付を解析しようとしているように見えますが、 '' 'GridView1.Rows [i] .Cells [1] .Text'''には何が表示されますか? – nramirez

+0

パラメータを設定する前にチェックを追加してください。 @Day_of_the_weekパラメータで埋められたフィールドのデータ型は何ですか? – Steve

答えて

1

あなたが入力フィールドを変換しようと変換パラメータを設定する必要があります。変換が失敗した場合(データベースがそのフィールドのヌル受け入れることができる場合)あなたのフィールドのために事前に定義された値を使用するか、またはDBNull.Valueをに設定しshouls

DateTime minValue = new DateTime(1900,1,1); // Arbitrary for missing values 
command.Parameters.Add("@Name", SqlDbType.NVarChar); 
command.Parameters.Add("@Day_of_the_Week", SqlDbType.DateTime); 
command.Parameters.AddWithValue("@Hours_Total", SqlDbType.NVarChar); 
for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
{ 
    DateTime day; 
    if(!DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out day) 
     day = minValue; 

    command.Parameters["@Name"].Value = GridView1.Rows[i].Cells[0].Text; 
    command.Parameters["@Day_of_the_Week"].Value = day; 
    command.Parameters["@Hours_Total"].Value = GridView1.Rows[i].Cells[2].Text; 
    command.ExecuteNonQuery(); 
} 

お知らせも私はAddWithValueを使用しないこと。便利な方法ですが、認識するためにsome serious problemsがあります。 Addメソッドを使用すると、データ型を正確に指定できます。このデータ型は、データベースの型と一致する必要があります。
最後に、すべてのパラメータをループ外で宣言し、ループ内の値を設定するだけです(ループごとに値を再設定する必要はありません)。

+0

Thanks、Steve。魅力のように動作します –

0

ので、それはそれ以外の場合はExcelからの生の情報を返す変換した場合、あなたが取得しようとしているセルの内容は、有効な日時である場合には、このほとんどチェックまたは....

DateTime outDate; 
    for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
    { 

     command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text); 
     command.Parameters.AddWithValue("@Day_of_the_Week", DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out outDate) ? Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text) : GridView1.Rows[i].Cells[1].Text); 
     command.Parameters.AddWithValue("@Hours_Total", GridView1.Rows[i].Cells[2].Text); 
     command.ExecuteNonQuery(); 
     command.Parameters.Clear(); 
    } 

...あなたの@Day_of_the_Weekのparamは日時型の場合は日時分の日付を返します。

DateTime outDate; 
    for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
    { 

     command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text); 
     command.Parameters.AddWithValue("@Day_of_the_Week", DateTime.TryParse(GridView1.Rows[i].Cells[1].Text, out outDate) ? Convert.ToDateTime(GridView1.Rows[i].Cells[1].Text) : DateTime.MinValue); 
     command.Parameters.AddWithValue("@Hours_Total", GridView1.Rows[i].Cells[2].Text); 
     command.ExecuteNonQuery(); 
     command.Parameters.Clear(); 
    } 
+0

ありがとうございます。私は今このエラーが発生しています。 \t \t "'System.DateTime'と 'string'の間に暗黙的な変換がないため、条件式の型を判別できません" –

+0

最初の例は、3項演算子の2つの部分が同じ型を返すためコンパイルされません。とにかく、演算子の真の部分でout変数を使うことができます。最後にAddWithValueを使用しないでください(特に日付を使用) – Steve

0

1つのヘルパー関数(NULLになることがわかっている場合はNULLと言ってください)をチェックしてから、何をデフォルトとして与えるかを決めます。私は通常、「通常のプリミティブ型の型」(CLRを意味する)とSQL型の型を作成します。 null可能なバリアントを安全に扱うこともできます。

あなたの場合、UIを行っているので、3番目の種類 - 表示文字列のデフォルトが必要な場合があります。何かDateTimeが見つからない場合、適切なデフォルトは単に空文字列か「不定」のような短い単語かもしれません。その側面は、特定のUIの目的に非常に依存します。

0

"Day_of_the_Week"の中に入れようとしているデータの種類が何であるかはわかりません。曜日、数値インデックス、完全な日付ですか。

しかし、ちょうどフル日時場合は、「YYYYMMDD」のような文字列の構文を使用してSQLでそれを追加することができます:あなたのループにブレークポイントを設定し

DateTime outDate; 
IFormatProvider culture = new CultureInfo("en-US", true) 

    for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
    { 

     command.Parameters.AddWithValue("@Name", GridView1.Rows[i].Cells[0].Text); 
     command.Parameters.AddWithValue("@Day_of_the_Week", Datetime.ParseExact(GridView1.Rows[i].Cells[1].Text, "yyyy-MM-dd", culture); 
     command.Parameters.AddWithValue("@Hours_Total", GridView1.Rows[i].Cells[2].Text); 
     command.ExecuteNonQuery(); 
     command.Parameters.Clear(); 
    } 
+0

そのタイムシートの計算、曜日はfulldateです。応答ありがとう –

関連する問題