2012-11-11 4 views
0

if 문에 =,>, & &과 같은 연산자를 사용하고 싶습니다. 이 조건이 true 일 때만 전자 메일을 보내려고합니다 (strAuthReqDate == null & & strDate < strExpectedSubDate). 그래서 내 "미만의 운영자"잘못 pls 내가 여기에 잘못하고있는 일을 알려주라고 생각합니다.C#을 사용하는 If 문에서 둘 이상의 연산자를 사용하는 방법

foreach (GridViewRow gr in GridView1.Rows) 
{ 
    CheckBox cb = (CheckBox)gr.FindControl("chkItem"); 
    if (cb.Checked) 
    { 
     // string strID = gr.Cells[0].Text; 
     string strExpectedSubDate = gr.Cells[3].Text; 
     string strAuthReqDate = gr.Cells[8].Text; 
     string strDate = Convert.ToString(System.DateTime.Now); 
     if (strAuthReqDate == null && strDate < strExpectedSubDate) 
     { 
      send email(); 
     } 
    } 
} 
+0

괜찮아 보입니다. 문제 시체는 무엇입니까? – Rikki

+0

날짜 비교가 아닌 문자열 비교를 수행하고 있습니다. 'strExpectedSubDate' 형식은 어떤 형식입니까? –

+0

두 필드는 모두 날짜 필드입니다. 모하메드에게 몇 가지 예를 보여 주시겠습니까? – moe

답변

2

연산자보다 작은 시도를 사용하여 비교할 수 없습니다, 캐스팅하려고 대신 DateTime으로

문자열을 비교할 수 없으며 C#이 VB6이 아닌 것처럼 DateTimes처럼 처리됩니다. 따라서 먼저 Datetime으로 변환하십시오.

DateTime ExpectedSubDate; 
string strExpectedSubDate = gr.Cells[3].Text; 
if(DateTime.TryParse(strExpectedSubDate, out ExpectedSubDate)) 
{ 
    DateTime AuthReqDate; 
    string strAuthReqDate = gr.Cells[8].Text; 
    if(!DateTime.TryParse(strAuthReqDate, out AuthReqDate)) 
    { 
     if(DateTime.Now < ExpectedSubDate) 
     { 
      SendMail(); 
     } 
    } 
} 
+0

감사합니다 gabba,이게 내가 그것을 시도하자 작동한다고 생각합니다 – moe

+0

나는 오류가 발생하고 내 데이트에서 시간 부분을 없앨 필요가 있다고 생각합니다. 그것은 필드에 날짜와 시간을 모두 표시하므로 strExpectedSubDate에 날짜 만 표시되기 때문에 어떻게 날짜를 표시 할 수 있습니까? 감사합니다 – moe

+0

그리드에서 짧은 날짜 만 표시하도록 돕기 그리드에 레코드를 추가하는 코드를 볼 필요가 있습니다. 일반적으로 어딘가에 dateformat을 설정해야합니다. – gabba

1

귀하의 코드는 텍스트 날짜, 실제적인 날짜를 비교 : 여기

감사

내 코드입니다. 기대했던 것과 같이 작동하려면 DateTime 객체를 비교해야합니다!

+0

그래서 이렇게 변경해야합니까 : DateTime strExpectedSubDate = gr.Cells [3] .Text; DateTime strAuthReqDate = gr.Cells [8]. 텍스트; – moe

+0

당신은 텍스트를 변환해야합니다,이 솔루션을 참조하십시오 : http://stackoverflow.com/questions/7580809/parse-c-sharp-string-to-datetime – emartel

1

strDate와 strExpectedSubDate가 문자열이기 때문입니다. 당신이 String.IsNullOrWhiteSpce 또는 더 나은을 사용해야하므로 그래서 당신은, Cells[i].Text가 null 결코

DateTime strExpectedSubDate = DateTime.ParseExact(gr.Cells[3].Text, dateformat); 
DateTime strAuthReqDate = DateTime.ParseExact(gr.Cells[8].Text, dateformat); 
DateTime strDate = System.DateTime.Now; 
if (strAuthReqDate == null && strDate < strExpectedSubDate) 
{ 
} 
2

먼저 날짜를 비교하기 위해이 코드를 사용하는

관련 문제