代码如下:
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();
请高手指教!
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();
请高手指教!
再者你可以加个判断 string.isNullOrEmpty(Request.QueryString["ProductID"])
productID = Request.QueryString["ProductID"].ToString();
else
productID = "0";