| | |
Getting OUTPUT values back from SQL Stored Procedure
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
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.
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
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
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.
C# Syntax (Toggle Plain Text)
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
SQL Syntax (Toggle Plain Text)
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
Thanks
Last edited by sean_morris9; Feb 22nd, 2008 at 6:25 am.
I see your SP not written well what about that
Pass your image ID
It works with me
C# Syntax (Toggle Plain Text)
alter PROCEDURE dbo.spGet_image ( @Image_id int ) AS SELECT [Image_MIME_type] ,[Image_image] FROM [MailSystem].[dbo].[image] WHERE Image.Image_id = @Image_id
C# Syntax (Toggle Plain Text)
SqlCommand cmd = new SqlCommand("spGet_image", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add( new SqlParameter("@Image_id", SqlDbType.INT,4)); cmd.Parameters["@Image_id"].Value = pic_id; adm = cmd.ExecuteReader(); if (adm.HasRows) { while (adm.Read()) { //System.Windows.Forms.MessageBox.Show("We got data"); Response.Redirect("image.aspx"); Response.ContentType = adm["Type"].ToString(); Response.BinaryWrite((byte[])adm["Image"]); Response.Write("hello!!!!"); } } else { System.Windows.Forms.MessageBox.Show("We have no data"); }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
Hi Ramy,
It's getting data back from the database now, but it's coming up as a broken image. Any ideas? So close!
It's getting data back from the database now, but it's coming up as a broken image. Any ideas? So close!
C# Syntax (Toggle Plain Text)
if (adm.HasRows) { while (adm.Read()) { // Response.Write(adm["Image_MIME_type"].ToString()); Response.ContentType = adm["Image_MIME_type"].ToString(); Response.BinaryWrite((byte[])adm["Image_image"]); } } else { Response.Write("Reaer has no rows"); } adm.Close(); conn.Close();
Last edited by sean_morris9; Feb 25th, 2008 at 10:05 am.
did u insert the whole binary data for the image??!
how did u insert let me check your code??
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
Thanks for alll you help Ramy,
Here is the code for inserting the image...
code behind page..
Content_Manager C# class
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
Here is the code for inserting the image...
code behind page..
C# Syntax (Toggle Plain Text)
int imgLen = FileUpload1.PostedFile.ContentLength; string imgContentType = FileUpload1.PostedFile.ContentType; string imgName = txtname.Text; string ac_name; //declares a string to hold username ac_name = txtid.Text;//please ignore name of text box it's for the name not id Who_Am_I who = new Who_Am_I(); int accommodation_id = who.mGet_ID(ac_name); string description = txtdescription.Text; byte[] imgBinaryData = new byte[imgLen]; Content_Manager content = new Content_Manager(); content.Insert_Image(accommodation_id, imgName, description, imgContentType, imgBinaryData);
C# Syntax (Toggle Plain Text)
public void Insert_Image(int id, string image_name, string alt, string type, byte[] img) { // Get a connection to the database string s; s = (ConfigurationManager.ConnectionStrings["Accommodation_Centre_Connection"].ToString()); SqlConnection conn = new SqlConnection(s); conn.Open();//opens connection SqlDataReader adm = null;//declares the data reader //Call the stored procedure SqlCommand cmd = new SqlCommand("spInsert_Photo", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add( new SqlParameter("@accomm_id", id)); cmd.Parameters.Add( new SqlParameter("@name", image_name)); cmd.Parameters.Add( new SqlParameter("@alt", alt)); cmd.Parameters.Add( new SqlParameter("@type", type)); cmd.Parameters.Add( new SqlParameter("@image", img)); adm = cmd.ExecuteReader(); adm.Close(); conn.Close(); }
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
![]() |
Similar Threads
- ASP.net/Stored Proc & Login Verification (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Reading binary data from SPROC..help please...
- Next Thread: How to set the focus on a text box..?
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox connection console control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment developer development draganddrop drawing editing encryption enum event excel file form format forms function gdi+ hospitalmanagementinformationsystem httpwebrequest image imageprocessing index input install java label list listbox mandelbrot math mouseclick mysql operator oracle path photoshop picturebox pixelinversion post priviallages. programming radians regex remote remoting richtextbox rows serialization server sleep socket sql statistics stream string table temperature text textbox thread time timer txt update uploadatextfile usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf xml






