Hi,
I'm looking for help with this. I'm tring to get an image back from my SQL Server Express database. The code below is where I'm calling the stored procedure from. If I leave out the IF STATEMENT and WHILE LOOP I'm getting an error saying it's trying to read data when no data is present. From this I get that the SPOC isn't returning anything. I tried changing the parameter direction to inputoutput instead of output, but it's the exact same. It's also the same when I don't put the parameters = NULL.
string s; // creates a string to hold connection string
//set string = connection string from web.config
s = (ConfigurationManager.ConnectionStrings["Accommodation_Centre_Connection"].ToString());
//create the connection using
SqlConnection conn = new SqlConnection(s);
conn.Open();//opens connection
int pic_id = Convert.ToInt32(Request.QueryString["img"]); //declares a string to hold pic id
SqlDataReader adm = null;//declares the data reader
//Call the stored procedure
SqlCommand cmd = new SqlCommand("spGet_image", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@Image_id", pic_id));
SqlParameter Image = new SqlParameter("@image", SqlDbType.Binary);
//Image = null;
Image.Direction = ParameterDirection.InputOutput;
Image.Value = null;
Image.Size = 8000;
cmd.Parameters.Add(Image);
SqlParameter Type = new SqlParameter("@type", SqlDbType.NVarChar);
// Type=null;
Type.Direction = ParameterDirection.InputOutput;
Type.Value = null;
Type.Size = 50;
cmd.Parameters.Add(Type);
adm = cmd.ExecuteReader();
if (adm.HasRows)
{
while (adm.Read())
{
Response.Redirect("image.aspx");
Response.ContentType = adm["Type"].ToString();
Response.BinaryWrite((byte[])adm["Image"]);
Response.Write("hello!!!!");
}
}
else
{
Response.Write("NOOOOOOOOOOOOOOOOOOO");
}
adm.Close();
conn.Close();
NOTE: It's not outputting eiter Response.Write("hello!!!!"); or Response.Write("NOOOOOOOOOOOOOOOOOOO"); I don't understand this, surely the reader either has rows or doesn't?
Below is the SPROC
ALTER PROCEDURE dbo.spGet_image
(
@Image_id int = NULL,
@type nvarchar(50) = NULL OUTPUT,
@image varbinary(MAX)= NULL OUTPUT
)
AS
BEGIN
SET @type=(
SELECT Image.Image_MIME_type
FROM Image
WHERE Image.Image_id = @Image_id)
SET @image=(
SELECT Image.Image_image
FROM Image
WHERE Image.Image_id = @Image_id)
END
Again I've tried variations on this. I've tried it without having the parameters = NULL. I've also tried leaving out the BEGIN and END, and a few more things. Any help would be appreciated!!!
Thanks