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 !!");
}
}
当我尝试保存时,它会给出错误转换从字符串转换日期/时间时失败。
答案 0 :(得分:3)
下面
Convert.ToDateTime(DT1.Value.ToShortDateString())
将DateTime转换为字符串,然后再转换回日期。这不是必需的。删除这两次转化并直接使用DT1.Value
。
答案 1 :(得分:1)
为什么要将DateTime
转换为string
只是为了将其解析回DateTime
?如果要从日期中删除时间组件,请使用DateTime.Date
:
所以而不是:
Convert.ToDateTime(DT1.Value.ToShortDateString())
此
DT1.Value.Date
答案 2 :(得分:0)
要将基于字符串的日期转换为System.DateTime对象,您可以使用Convert.ToDateTime(String)
方法或DateTime.Parse(String)
静态方法
Convert.ToDateTime(DT1.Value.ToShortDateString())