【问题标题】:Value cannot be null in querystring查询字符串中的值不能为空
【发布时间】:2018-01-13 05:47:11
【问题描述】:

我的页面上有每个产品的 2 个链接按钮。其中一个是删除该产品,另一个是通过查询字符串将其重定向到另一个页面以编辑该产品。

 hereprotected void dlMusic_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "EditItem")
        {
            Response.Redirect("~/Admin/EditMusic.aspx?id=" + id);

        }
        else if (e.CommandName == "DeleteItem")
        {
            SqlCommand cmd = new SqlCommand("", Connection);
            cmd.CommandText = "DELETE FROM MusicTable WHERE MusicId=@id";
            cmd.Parameters.AddWithValue("@id", id);
            Connection.Open();
            cmd.ExecuteNonQuery();
            Connection.Close();
            LoadData();
        }
    }

删除按钮工作正常,但在编辑时出现问题。

 protected void Page_Load(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString["id"]);
        SqlDataAdapter da = new SqlDataAdapter("", Connection);
        DataTable dt = new DataTable();
        da.SelectCommand.CommandText = "SELECT * FROM MusicTable WHERE MusicId=@id";
        da.SelectCommand.Parameters.AddWithValue("@id", id);
        da.Fill(dt);
        string name = dt.Rows[0]["MusicName"].ToString();
        string signame = dt.Rows[0]["SingerName"].ToString();
        string prodname = dt.Rows[0]["ProducerName"].ToString();
        string albname = dt.Rows[0]["AlbumeName"].ToString();
        string des = dt.Rows[0]["Description"].ToString();
        string cover = dt.Rows[0]["Cover"].ToString();
        txtMusicName.Text = name;
        txtSingerName.Text = signame;
        txtProducerName.Text = prodname;
        txtAlbumeName.Text = albname;
        coverImg.ImageUrl = "~/images/" + cover;
        txtDes.InnerText = des;
    }

它可以正常工作,直到查询字符串请求并且错误出现

附加信息:值不能为空。

提前致谢

【问题讨论】:

  • 您在哪一行出现错误?此外,您在切换到编辑页面时是否看到正确的 URL(填充了查询字符串)?
  • int id = int.parse(request.querystring["id"]);

标签: asp.net nullreferenceexception


【解决方案1】:

从您的评论来看,“id”字段显然不是您的 QueryString 的一部分。
请在浏览器加载编辑页面时检查您的 URL(如果您放置断点并切换到浏览器窗口,您可以看到它)。
如果您认为您的网址正确,请发布您加载浏览器的屏幕截图。
另一个想法(虽然非常绝望),将“id”更改为其他任何内容(即“myid”)

Response.Redirect("~/Admin/EditMusic.aspx?myid=" + id);

int id = int.Parse(Request.QueryString["myid"]);

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多