Last post Oct 28, 2010 04:02 PM by jesal27
Nov 14, 2008 07:27 AM|aspnetAficionado|LINK
I've been working on a code that allows users to upload images (jpgs) as an optional field. Alhough the uploading works, I'm struggling when the user doesn't have an image to upload. I can't seem to find the way to NOT insert binary
data into my database, I just want an NULL value when there is no image.
I get this error message: ...expects parameter @Image, which was not supplied.
Here is the code:
Nov 14, 2008 08:39 AM|Corwin|LINK
Where you are checking for imageData having a value, you could also build your sqlCmd1 string. If value found, build insert as you have now, with all three fields. If no value found, sqlCmd1 = "INSERT INTO Add_Item (Title, MIMEType) VALUES (@Title, @MIMEType)",
etc. Same with Cmd1.Parameters, only .Add the two. Then do the Try/Catch.
(Another thing you may want to consider - is there a reason you want to do the insert when no Image is upload?)
Nov 14, 2008 08:52 AM|dhimant|LINK
You need to make some provision in your code to handle the case when no image is uploaded ( albeit strange but can be possible based on how you've coded the page ).
One way could be to show the user with some message and cancel the table insert.
Nov 14, 2008 09:57 AM|aspnetAficionado|LINK
Thanks for your quick response.
I went for creating two sqlCmd1 strings/parameters. Pretty obvious! I'm just starting with .net, as you can see.
To answer your question Title, Image and MIMEType are just three of a very long list of fields I need to insert into my database. Some of them are compulsory, others, like Image, are optional.
I would like anyway to know how to set a null value to binary data. I've tried the following without success:
imageData = Nothing
Nov 14, 2008 10:11 AM|dhimant|LINK
You may alternatively try a stored procedure to insert the values in your table. You can then have any parameter which is optional to have a default value of null inside the SP. Then in the .Net code, just ignore adding and setting the value for the parameter
for which the value is missing. Below is the psuedo example for you.
create procedure p_insertData
@param1 int ,
@param2 varchar(50) = null -- this will be the optional parameter ,
insert MyTable ( col1 , col2 , ... , colN ) values ( @param1 , @param2 , ... , @paramN )
Nov 14, 2008 10:56 AM|Naom|LINK
I was having similar problem with Image uploading. I need to review my code and SP for this, I'll be able to do so Saturday's night.
Nov 14, 2008 12:56 PM|Corwin|LINK
You shouldn't have to set it to anything. You don't need to supply that value as a parameter since the INSERT statement won't contain that column. In your database, make sure that column is nullable.
Nov 15, 2008 08:28 PM|Naom|LINK
In my SP I have parameter defined as
In insert statement I just insert this parameter.
Here is my code in C# User-Control
// string script = @" var Pic = document.getElementById('picresult');
// if (Pic)
// Pic.src = '" + Request_PhysicalPath + imgName + @"';";
// this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowPic", script, true);
// // picresult.src = Request_PhysicalPath + imgName;
Oct 28, 2010 04:02 PM|jesal27|LINK
One way to handle situations like this is something as follows..
In App. Code ->
Dim p As New SqlClient.SqlParameter
p.ParameterName = "@FileName"
p.DbType = DbType.Binary
If fileName Is Nothing Then
p.Value = System.DBNull.Value
p.Value = fileName (<-- Your Byte Array with Image/PDF/Etc File Contents)
In Database, simply allow Null in the Field and do your regular insert as shown below:
create procedure [dbo].[UploadFax](
@FileName as image,
insert into ImageTable