Getting OUTPUT values back from SQL Stored Procedure

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 6
Reputation: sean_morris9 is an unknown quantity at this point 
Solved Threads: 0
sean_morris9 sean_morris9 is offline Offline
Newbie Poster

Getting OUTPUT values back from SQL Stored Procedure

 
0
  #1
Feb 22nd, 2008
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.

  1. string s; // creates a string to hold connection string
  2. //set string = connection string from web.config
  3. s = (ConfigurationManager.ConnectionStrings["Accommodation_Centre_Connection"].ToString());
  4. //create the connection using
  5. SqlConnection conn = new SqlConnection(s);
  6. conn.Open();//opens connection
  7. int pic_id = Convert.ToInt32(Request.QueryString["img"]); //declares a string to hold pic id
  8. SqlDataReader adm = null;//declares the data reader
  9.  
  10. //Call the stored procedure
  11.  
  12. SqlCommand cmd = new SqlCommand("spGet_image", conn);
  13. cmd.CommandType = CommandType.StoredProcedure;
  14. cmd.Parameters.Add(
  15. new SqlParameter("@Image_id", pic_id));
  16. SqlParameter Image = new SqlParameter("@image", SqlDbType.Binary);
  17. //Image = null;
  18. Image.Direction = ParameterDirection.InputOutput;
  19. Image.Value = null;
  20. Image.Size = 8000;
  21. cmd.Parameters.Add(Image);
  22. SqlParameter Type = new SqlParameter("@type", SqlDbType.NVarChar);
  23. // Type=null;
  24. Type.Direction = ParameterDirection.InputOutput;
  25. Type.Value = null;
  26. Type.Size = 50;
  27. cmd.Parameters.Add(Type);
  28.  
  29. adm = cmd.ExecuteReader();
  30.  
  31.  
  32.  
  33.  
  34. if (adm.HasRows)
  35. {
  36. while (adm.Read())
  37. {
  38. Response.Redirect("image.aspx");
  39. Response.ContentType = adm["Type"].ToString();
  40. Response.BinaryWrite((byte[])adm["Image"]);
  41. Response.Write("hello!!!!");
  42. }
  43.  
  44. }
  45. else
  46. {
  47. Response.Write("NOOOOOOOOOOOOOOOOOOO");
  48. }
  49.  
  50.  
  51.  
  52. adm.Close();
  53. 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

  1. ALTER PROCEDURE dbo.spGet_image
  2.  
  3. (
  4. @Image_id INT = NULL,
  5. @type nvarchar(50) = NULL OUTPUT,
  6. @image VARBINARY(MAX)= NULL OUTPUT
  7. )
  8.  
  9. AS
  10. BEGIN
  11. SET @type=(
  12. SELECT Image.Image_MIME_type
  13. FROM Image
  14. WHERE Image.Image_id = @Image_id)
  15.  
  16. SET @image=(
  17. SELECT Image.Image_image
  18. FROM Image
  19. WHERE Image.Image_id = @Image_id)
  20.  
  21. 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
Last edited by sean_morris9; Feb 22nd, 2008 at 6:25 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Getting OUTPUT values back from SQL Stored Procedure

 
0
  #2
Feb 22nd, 2008
I see your SP not written well what about that
  1. alter PROCEDURE dbo.spGet_image
  2. ( @Image_id int
  3. ) AS
  4. SELECT [Image_MIME_type]
  5. ,[Image_image]
  6. FROM [MailSystem].[dbo].[image] WHERE Image.Image_id = @Image_id
Pass your image ID
  1. SqlCommand cmd = new SqlCommand("spGet_image", conn);
  2. cmd.CommandType = CommandType.StoredProcedure;
  3. cmd.Parameters.Add(
  4. new SqlParameter("@Image_id", SqlDbType.INT,4));
  5. cmd.Parameters["@Image_id"].Value = pic_id;
  6. adm = cmd.ExecuteReader();
  7. if (adm.HasRows)
  8. {
  9. while (adm.Read())
  10. {
  11. //System.Windows.Forms.MessageBox.Show("We got data");
  12. Response.Redirect("image.aspx");
  13. Response.ContentType = adm["Type"].ToString();
  14. Response.BinaryWrite((byte[])adm["Image"]);
  15. Response.Write("hello!!!!");
  16. }
  17.  
  18. }
  19. else
  20. {
  21. System.Windows.Forms.MessageBox.Show("We have no data");
  22. }
It works with me
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: sean_morris9 is an unknown quantity at this point 
Solved Threads: 0
sean_morris9 sean_morris9 is offline Offline
Newbie Poster

Re: Getting OUTPUT values back from SQL Stored Procedure

 
0
  #3
Feb 25th, 2008
Hi Ramy,
It's getting data back from the database now, but it's coming up as a broken image. Any ideas? So close!

  1. if (adm.HasRows)
  2. {
  3.  
  4. while (adm.Read())
  5. {
  6. // Response.Write(adm["Image_MIME_type"].ToString());
  7.  
  8. Response.ContentType = adm["Image_MIME_type"].ToString();
  9. Response.BinaryWrite((byte[])adm["Image_image"]);
  10. }
  11. }
  12. else
  13. {
  14. Response.Write("Reaer has no rows");
  15.  
  16. }
  17. adm.Close();
  18. conn.Close();
Last edited by sean_morris9; Feb 25th, 2008 at 10:05 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Getting OUTPUT values back from SQL Stored Procedure

 
0
  #4
Feb 27th, 2008
did u insert the whole binary data for the image??!
how did u insert let me check your code??
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: sean_morris9 is an unknown quantity at this point 
Solved Threads: 0
sean_morris9 sean_morris9 is offline Offline
Newbie Poster

Re: Getting OUTPUT values back from SQL Stored Procedure

 
0
  #5
Feb 27th, 2008
Thanks for alll you help Ramy,

Here is the code for inserting the image...
code behind page..
  1. int imgLen = FileUpload1.PostedFile.ContentLength;
  2. string imgContentType = FileUpload1.PostedFile.ContentType;
  3. string imgName = txtname.Text;
  4. string ac_name; //declares a string to hold username
  5.  
  6. ac_name = txtid.Text;//please ignore name of text box it's for the name not id
  7. Who_Am_I who = new Who_Am_I();
  8. int accommodation_id = who.mGet_ID(ac_name);
  9. string description = txtdescription.Text;
  10. byte[] imgBinaryData = new byte[imgLen];
  11.  
  12. Content_Manager content = new Content_Manager();
  13. content.Insert_Image(accommodation_id, imgName, description, imgContentType, imgBinaryData);
Content_Manager C# class
  1. public void Insert_Image(int id, string image_name, string alt, string type, byte[] img)
  2. {
  3. // Get a connection to the database
  4. string s;
  5. s = (ConfigurationManager.ConnectionStrings["Accommodation_Centre_Connection"].ToString());
  6. SqlConnection conn = new SqlConnection(s);
  7. conn.Open();//opens connection
  8. SqlDataReader adm = null;//declares the data reader
  9. //Call the stored procedure
  10. SqlCommand cmd = new SqlCommand("spInsert_Photo", conn);
  11. cmd.CommandType = CommandType.StoredProcedure;
  12. cmd.Parameters.Add(
  13. new SqlParameter("@accomm_id", id));
  14. cmd.Parameters.Add(
  15. new SqlParameter("@name", image_name));
  16. cmd.Parameters.Add(
  17. new SqlParameter("@alt", alt));
  18. cmd.Parameters.Add(
  19. new SqlParameter("@type", type));
  20. cmd.Parameters.Add(
  21. new SqlParameter("@image", img));
  22. adm = cmd.ExecuteReader();
  23. adm.Close();
  24. conn.Close();
  25. }

everything seems to be inserting into the database fine. Binary data shown as <binary data>. That's normal I think.

Maybe the binary is corrupted in some way?

Thank u so much for the help, I'm really under pressure.

Sean
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC