PosterDirectory returns null

本文关键字:null returns PosterDirectory | 更新日期: 2023-09-27 18:21:07

我在这里声明了变量PosterDirectory

 SqlConnection con = new SqlConnection(Helper.GetConnectionString());
String PosterDirectory;
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Role"].ToString() != "Admin")
    {
        Session.Clear();
        Response.Redirect("Default.aspx");
    }

    if (!IsPostBack)
    {
        GetEvent(int.Parse(Request.QueryString["EventID"].ToString()));
    }

}  

假定PosterDirectory将在GetEvent函数中给定一个值。

void GetEvent(int EventID)
{
    con.Open();
    SqlCommand com = new SqlCommand(
        "select * from event where EventID = @EventID", con);
    com.Parameters.Add("@EventID", SqlDbType.Int).Value = EventID;
    SqlDataReader reader = com.ExecuteReader();
    while (reader.Read())
    {
        //Fill in the input fields based on the EventID
        txtName.Text = reader["Name"].ToString();
        txtVenue.Text = reader["Venue"].ToString();
        txtStreet.Text = reader["Street"].ToString();
        txtCity.Text = reader["City"].ToString();
        txtCountry.Text = reader["Country"].ToString();
        txtMap.Text = reader["Map"].ToString();
        txtStartDate.Text = Convert.ToDateTime(reader["DateTimeStarted"].ToString()).ToString("yyyy-MM-dd");
        txtStartTime.Text = Convert.ToDateTime(reader["DateTimeStarted"].ToString()).TimeOfDay.ToString();
        txtEndDate.Text = Convert.ToDateTime(reader["DateTimeEnded"].ToString()).ToString("yyyy-MM-dd");
        txtEndTime.Text = Convert.ToDateTime(reader["DateTimeEnded"].ToString()).TimeOfDay.ToString();
        PosterDirectory = reader["Poster"].ToString();
        txtDesciption.Text = reader["Description"].ToString();
    }
    con.Close();
}

稍后,我将使用字符串PosterDirectory作为SQL参数来检索照片的文件路径。

 protected void btnUpdate_Click(object sender, EventArgs e)
{
    //Get the Date and Time Strings
    DateTime DateStarted = Convert.ToDateTime(txtStartDate.Text);
    TimeSpan TimeStarted = TimeSpan.Parse(txtStartTime.Text);
    DateTime DateEnded = Convert.ToDateTime(txtEndDate.Text);
    TimeSpan TimeEnded = TimeSpan.Parse(txtEndTime.Text);
    DateTime DateTimeStarted = DateStarted + TimeStarted;
    DateTime DateTimeEnded = DateEnded + TimeEnded;
    //Get the DateTimeCreated
    DateTime DateTimeLastUpdated;
    con.Open();
    //if (fuPoster.HasFile)
    {
        SqlCommand com = new SqlCommand(
            "update event set Name=@Name, Venue=@Venue, Street=@Street, City=@City, Country=@Country, Map=@Map, DateTimeStarted=@DateTimeStarted, DateTimeEnded=@DateTimeEnded, DateTimeUpdated=@DateTimeUpdated, Poster=@Poster, Description=@Description where EventID=@EventID",
            con);
        com.Parameters.Add("@EventID", SqlDbType.Int).Value = int.Parse(Request.QueryString["EventID"].ToString());
        com.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
        com.Parameters.Add("@Venue", SqlDbType.NVarChar).Value = txtVenue.Text;
        com.Parameters.Add("@Street", SqlDbType.NVarChar).Value = txtStreet.Text;
        com.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity.Text;
        com.Parameters.Add("@Country", SqlDbType.NVarChar).Value = txtCountry.Text;
        com.Parameters.Add("@Map", SqlDbType.NVarChar).Value = txtMap.Text;
        com.Parameters.Add("@DateTimeStarted", SqlDbType.DateTime).Value = DateTimeStarted;
        com.Parameters.Add("@DateTimeEnded", SqlDbType.DateTime).Value = DateTimeEnded;
        com.Parameters.Add("@DateTimeUpdated", SqlDbType.DateTime).Value = DateTime.Now;
        DateTimeLastUpdated = DateTime.Now;
        if (fuPoster.HasFile)
        {
            com.Parameters.Add("@Poster", SqlDbType.Text).Value = "images/" + fuPoster.FileName;
            fuPoster.SaveAs(Server.MapPath("images/" + fuPoster.FileName));
        }
        else
        {
            com.Parameters.Add("@Poster", SqlDbType.Text).Value = PosterDirectory;
        }
        com.Parameters.Add("@Description", SqlDbType.Text).Value = txtDesciption.Text;
        com.ExecuteNonQuery();
        con.Close();
        //Auditing Event
        AuditEvent(DateTimeLastUpdated);
        Response.Redirect("Event.aspx");
    }
    //else
    //{
    //    SqlCommand com = new SqlCommand(
    //        "update event set Name=@Name, Venue=@Venue, Street=@Street, City=@City, Country=@Country, Map=@Map, DateTimeStarted=@DateTimeStarted, DateTimeEnded=@DateTimeEnded, DateTimeUpdated=@DateTimeUpdated, Description=@Description where EventID=@EventID",
    //        con);
    //    com.Parameters.Add("@EventID", SqlDbType.Int).Value = int.Parse(Request.QueryString["EventID"].ToString());
    //    com.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
    //    com.Parameters.Add("@Venue", SqlDbType.NVarChar).Value = txtVenue.Text;
    //    com.Parameters.Add("@Street", SqlDbType.NVarChar).Value = txtStreet.Text;
    //    com.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity.Text;
    //    com.Parameters.Add("@Country", SqlDbType.NVarChar).Value = txtCountry.Text;
    //    com.Parameters.Add("@Map", SqlDbType.NVarChar).Value = txtMap.Text;
    //    com.Parameters.Add("@DateTimeStarted", SqlDbType.DateTime).Value = DateTimeStarted;
    //    com.Parameters.Add("@DateTimeEnded", SqlDbType.DateTime).Value = DateTimeEnded;
    //    com.Parameters.Add("@DateTimeUpdated", SqlDbType.DateTime).Value = DateTime.Now;
    //    com.Parameters.Add("@Description", SqlDbType.Text).Value = txtDesciption.Text;
    //    DateTimeLastUpdated = DateTime.Now;
    //    com.ExecuteNonQuery();
    //    con.Close();
    //    //Auditing Event
    //    AuditEvent(DateTimeLastUpdated);
    //    Response.Redirect("Event.aspx");
    //}
}

PosterDirectory returns null

成员不会在请求之间持久化。Form类仅对单个请求有效。

有关上下文,请参阅ASP.NET页面生命周期。

您可以将您的值放入隐藏的表单字段或视图状态中。