Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one.
I'm using picture box to save image in database but now i want if there is no image brows then it save null value in image attribute in database. Any one tell me how can i do this
I'm using this code to save image in SQL server 2014

What I have tried:

MemoryStream ms = new MemoryStream();
                           pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                           byte[] a = ms.GetBuffer();
                           ms.Close();
                           cmd.Parameters.AddWithValue("@LogoImage", a);
Posted
Updated 7-Dec-18 22:33pm

1 solution

Assuming that your DB accepts NULL values in the column:
C#
byte[] a = null;
if (pictureBox1.Image != null)
    {
    MemoryStream ms = new MemoryStream();
    pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
    a = ms.GetBuffer();
    ms.Close();
    }
cmd.Parameters.AddWithValue("@LogoImage", a);
 
Share this answer
 
Comments
Fahid Zahoor 10-Dec-18 13:42pm    
i follow your code but it give this type of error message

System.Data.SqlClient.SqlException: 'Procedure or function 'USP_Insert_Product_Details' expects parameter '@LogoImage', which was not supplied.'
but i supply this parameter please help me
OriginalGriff 10-Dec-18 13:48pm    
But if it's null, and your SP doesn't specifically accept a null parameter ...
Check your SP definition.
Fahid Zahoor 11-Dec-18 0:47am    
Its my Table
CREATE TABLE [dbo].[Product_Details](
[PID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[PDescription] [nvarchar](250) NULL,
[Category] [nvarchar](50) NULL,
[Scategory] [nvarchar](50) NULL,
[SScategeory] [nvarchar](50) NULL,
[Brand] [nvarchar](50) NULL,
[GroupName] [nvarchar](50) NULL,
[Vatpercentage] [nvarchar](50) NULL,
[VatState] [nvarchar](50) NULL,
[Ptype] [nvarchar](50) NULL,
[Weight] [nvarchar](50) NULL,
[QtyAlret] [float] NULL,
[Isactiveoffer] [bit] NULL,
[Isexpiryproduct] [bit] NULL,
[PImage] [image] NULL,
CONSTRAINT [PK_Product_Details] PRIMARY KEY CLUSTERED
(
[PID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
=============================================
Its my Store procedure please check it
ALTER procedure [dbo].[USP_Insert_Product_Details]
(@Name nvarchar(50)
,@PDescription nvarchar(250)
,@Category nvarchar(50)
,@Scategory nvarchar(50)
,@SScategeory nvarchar(50)
,@Brand nvarchar(50)
,@GroupName nvarchar(50)
,@Vatpercentage nvarchar(50)
,@VatState nvarchar(50)
,@Ptype nvarchar(50)
,@Weight nvarchar(50)
,@QtyAlret float
,@Isactiveoffer bit
,@Isexpiryproduct bit
,@PImage image)
as
begin
INSERT INTO [dbo].[Product_Details]
([Name]
,[PDescription]
,[Category]
,[Scategory]
,[SScategeory]
,[Brand]
,[GroupName]
,[Vatpercentage]
,[VatState]
,[Ptype]
,[Weight]
,[QtyAlret]
,[Isactiveoffer]
,[Isexpiryproduct]
,[PImage])
VALUES
(@Name
,@PDescription
,@Category
,@Scategory
,@SScategeory
,@Brand
,@GroupName
,@Vatpercentage
,@VatState
,@Ptype
,@Weight
,@QtyAlret
,@Isactiveoffer
,@Isexpiryproduct
,@PImage )
end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900