2017-01-29 6 views
-2

im пытается показать оставшиеся дни в коде ниже, а также хочу показать как дни счета и сохранить его в База данных .. как каждый день оставшиеся дни должны, как представляется, изменитьЯ хочу показать левые дни в окне сообщений в нижнем коде ... нет ошибки .. но ее нет показ в ящике сообщений

//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

Существует что-то не так с форматом ваших строк «первый» и «второй» Debug, что вы получите из базы данных, если вы что-нибудь, и упорядочить правильное форматирование.

Скорее всего из базы данных вы получаете строку «dd/MM/yyyy», и вы пытаетесь отформатировать эту строку в «MM/dd/yyyy», но это зависит от настроек языка.

Чтобы проверить свой ответ, я заменил вывод из БД набранными строками, и ваш код работает без ошибок.