2017-01-29 8 views
-2

私は以下のコードに残った日数を表示しようとしています。データベース..のような日常の残り日数べき、あなたが何かを得れば、あなたがデータベースから得るものがあり、あなたの文字列の形式と間違って何かが「最初」であり、「第二」のデバッグ私はメッセージボックスに以下のコードを表示します。エラーはありません。ただし、メッセージボックスを表示していません。

//i want to get the two dates which is already saved in sql and then 
//calculating the remaining days 
//reading the data 
using (SqlDataReader read = cmd.ExecuteReader()) { 
    //loop throught the reader 
    while (read.Read()) { 
     // putting the salarydate value from database to first variable 
     String first =(read["salarydate"].ToString()); 
     // putting the expirydate value from database to second variable 
     String Second = (read["expiredate"].ToString()); 

     //converting the first fron string to datetime and then saving it to another 
     //datetime variable name salaryDate 
     DateTime salaryDate = DateTime.ParseExact(first, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); 

     //converting the second fron string to datetime and then saving it to another 
     //datetime variable name expireDate 
     DateTime expireDate = DateTime.ParseExact(Second, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); 
     //timepaning between two 
     TimeSpan t1 = expireDate - salaryDate; 

     //storing the days from t1 to daysleft which means days remaining 
     int daysleft = t1.Days; //days left 

     //this message box is not showing 
     //i also want to save the difference in sql and when that difference become 
     //zero , i want to put condition on that .. 
     MessageBox.Show(daysleft.ToString(),"Days Left"); 
    } 
} 
+0

コードにはどのような問題がありますか? – FeliceM

+0

ここにあります: エラー: 文字列が有効ではありませんDateTime。 –

+0

文字列はどのように見えますか?それをデバッグします。 – LarsTech

答えて

1

を変更しているように見えます正しい書式を整えます。

ほとんどの場合、DBから "dd/MM/yyyy"という文字列が得られ、 "MM/dd/yyyy"でその文字列をフォーマットしようとするよりも、言語設定によって異なります。

私の答えをテストするために、私はDBからの出力を型付きの文字列に置き換え、あなたのコードはエラーなしで動作しています。

関連する問題