C#如果那么其他问题

时间:2011-11-10 12:34:14

标签: c# .net if-statement

我收到错误无效的表达式术语else。括号之前的括号错误

  

“只有赋值,调用,递增,递减和新对象表达式才能用作语句”。

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");
        }
    }
}

8 个答案:

答案 0 :(得分:6)

你的语法错了,你有一个问题,没有尝试,你可以这样做。虽然你为什么要捕捉异常?是因为你认为日期不会出现吗?如果是这样,请检查if中的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");
     }

答案 1 :(得分:3)

你的大括号和try {} catch {}块不匹配。以下是更正后的代码:

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!";
        }
    }
}

答案 2 :(得分: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
            {
                //?
            }
        }

答案 3 :(得分:2)

else块之后的最后一个catch块没有相应的if块。

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

答案 4 :(得分:1)

您没有与try匹配的catch声明 - 这只是您问题中的拼写错误吗?

答案 5 :(得分:1)

你需要在try / catch中包含其他内容,请发布尝试以便下次更容易

答案 6 :(得分:1)

这些行不正确

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

删除这些行或添加try构造。

答案 7 :(得分:1)

移动第二个括号,否则为关键词

相关问题