user2873347
user2873347

Reputation: 21

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.

Upvotes: 0

Views: 259

Answers (3)

Nagaraj S
Nagaraj S

Reputation: 13484

To convert a string-based date to a System.DateTime object, you can use the Convert.ToDateTime(String) method or the DateTime.Parse(String) static method

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

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

Upvotes: 1

Heinzi
Heinzi

Reputation: 172260

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.

Upvotes: 3

Related Questions