Value cannot be null in querystring

时间:2017-08-05 11:04:48

标签: asp.net nullreferenceexception

I have 2 link buttons on my page for each product.1 of them is delete that product and the other is redirect it by query string to the other page to Edit that product.

 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();
        }
    }

Delete button worked correctly but on edit I have problem.

 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;
    }

It works correctly until requested by query string and the error come is

Additional information: Value cannot be null.

Thanks in advance

1 个答案:

答案 0 :(得分:0)

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

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

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