“/”应用程序中的服务器错误。

出现问题:
图片说明

原代码:

 public partial class Details : System.Web.UI.Page
{
    string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["id"];
        //if (Session["UserID"] == null)
        //{
        //    Response.Write("<script language='javascript'>alert('请登录后再进行操作');window.location.href='User/Login.aspx';</script>");
        //}
        if (!this.IsPostBack)
        {
            Bind();
        }
    }

    public void Bind()
    {
       // string str = "Select * From movies where movieid ="+id + "";
        string str = "select * from Movies where MovieName  like '%" + Session["MovieName"].ToString() + "%'";
        SqlConnection con = DB.getConnection();
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        sda.Fill(ds, "Movie");
        this.DataList1.DataSource = ds.Tables["Movie"].DefaultView;
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Play")
        {
            string hit = (e.Item.FindControl("Label8") as Label).Text; //当前的点播次数
            string getmovieid = (e.Item.FindControl("Lable20") as Label).Text;//当前所选择的视频ID号
            int num = Convert.ToInt32(hit) + 1;
            SqlConnection con = DB.getConnection();
            if (con.State != ConnectionState.Open)
            {
                con.Close();
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("Update Movies set moviehit = " + num + " where movieid =" + getmovieid + "", con);
            cmd.ExecuteNonQuery();//执行更新的语句
            con.Close();
            Response.Redirect("Play.aspx?ID=" + getmovieid);


        }
    }
}

网站功能是视频网站,点击视频图片应该跳转页面,但是出现以上问题,怎么修改呢?

看看Session中是不是没有MovieName这个字段。

先打印一下拼接的SQLu语句,看是否正确。

如果Session中的MovieName的值不为nothing,则可以tostring,否则会出现此错误。

string str = "select * from Movies where MovieName like '%" + Session["MovieName"].ToString() + "%'";

这句之前加上 if (Session["MovieName"] = null) {Session["MovieName"] =""}

Session["MovieName"] 的值为 null 了,所以把null去Tostring()是错误的