简体   繁体   中英

Error converting string to DateTime

private void SaveButton_Click(object sender, EventArgs e)
{
   try
   {
       if (ValidateInput())
       {
          if (md.NAME_ID("LOGIN", "UID", "UNAME", UserNameTextBox.Text) != "")
          {
              MessageBox.Show("User already exist");
              return;
          }
          string PASS = "";
          if (UpassTextBox.Text != "")
          {
              PASS = base64Encode(UpassTextBox.Text);
          }
          else
          {
             PASS = "";
          }
          LoginTbl login = new LoginTbl(Convert.ToString(md.Max_ID("UID", "LOGIN")),
                               Convert.ToDateTime(DT1.Value.ToShortDateString()), 
                               Convert.ToBoolean(ActiveRadioButton.Checked ? true : false), 
                               UserNameTextBox.Text, PASS, MobileTextBox.Text, 
                               RemarksTextBox.Text, UpdateCheckBox.Checked, 
                               DeleteCheckBox.Checked, MenuCheckBox.Checked, 
                               AddItemCheckBox.Checked, SearchItemCheckBox.Checked, 
                               ListViewCheckBox.Checked, PurchaseCheckBox.Checked, 
                               PurReturnCheckBox.Checked, PurPerformaCheckBox.Checked, 
                               PurImportsCheckBox.Checked, PurReportsCheckBox.Checked, 
                               SaleCheckBox.Checked, SaleReturnCheckBox.Checked, 
                               SalePerformaCheckBox.Checked, SaleReportsCheckBox.Checked, 
                               InventoryCheckBox.Checked, PaymentCheckBox.Checked, 
                               JVCheckBox.Checked, AccLedgerCheckBox.Checked, 
                               TrailCheckBox.Checked, PLCheckBox.Checked, 
                               AccReportsCheckBox.Checked, SmsCheckBox.Checked, 
                               EmailCheckBox.Checked, SmsReportsCheckBox.Checked, 
                               StockNavigationCheckBox.Checked);

          if (new LoginDAL().Save(login))
          {
             MessageBox.Show("User has been added successfully");
             REFRESH_FORM();
             T1.Text = Convert.ToString(md.Max_ID("UID", "LOGIN"));
          }
          else
          {
             MessageBox.Show("Not added try again");
          }

      }
      else {
             MessageBox.Show("Please enter into \'*\' empty fields");

           }
   }
   catch (Exception ex)
   {
      MessageBox.Show("Error: " + ex.Source + ": " + ex.Message, "Connection Error !!");
   }
}

when i try to save then it will give an error Conversion failed when converting date/time from character string.

Here

Convert.ToDateTime(DT1.Value.ToShortDateString())

you convert your DateTime to a string and then back to a date. This is not necessary. Remove both conversions and use DT1.Value directly.

Why do you convert the DateTime to string at all just to parse it back to DateTime ? If you want to remove the time component from the date use DateTime.Date :

So instead of:

Convert.ToDateTime(DT1.Value.ToShortDateString())

this

DT1.Value.Date

要将基于字符串的日期转换为System.DateTime对象,可以使用Convert.ToDateTime(String)方法或DateTime.Parse(String)静态方法。

Convert.ToDateTime(DT1.Value.ToShortDateString())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM