根据下面的代码,获得一些值(newrev,newreview)并放入变量后,我需要将它们放入标签(“某些文本” + newrev)中。但是我有问题(newrev)在当前上下文中不存在。

Label1.Text = "Review Number:" + newReview + "(for preparing of Rev." + newrev+")";

protected void ddlProjectDocument_SelectedIndexChanged(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();

    var x = ddlProjectDocument.SelectedValue;

    var MaxRev = (from rev in _DataContext.tblTransmittalls
                  where rev.DocID.ToString() == ddlProjectDocument.SelectedValue
                  select  rev.REV).Max();

    if (MaxRev == null)
    {
        var newRev = 0;

    }

    else
    {

        var newRev = Convert.ToInt32(MaxRev) + 1;

    }

    var MaxReview = (from rev in _DataContext.tblFiles
                  where (rev.DocId.ToString()==ddlProjectDocument.SelectedValue)&&
                        (rev.Rev.ToString()==MaxRev)
                  select rev.Review).Max();

    if (MaxReview == null)
    {
        var newReview = 1;
    }

    else
    {
        var newReview = Convert.ToInt32(MaxReview) + 1;
    }

    Label1.Text = "Review Number:" + newReview + "(for preparing of Rev." + newrev+")";
}

最佳答案

您需要在newRev语句之外声明if

int newrev;
if(MaxRev == null)
    newRev = 0;
else
    newRev = Convert.ToInt32(MaxRev) + 1;

07-27 13:34