2011-11-10 58 views
-7

我收到错误Invalid expression term else。另外还有一个支架之前的支架出现错误,说C#if then else issues

“只能将分配,调用,增量,减量和新的对象表达式用作语句”。

public partial class frmPersonnel : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      DateTime dt1; 
      DateTime dt2; 

     if (txtFirstName.Text == "") 
     { 
      txtFirstName.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Please enter first name"; 
     } 
     if (txtLastName.Text == "") 
     { 
      txtLastName.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Please enter last name!"; 
     } 
     if (txtPayRate.Text == "") 
     { 
      txtPayRate.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Please enter pay rate!"; 
     } 
     if (txtStartDate.Text == "") 
     { 
      txtStartDate.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Please enter start date!"; 
     } 
     if (txtEndDate.Text == "") 
     { 
      txtEndDate.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Please enter end date!"; 
     } 
     dt1 = DateTime.Parse(txtStartDate.Text); 
     dt2 = DateTime.Parse(txtEndDate.Text); 


     if (DateTime.Compare(dt1, dt2) > 0) 
     { 
      txtStartDate.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Start Date must not be greater than End Date."; 
     } 
     } 
     catch (Exception ex) 
     { 
      lblError.Text = "Please enter valid data!"; 
     } 

     else 
     { 
      Session["txtFirstName"] = txtFirstName.Text; 
      Session["txtLastName"] = txtLastName.Text; 
      Session["txtPayRate"] = txtPayRate.Text; 
      Session["txtStartDate"] = txtStartDate.Text; 
      Session["txtEndDate"] = txtEndDate.Text; 
      Server.Transfer("frmPersonalVerified.aspx"); 
     } 
    } 
} 
+6

我看到太多的右括号。这似乎不是完整的代码 – Jan

+0

你错过了一些重要的代码。你的“尝试”在哪里? –

+0

你只是让你的“{”和“}”不匹配。就这样。 “if”对应于“else”的位置 - 因为它们不能跨越“try ... catch”块 –

回答

3

你的括号,并尝试{}赶上{}块是不匹配的。这里是校正代码:

public partial class frmPersonnel : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      DateTime dt1; 
      DateTime dt2; 

      if (txtFirstName.Text == "") 
      { 
       txtFirstName.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Please enter first name"; 
      } 
      if (txtLastName.Text == "") 
      { 
       txtLastName.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Please enter last name!"; 
      } 
      if (txtPayRate.Text == "") 
      { 
       txtPayRate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Please enter pay rate!"; 
      } 
      if (txtStartDate.Text == "") 
      { 
       txtStartDate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Please enter start date!"; 
      } 
      if (txtEndDate.Text == "") 
      { 
       txtEndDate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Please enter end date!"; 
      } 
      dt1 = DateTime.Parse(txtStartDate.Text); 
      dt2 = DateTime.Parse(txtEndDate.Text); 


      if (DateTime.Compare(dt1, dt2) > 0) 
      { 
       txtStartDate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text = "Start Date must not be greater than End Date."; 
      } 

      else 
      { 
       Session["txtFirstName"] = txtFirstName.Text; 
       Session["txtLastName"] = txtLastName.Text; 
       Session["txtPayRate"] = txtPayRate.Text; 
       Session["txtStartDate"] = txtStartDate.Text; 
       Session["txtEndDate"] = txtEndDate.Text; 
       Server.Transfer("frmPersonalVerified.aspx"); 
      } 
     } 
     catch (Exception ex) 
     { 
      lblError.Text = "Please enter valid data!"; 
     } 
    } 
} 
1

你没有try声明符合catch - 是,仅仅在你的问题一个错字?

1

你需要有其他的try/catch语句中,请张贴的尝试很容易在下一次

1

这些线的地方不正确

}  
catch (Exception ex) 
{ 
     lblError.Text = "Please enter valid data!";  
} 

eithe删除这些线或添加try结构,使。

1

移动第二支架其他关键字

2

前试试这个:

protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      DateTime dt1; 
      DateTime dt2; 
      lblError.Text = ""; 

      //Increment lblError.Text to see all errors 

      if (txtFirstName.Text == "") 
      { 
       txtFirstName.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text += "Please enter first name"; 
      } 
      if (txtLastName.Text == "") 
      { 
       txtLastName.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text += "Please enter last name!"; 
      } 
      if (txtPayRate.Text == "") 
      { 
       txtPayRate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text += "Please enter pay rate!"; 
      } 
      if (txtStartDate.Text == "") 
      { 
       txtStartDate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text += "Please enter start date!"; 
      } 
      if (txtEndDate.Text == "") 
      { 
       txtEndDate.BackColor = System.Drawing.Color.Yellow; 
       lblError.Text += "Please enter end date!"; 
      } 

      try 
      { 
       //This is only to cacth parse erros from string to datetime 
       dt1 = DateTime.Parse(txtStartDate.Text); 
       dt2 = DateTime.Parse(txtEndDate.Text); 

       if (DateTime.Compare(dt1, dt2) > 0) 
       { 
        txtStartDate.BackColor = System.Drawing.Color.Yellow; 
        lblError.Text = "Start Date must not be greater than End Date."; 
       } 
      } 
      catch (Exception) 
      { 
       lblError.Text = "Please enter valid data!"; 
      } 


      //Do wathever you want next 
      //If this is only to get called with no errors you could validate if lblError.Text is empty 
      if (string.IsNullOrEmpty(lblError.Text)) 
      { 
       Session["txtFirstName"] = txtFirstName.Text; 
       Session["txtLastName"] = txtLastName.Text; 
       Session["txtPayRate"] = txtPayRate.Text; 
       Session["txtStartDate"] = txtStartDate.Text; 
       Session["txtEndDate"] = txtEndDate.Text; 
       Server.Transfer("frmPersonalVerified.aspx"); 
      } 
      else 
      { 
       //? 
      } 
     } 
+0

但是'try'模块中的代码不太可能引发异常,尤其是由于数据无效。 – Justin

+0

@DivHenr:除非在代码文件的其他部分中还有其他一些不匹配的“{”和“}”,否则这个块本身没有任何问题。检查上面或下面的其他功能。还有类和名称空间的开合花括号 –

+0

可接受的解决方案可以工作,但代码中没有逻辑。首先,你只能看到最后一个错误,你总是覆盖lblError.Text,仅当日期确定后才会重定向。非常奇怪的代码! – hjgraca

6

你的语法是错误的,你有一个渔获物和没有尝试,你可以做这种方式来代替。为什么你抓住一个例外?是因为你认为日期不会在那里?如果是这样,请检查if中是否等待在比较中捕获异常。例外情况只应用在例外的情况下。要解决语法在您的文章,请执行下列操作(注意,终于出现异常是否被抛出)

 try 
    { 
     if (DateTime.Compare(dt1, dt2) > 0) 
     { 
      txtStartDate.BackColor = System.Drawing.Color.Yellow; 
      lblError.Text = "Start Date must not be greater than End Date."; 
     } 
     else 
     { 
      lblError.Text = "Please enter valid data!"; 
     } 
    } 
    catch (Exception ex) 
    { 
     lblError.Text = "Please enter valid data!"; 
    } 
    finally 
    { 
     Session["txtFirstName"] = txtFirstName.Text;  
     Session["txtLastName"] = txtLastName.Text;  
     Session["txtPayRate"] = txtPayRate.Text;  
     Session["txtStartDate"] = txtStartDate.Text;  
     Session["txtEndDate"] = txtEndDate.Text;  
     Server.Transfer("frmPersonalVerified.aspx"); 
    } 
2

catch的块之后的最后else块没有对应if块。

很难猜测else块中的代码是否打算执行。给我一个提示,我可能会告诉你正确的地方:)