0
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.

3
  • It's going to be something in the LoginTbl but it's impossible to tell without more information. Can you look at everything that's doing a Convert.ToDateTime( ) (DT1.Value for example) and see what the value is? Commented Jan 7, 2014 at 9:08
  • What is DT1 what type it is and when you get issue what exactly value it hold. Most probably the problem is looking into that area as it might hold wrong value which can't be converted into date time. Commented Jan 7, 2014 at 9:10
  • @PouyaSamie: i assume it's a DateTimePicker. Commented Jan 7, 2014 at 9:22

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.