C# 无法访问循环外的datetime对象

C# 无法访问循环外的datetime对象,c#,datetime,C#,Datetime,做这个 public void ExpoFunc() { string dateInString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); DateTime startDate = DateTime.Parse(dateInString); if (ddlplan.SelectedItem.Text.Substring(0, 6) == "Weekly") { DateTime expiryD

做这个

public void ExpoFunc()
{
    string dateInString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    DateTime startDate = DateTime.Parse(dateInString);

    if (ddlplan.SelectedItem.Text.Substring(0, 6) == "Weekly")
    {
        DateTime expiryDate = startDate.AddDays(7);
        return;
    }
    else if (ddlplan.SelectedItem.Text.Substring(0, 7) == "1 Month")
    {
        DateTime expiryDate = startDate.AddMonths(1);
        return;
    }
    if (ddlplan.SelectedItem.Text.Substring(0, 9) == "12 Months")
    {
        DateTime expiryDate = startDate.AddMonths(12);
        return;
    }
    con.Open();
    string strupdate = "Update Users set t_effectiveto='" + Convert.ToDateTime(expiryDate) + "' where c_planduration='" + ddlplan.SelectedItem.Text + "' and c_mail='" + txtmail.Text + "'";
    //expiryDate confilct with declaration 
    SqlCommand cmd1 = new SqlCommand(strupdate,con);
    cmd1.ExecuteNonQuery();
    con.Close();       
}

声明
expiryDate
ouside if conditions感谢并同意Winney的建议。。现在我的代码正在运行
public string ExpoFunc()
{
    string dateInString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    DateTime startDate = DateTime.Parse(dateInString);

   //******Declare outside the loope
    DateTime expiryDate = new DateTime();

    if (ddlplan.SelectedItem.Text.Substring(0, 6) == "Weekly")
   {
        expiryDate = startDate.AddDays(7);
    }
    else if (ddlplan.SelectedItem.Text.Substring(0, 7) == "1 Month")
    {
        expiryDate = startDate.AddMonths(1);
    }
    if (ddlplan.SelectedItem.Text.Substring(0, 9) == "12 Months")
    {
       expiryDate = startDate.AddMonths(12);
    }
    con.Open();
    string strupdate = "Update Users set t_effectiveto='" +     Convert.ToDateTime(expiryDate) + "' where c_planduration='" + ddlplan.SelectedItem.Text + "' and c_mail='" + txtmail.Text + "'";
    //expiryDate confilct with declaration 
    SqlCommand cmd1 = new SqlCommand(strupdate,con);
    cmd1.ExecuteNonQuery();
    con.Close();     

    //this will return the date as well... just in case  
    return strupdate;
}