2016-12-30 97 views
0

我在asp.net具有DATETIME數據類型的兩列,當我連接到MySQL數據庫中保存它創建一個gridview,它顯示了我一個錯誤「不正確的日期時間值:01/01/2017)00:07:26'在列1中的'LogInDate_Time'列'in cmd.ExecuteNonQuery();行保存DATETIME數據類型到MySQL

如何解決?

C#

protected void Page_Load(object sender, EventArgs e) 
{ 
} 
protected void LoggedIn(object sender, EventArgs e) 
{ 
    CheckBox checkedCheckBox = (sender as CheckBox); 
    GridViewRow checkedRow = (checkedCheckBox.NamingContainer as GridViewRow); 
    Label loggedInDateTime = checkedRow.FindControl("lblLoggedInDateTime") as Label; 
    if (checkedCheckBox.Checked) 
    { 
     loggedInDateTime.Text = DateTime.Now.ToString(); 
    } 
    else 
    { 
     loggedInDateTime.Text = ""; 
    } 
} 

protected void LoggedOut(object sender, EventArgs e) 
{ 
    CheckBox checkedCheckBox = (sender as CheckBox); 
    GridViewRow checkedRow = (checkedCheckBox.NamingContainer as GridViewRow); 
    Label loggedOutDateTime = checkedRow.FindControl("lblLoggedOutDateTime") as Label; 
    if (checkedCheckBox.Checked) 
    { 
     loggedOutDateTime.Text = DateTime.Now.ToString(); 
    } 
    else 
    { 
     loggedOutDateTime.Text = ""; 
    } 
} 

protected void btnSave_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     string Attendance_ID = (row.FindControl("lblAttendanceID") as Label).Text; 
     string Attendance_Name = (row.FindControl("lblAttendanceName") as Label).Text; 
     string LogInDate_Time = (row.FindControl("lblLoggedInDateTime") as Label).Text; 
     string LogOutDate_Time = (row.FindControl("lblLoggedOutDateTime") as Label).Text; 
     InsertData(Attendance_ID, Attendance_Name, LogInDate_Time, LogOutDate_Time); 
    } 
    lblMessage.Text = "All Records Saved Successfully!!"; 
} 

public void InsertData(string Attendance_ID, string Attendance_Name, string LogInDate_Time, string LogOutDate_Time) 
{ 

    //Your saving code. 
    string A = "server=localhost; userid=; password=; database=admindb; allowuservariables=True; Convert Zero Datetime=True; Allow Zero Datetime=True "; 
    using (MySqlConnection connection = new MySqlConnection(A)) 
    { 
     string UpdateQuery = " INSERT INTO Attendance_Table (Attendance_ID, Attendance_Name,LogInDate_Time, LogOutDate_Time)" + " VALUES (@Attendance_ID,@Attendance_Name,@LogInDate_Time,@LogOutDate_Time)"; 
     MySqlCommand cmd = new MySqlCommand(UpdateQuery, connection); 
     MySqlParameter paramAttendance_ID = new MySqlParameter("@Attendance_ID", Attendance_ID); 
     cmd.Parameters.Add(paramAttendance_ID); 
     MySqlParameter paramAttendance_Name = new MySqlParameter("@Attendance_Name", Attendance_Name); 
     cmd.Parameters.Add(paramAttendance_Name); 
     MySqlParameter paramLogInDate_Time = new MySqlParameter("@LogInDate_Time", LogInDate_Time); 
     cmd.Parameters.Add(paramLogInDate_Time); 
     MySqlParameter paramLogOutDate_Time = new MySqlParameter("@LogOutDate_Time", LogOutDate_Time); 
     cmd.Parameters.Add(paramLogOutDate_Time); 
     cmd.Connection.Open(); 
     cmd.ExecuteNonQuery(); 
     cmd.Connection.Close(); 
    } 
} 




protected void lbInsert_Click(object sender, EventArgs e) 
{ 

    ObjectDataSource1.InsertParameters["Attendance_ID"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtID")).Text; 
    ObjectDataSource1.InsertParameters["Attendance_Name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtName")).Text; 
    ObjectDataSource1.InsertParameters["Attendance_Con"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("cbAttendanceCon")).Text; 
    ObjectDataSource1.InsertParameters["LogInDate_Time"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLogIn")).Text; 
    ObjectDataSource1.InsertParameters["Leaving_Con"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("cbLeavingCon")).Text; 
    ObjectDataSource1.InsertParameters["LogOutDate_Time"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLogOut")).Text; 
    ObjectDataSource1.Insert(); 



} 

}

+0

犯規聲音就像你存儲日期爲日期按'字符串LogInDate_Time'。字符串不是日期 – Plutonix

+0

但是當我試圖聲明它作爲日期時間不啓用! –

+0

如果您希望日期充當日期,則必須將日期保存爲日期。如果你不知道如何有數百個帖子,她解釋如何 – Plutonix

回答

1

MySQL的希望在YYYY-MM-DD格式的日期。

參考:

+0

如何在c#中聲明它? –

+0

使用您則DateTime.ToString()的格式:https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx – GamerJ5

+0

我覺得這個鏈接:https://msdn.microsoft .com/it-it/library/8kb3ddd4(v = vs.110).aspx可以幫助你 –