代码如下:
 protected void Page_Load(object sender, EventArgs e)
    {
        string productID = Request.QueryString["ProductID"].ToString();//这一行一直报错,可是不知道怎样改正
string photoSize = Request.QueryString["Size"].ToString();
System.Data.SqlClient.SqlDataReader imageReader;
        string dbConn = ConfigurationManager.ConnectionStrings["MyAdventureWorksConnectionString1"].ConnectionString;
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(dbConn);
sqlConn.Open();
System.Data.SqlClient.SqlCommand imageCommand = new System.Data.SqlClient.SqlCommand(
"SELECT ThumbNailPhoto, LargePhoto FROM Product "
+ "INNER JOIN ProductProductPhoto ON Product.ProductID = ProductProductPhoto.ProductID "
+ "INNER JOIN ProductPhoto ON ProductProductPhoto.ProductPhotoID = ProductPhoto.ProductPhotoID "
+ "WHERE Product.ProductID=" + productID,sqlConn);
imageReader = imageCommand.ExecuteReader(CommandBehavior.CloseConnection);
        try
        {
            imageReader.Read();
            if (photoSize == "ThumbNail")
            {
                byte[] imageBytes = (byte[])imageReader["ThumbNailPhoto"];
                Response.ContentType = "image/GIF";
                Response.BinaryWrite(imageBytes);
            }
            else if (photoSize == "Large")
            {
                byte[] imageBytes = (byte[])imageReader["LargePhoto"];
                Response.ContentType = "image/JPEG";
                Response.BinaryWrite(imageBytes);
            }
        }
        finally
        {
            imageReader.Close();
            sqlConn.Close();
        }

    }发生的错误是:
未将对象引用设置到对象的实例。 
错误行: string productID = Request.QueryString["ProductID"].ToString();
请高手指教!

解决方案 »

  1.   

    首先这个Request.QueryString["ProductID"]返回的就是字符串 
    再者你可以加个判断 string.isNullOrEmpty(Request.QueryString["ProductID"])
      

  2.   

    if(Request.QueryString["ProductID"]!=null)
    productID = Request.QueryString["ProductID"].ToString();
    else
    productID = "0";
      

  3.   

    url里面没有 ProductID=xxx 这样的参数
      

  4.   

    string productID = Request.QueryString["ProductID"].ToString();//如果传过来的ProductID 为空值,自然会报错,首先if他为非空吧,顶楼上!