Если другое условие не работает
у меня есть gridview в котором сотрудник intime и outtime exit и часы также вычисляются из разницы intime и outtime например:
INTIME OUTTIME HOURs 12/12/2018 08:00am 12/12/2018 08:00pm 12Теперь я хочу, когда я обновляю gridview, то если INTIME не доступен, то часы равны 0.
INTIME OUTTIME HOURs 12/12/2018 08:00pm 0
Что я уже пробовал:
protected void update_Click(object sender, EventArgs e) { System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US"); foreach (GridViewRow row in this.GVEAtt.Rows) { var AttdID = row.FindControl("AttdID") as Label; var Name = row.FindControl("Name") as Label; var txtCountry = row.FindControl("txtCountry") as TextBox; var date2 = (row.FindControl("txtINTime") as TextBox).Text; var date3 = (row.FindControl("txtOuttime") as TextBox).Text; var Hours = (row.FindControl("txtHours") as TextBox).Text; var Days = (row.FindControl("txtDays") as TextBox).Text; var OT = (row.FindControl("txtOT") as TextBox).Text; var OTRate = (row.FindControl("txtOTRate") as TextBox).Text; var Duty = (row.FindControl("txtDuty") as TextBox).Text; var OTAmount = (row.FindControl("txtOTAmount") as TextBox).Text; var Time = (row.FindControl("txtTime") as TextBox).Text; var LTime = (row.FindControl("txtLTime") as TextBox).Text; var Late = (row.FindControl("txtlate") as TextBox).Text; var Status = row.FindControl("Status") as Label; DateTime INTime; if (!string.IsNullOrEmpty(date2)) { INTime = DateTime.ParseExact(date2, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture); } else { INTime = System.DateTime.Now; } DateTime Outtime; if (!string.IsNullOrEmpty(date3)) { Outtime = DateTime.ParseExact(date3, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture); } else { Outtime = System.DateTime.Today; } var command = new SqlCommand(); command.CommandText = "UPDATE EmployeeAttedance SET INTime=@INTime,Duty=@Duty,Outtime=@Outtime,OT=@OT,Days=@Days,Hours=@Hours, OTAmount=@OTAmount ,OTRate=@OTRate,Time=@Time,Late=@Late,LTime=@LTime WHERE AttdID=@AttdID"; con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=AttendanceManagement"); command.Connection = con; Time = INTime.ToString("HH:mm:ss"); if (AttdID != null) command.Parameters.AddWithValue("@AttdID", AttdID.Text); if (Name != null) command.Parameters.AddWithValue("@Name", Name.Text); // if (txtCountry != null) command.Parameters.AddWithValue("@Country", txtCountry.Text); if (date2 != null) command.Parameters.AddWithValue("@INTime", date2); if (date3 != null) command.Parameters.AddWithValue("@Outtime", date3); if (Hours != null) command.Parameters.AddWithValue("@Hours", Hours); if (Days != null) command.Parameters.AddWithValue("@Days", Days); if (OT != null) command.Parameters.AddWithValue("@OT", OT); if (OTAmount != null) command.Parameters.AddWithValue("@OTAmount", OTAmount); if (OTRate != null) command.Parameters.AddWithValue("@OTRate", OTRate); if (Duty != null) command.Parameters.AddWithValue("@Duty", Duty); // if (txtCity != null) command.Parameters.AddWithValue("@City", txtCity.Text); // if (City != null) command.Parameters.AddWithValue("@City", City); if (Time != null) command.Parameters.AddWithValue("@Time", Time); if (LTime != null) command.Parameters.AddWithValue("@LTime", LTime); if (Late != null) command.Parameters.AddWithValue("@Late", Late); if ((date2 == "")) { command.Parameters["@INTime"].Value = DBNull.Value; } else { command.Parameters["@INTime"].Value = DateTime.Parse(date2); } if ((date3 == "")) { command.Parameters["@Outtime"].Value = DBNull.Value; } else { command.Parameters["@Outtime"].Value = DateTime.Parse(date3); } con.Open(); command.ExecuteNonQuery(); } } protected void TimeSpan(object sender, EventArgs e) { GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow; string txtINTime = (row.FindControl("txtINTime") as TextBox).Text; string txtOuttime = (row.FindControl("txtOuttime") as TextBox).Text; if (!string.IsNullOrEmpty(txtINTime) && !string.IsNullOrEmpty(txtOuttime)) { TimeSpan timeSpan = Convert.ToDateTime(txtOuttime) - Convert.ToDateTime(txtINTime); TextBox txtHours = (row.FindControl("txtHours") as TextBox); TextBox txtDays = (row.FindControl("txtDays") as TextBox); TextBox txtDuty = (row.FindControl("txtDuty") as TextBox); TextBox txtOT = (row.FindControl("txtOT") as TextBox); TextBox txtOTAmount = (row.FindControl("txtOTAmount") as TextBox); TextBox txtOTRate = (row.FindControl("txtOTRate") as TextBox); TextBox txtCity = (row.FindControl("txtCity") as TextBox); TextBox txtTime = (row.FindControl("txtTime") as TextBox); TextBox txtLTime = (row.FindControl("txtLTime") as TextBox); TextBox txtLate = (row.FindControl("txtLate") as TextBox); TextBox lblStatus = (row.FindControl("lblStatus") as TextBox); TextBox txtOTEntitled = (row.FindControl("txtOTEntitled") as TextBox); txtHours.Text = timeSpan.ToString(); txtHours.Text = timeSpan.TotalHours.ToString(); txtHours.Text = Math.Round(double.Parse(txtHours.Text), 0).ToString(); int t1; int t2; int t3; int t4; int t5; // int t6; txtDuty.Text = "8"; txtOTAmount.Text = "0"; txtOT.Text = "0"; // txtLate.Text = "1"; txtOTRate.Text = "100".ToString(); txtLTime.Text = "9:10".ToString(); t1 = int.Parse(txtHours.Text); t2 = int.Parse(txtDuty.Text); t3 = int.Parse(txtOTAmount.Text); t4 = int.Parse(txtOTRate.Text); t5 = int.Parse(txtOT.Text); // t6 = int.Parse(txtINTIME.Text); // if (t1 == 0) //{ // lblStatus.Text = "A"; // switch (lblStatus.Text) // { // case "A": // lblStatus.BackColor = Color.Red; // break; // } //if (t1 >= 8) //{ // lblStatus.Text = "P"; // switch (lblStatus.Text) // { // case "P": // lblStatus.BackColor = Color.Green; // break; // } if (txtOTEntitled.Text == "NO") { txtOTAmount.Text = "0"; txtOTRate.Text = "0"; } if (t1 > t2) { txtOT.Text = (t1 - t2).ToString(); } if (t5 < 1) { txtOTAmount.Text = (int.Parse(txtOT.Text) * int.Parse(txtOTRate.Text)).ToString(); } if (t2 <= t1) { txtDays.Text = "1"; } if (t1 <= 7) { txtDays.Text = ".5"; } if (txtDays.Text == ".5") { txtOT.Text = "0"; Response.Redirect(Request.RawUrl); con.Close(); } } } } }
F-ES Sitecore
В чем вопрос?
Member 12314309
как передать 0 в часах, если INTIME или OUTTIME не доступны в gridview или если один из них exit означает intime ?
Richard MacCutchan
Создание метода (TimeSpan
) с тем же именем, что и системный класс, - это не очень хорошая идея.
Member 12314309
так что же делать?
#realJSOP
Измените имя вашего метода с TimeSpan
к чему-то другому, например X
, или Y
.
Richard MacCutchan
Во-первых, измените имя этого метода, чтобы он не конфликтовал. Во-вторых, объясните, в чем именно заключается ваша проблема.
Member 12314309
моя проблема в том, что я использую
if (txttime.Text.length == 0 )
{ txthours.Текст = "0"
}
это не работает
Richard MacCutchan
Что содержит txttime на данный момент? А что значит "не работает" означает?
Member 12314309
когда txttime.text содержит 12-12-2018 08:00 когда txttime.Текст не имеет никакого значения,тогда txthours должен быть равен 0, но этот код не дает мне желаемого результата, когда это условие выполняется
Richard MacCutchan
Извините, но это не имеет смысла. Либо он содержит некоторый текст, и в этом случае длина будет больше нуля, либо он не содержит ничего, и в этом случае длина должна быть равна нулю. Однако лучшим тестом было бы использовать Строка.Метод IsNullOrEmpty(String) (System) | Microsoft Docs[^].