2013-07-30 2 views
-1

"if (period.Tostring() ="1 year ")"문제는 데이터베이스 nvarchar를 검사하고 변환해야하므로 루프를 수행하지만 빨간색으로 표시됩니다. 선.if 문에 빨간색 줄이있는 경우

string strCommandText4 = "SELECT autoLoanPeriod From AutoLoan WHERE userID= '" + Session["userID"] + "';"; 
    SqlCommand myCommand4 = new SqlCommand(strCommandText4, myConnection); 
    var period = myCommand.ExecuteScalar(); 


    if (period.ToString() = "1 year") 
    { 
     for (int i = 0; i<= 12; i++) 
     { 
      string strCommandText5 = "INSERT INTO AutoTrans VALUES(@loanID,@transPeriod,null,@transStatus);"; 

      SqlCommand myCommand5 = new SqlCommand(strCommandText5, myConnection); 
      myCommand5.Parameters.AddWithValue("@loanID", Session["@loanID"].ToString()); 
      myCommand5.Parameters.AddWithValue("@transPeriod", numPeriod); 
      myCommand5.Parameters.AddWithValue("@transStatus", status); 
     } 
    } 
+1

은''== 항등 연산자이다 =''할당 연산자이다. –

답변

4

If 문에 "=="을 사용해야합니다. 시도해보고 효과가 있는지 알려주세요.

if (period.ToString() == "1 year") 

또한 시도 할 수 있습니다 :

string.Equals (period.ToString(), "1 년");

+0

이미 그것을했지만 녹색 라인 "비교를 얻으려면 의도하지 않은 참조 비교, 문자열에 왼쪽 손잡이를 캐스팅"얻을. –

+0

문자열을 문자열로 변환하기 전에 "기간"유형은 무엇입니까? –

+0

그것은 .equals와 함께 작동합니다. –

1

adityaswami89가 말했듯이 평등성을 검사 할 때는 "=="를 사용해야합니다. 위의 코드에서 "="은 "period.ToString()"에 "1 year"값을 할당하려고 시도합니다. 이는 잘못된 연산입니다.

2

사용

if (period.ToString() == "1 year") 

또는

if (period.ToString().Equals("1 year")) 

비교하는 문자열

1

당신은이 같은 if 문에서 =을 가질 수는 pediod에 assign "일년"에 노력하고있다 , 할 수 없습니다. 귀하의 경우에 당신은

if (period.ToString() == "1 year") 

==이 연산자에 대한 MSDN 문서 여기를 참조하십시오 equality 연산자를 사용하려면;

http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx