简体   繁体   中英

Conversion failed when converting date and/or time from character string. System.Data.SqlClient.SqlException:

Every time I hit logout button I get this error System.Data.SqlClient.SqlException: Conversion failed when converting date and/or time from character string.

This my Coding Part. Help me.

  SqlConnection conlog = new SqlConnection(ConfigurationManager.ConnectionStrings["loginConnectionString"].ConnectionString);
        conlog.Open();
    Session["New"] = null;

    string logintime = "select LoginTime from Logindata where UserName ='" + Label1.Text + "'";
    SqlCommand logintimequery = new SqlCommand(logintime, conlog);
    int logintimestr = logintimequery.ExecuteNonQuery();


        string idQuery = "Select ID from [Table] where Username='"+Label1.Text+"'";
    SqlCommand idd = new SqlCommand(idQuery, conlog);
    string strQuery = "update logindata set LogoutTime='" + DateTime.Now + "' where LoginTime='"+logintimestr+ "' and UserName='" + Label1.Text + "'  ";
    SqlCommand cmd = new SqlCommand(strQuery, conlog);

    cmd.Parameters.AddWithValue("@uname", Label1.Text);
    cmd.Parameters.AddWithValue("@logouttime", DateTime.Now.ToString());
    cmd.ExecuteNonQuery();
    Response.Redirect("Loginform.aspx");
    conlog.Close();

The only thing that I can find that will generate that exception is this code line.

string strQuery = "update logindata set LogoutTime='" + DateTime.Now 

Change it to.

string strQuery = "update logindata set LogoutTime='" + DateTime.Now.ToString() 

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