---
title: "problem saving image in sql server 2014"  
description: "problem saving image in sql server 2014"  
author: "Soleiman Ghamshadzahi"  
published: 2014-12-22  
updated: 2014-12-22  
canonical: https://www.mindstick.com/forum/12805/problem-saving-image-in-sql-server-2014  
category: "asp.net"  
tags: ["asp.net", "sql server"]  
reading_time: 1 minute  

---

# problem saving image in sql server 2014

hi\
i have [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with saving jpeg image to [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server)\
below is my saving code \
*************\
string con_string = System.Configuration.ConfigurationManager.AppSettings["con_string"];\
System.Data.SqlClient.SqlConnection conobj = new System.Data.SqlClient.SqlConnection(con_string);\
\
string sql = "INSERT INTO Docs (nid,photo) VALUES (@nid,@photo)";\
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, conobj); \
int length = photoupload.PostedFile.ContentLength;

//create a [byte array](https://www.mindstick.com/forum/33877/char-array-to-byte-array-conversion-and-convert-back-again-to-char-arrray) to store the binary image data

byte[] imgbyte = new byte[length];

//store the currently selected file in memeory

[HttpPostedFile](https://www.mindstick.com/forum/23277/how-to-clone-a-httppostedfile) img = photoupload.PostedFile;

//set the [binary data](https://www.mindstick.com/blog/302219/explore-the-science-binary-data-here)

img.InputStream.Read(imgbyte, 0, length);

cmd.Parameters.AddWithValue("@nid",nid_lbl .Text );\
cmd.Parameters.Add("@photo", SqlDbType.VarBinary ).Value = imgbyte;\
cmd.Connection = conobj;\
conobj.Open();\
cmd.ExecuteNonQuery();\
conobj.Close();\
***************\
and my docs table [structure](https://www.mindstick.com/articles/23258/choose-your-business-structure-wisely) is like below \
**********\
nid char\
photo varbinary(max)\
************\
a new row add to docs table when i press save button but after [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) using handler it show nothing to [image control](https://www.mindstick.com/forum/95341/how-can-use-upload-image-control-in-asp-dot-net) \
my retrieve handler is correct problem is with save [procedure](https://www.mindstick.com/forum/32/stored-procedure-return-datatype)\
please help \
thanks


---

Original Source: https://www.mindstick.com/forum/12805/problem-saving-image-in-sql-server-2014

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
