How to read VarBinary Data from the SqlServer ASP.Net - c#

I have a binary data (Object of MyClass) in SqlServer field, But i am not able to retrieve the binary value and convert it into My Class Object.
I have used following code to write into the DB.
conn = getConnection();
MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(sql);
SqlCommand sqlCmd = new SqlCommand(#"INSERT INTO PExercise(P_Enroll_No,P_Exercises,P_Date) VALUES (#VarEnroll,#VarBinary,#Date)", conn);
sqlCmd.Parameters.Add("#VarEnroll",System.Data.SqlDbType.NVarChar,Int32.MaxValue);
sqlCmd.Parameters["#VarEnroll"].Value = sql.getEnrollNo();
sqlCmd.Parameters.Add("#VarBinary", System.Data.SqlDbType.VarBinary, Int32.MaxValue);
sqlCmd.Parameters["#VarBinary"].Value = memStream.GetBuffer();
sqlCmd.Parameters.Add("#Date",System.Data.SqlDbType.NVarChar,Int32.MaxValue);
sqlCmd.Parameters["#Date"].Value = date;
int success = sqlCmd.ExecuteNonQuery();
closeConnection();
This code successfully writes my data into the table. I have used following code to read it from database.
please help me with the code.
conn = getConnection();
SqlCommand sqlCmd = new SqlCommand(#"Select P_Exercises from PExercise where P_Enroll_No='" + enrollNo + "' AND P_Date='"+date+"'", conn);
SqlDataReader dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
byte[] bytes = new byte[1024];
long i = dr.GetBytes(0,0,bytes,0,1024*1024*8);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
mc = (List<ExerciseDetails>)bf.Deserialize(ms);
I have searched a lot but still have no luck.
Above code is giving me Exception "Invalid Read, No Data is Present".
I have also tried QueryManager to run the sql query. and its working fine there.

You could do something like the following where you do an ExecuteScaler as safe cast it as a byte array. By the way, make sure you wrap objects that implement IDisposable with using statements so they are closed and disposed properly.
using (var cn = _db.CreateConnection())
using (var cm = cn.CreateTextCommand(sql))
{
cm.AddInParam("name", DbType.String, name);
cn.Open();
var data = cm.ExecuteScalar() as byte[];
return data;
}

Related

C# retrieving image in db. Get Error Parameter in not valid

I don't know what's wrong with my code in retrieving image in db. I insert image without using image path file cause the image provide by the cam.
Here's my code in inserting image in db
Image img = pictureBox1.Image;
MemoryStream memStream = new MemoryStream();
img.Save(memStream, ImageFormat.Bmp);
byte[] imageBt = memStream.ToArray();
query = "";
query += "INSERT INTO table(picture) VALUES ('" + imageBt + "')";
cmd = new MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.close();
Here's my code in retrieving image in db
query = "";
query += "SELECT picture FROM table WHERE id = '1'";
cmd = new MySqlCommand(query, con);
con.Open();
MemoryStream ms = new MemoryStream();
byte[] image = (byte[])cmd.ExecuteScalar();
ms.Write(image, 0, image.Length);
con.Close();
Bitmap bmp = new Bitmap(ms)
pictureBox1.Image = bmp; //Still get the Error here parameter is not valid
Is there anywrong process in saving image in database. Btw my image type in db is Blob. I don't know why it doesn't work in retrieving image it always thrown error. Thanks
When reading, you haven't rewound the stream. If you Write, you must set ms.Position = 0; afterwards. Or simpler: create the stream from the data, then you don't need to:
byte[] image = ...
var ms = new MemoryStream(image);
When writing, you seem to have injected the data directly. That... almost certainly won't work - in fact, you're probably writing System.Byte[] to the command. Ideally, use a parameter:
query = "INSERT INTO table(picture) VALUES (#blob)";
cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("blob", imageBt);
cmd.ExecuteNonQuery();
(the exact syntax may change between RDBMS implementations)

while inserting the record into access db exception throw in c#

iam trying to insert a record into ms-access database for that below code and method use
string SqlString = "Insert Into RegistrationForm (ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
conv_photo();
using (OleDbConnection conn = new OleDbConnection(SqlString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("ClientCount", lblcount.Text);
cmd.Parameters.AddWithValue("Name", textBox20.Text);
cmd.Parameters.AddWithValue("Address", textBox21.Text);
cmd.Parameters.AddWithValue("Contact", textBox19.Text);
cmd.Parameters.AddWithValue("Documents", textBox18.Text);
cmd.Parameters.AddWithValue("Money_Taking_Date", maskedTextBox1.Text.ToString());
cmd.Parameters.AddWithValue("Muddat", textBox22.Text);
cmd.Parameters.AddWithValue("Money_Return_date", maskedTextBox2.Text.ToString());
cmd.Parameters.AddWithValue("Account_status", textBox23.Text);
cmd.Parameters.AddWithValue("Taking_Amout", textBox17.Text);
cmd.Parameters.AddWithValue("Interest_per_month", textBox16.Text);
cmd.Parameters.AddWithValue("Pending_interest_month", textBox15.Text);
cmd.Parameters.AddWithValue("Pending_interst_Amount", Convert.ToDouble(textBox13.Text));
cmd.Parameters.AddWithValue("Total_Amount", Convert.ToDouble(textBox14.Text));
cmd.Parameters.AddWithValue("#Client_image", pictureBox6);
cmd.Parameters.AddWithValue("Document_image1", pictureBox4);
cmd.Parameters.AddWithValue("Document_image2", pictureBox5);
conn.Open();
int n=cmd.ExecuteNonQuery();
conn.Close();
if (n > 0)
{
MessageBox.Show("record inserted");
loaddata();
// rno++;
}
else
MessageBox.Show("insertion failed");
}
}
method conv_photo :
public void conv_photo()
{
//converting photo to binary data
if (pictureBox6.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox6.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("#pictureBox6", photo_aray);
}
if (pictureBox4.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox4.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("#pictureBox4", photo_aray);
}
if (pictureBox5.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox5.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("#pictureBox5", photo_aray);
}
now problem is when i run the application
Object reference not set to an instance of an object execption throw in cmd.Parameters.AddWithValue("#pictureBox6", photo_aray); line
if i put conv_photo method after
using (OleDbConnection conn = new OleDbConnection(SqlString))
{
}
loop it giving me Format of the initialization string does not conform to specification starting at index 0. execption (Argument exception waS unhandled)
not getting what should i do .
Two issues here:
You appear to be using two versions of cmd. One seems to be global, since it is available inside of conv_photo, and the other is instantiated inside of wherever your top method is. The global one is not being instantiated, at all (according to what you have displayed.)
Your using statement creates a brand new cmd object that has no relation to the one you're attempting to use inside of conv_photo. Get rid of your global cmd object and put the call to conv_photo inside of your
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
and pass the cmd object into it, as an argument.
Your SqlString is NOT a connection string. It is a SQL Statement. You're going to need to look into what a connection string is, if you are unfamiliar.

Read Image from SQL Server VarBinary(Max)

I have a SQL Server Database. I have stored an image in there which is a varbinary(max). I inserted the image the following way :
string consString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection con = new SqlConnection(consString);
con.Open();
String filePath = fuImage.PostedFile.FileName;
String naam = Path.GetFileName(filePath);
String extension = Path.GetExtension(naam);
Stream stream = fuImage.PostedFile.InputStream;
BinaryReader br = new BinaryReader(stream);
Byte[] imgByte = br.ReadBytes((Int32)stream.Length);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spAddImage";
cmd.Parameters.AddWithValue("#FOTO", imgByte);
cmd.Parameters.AddWithValue("#ARTIEST", ddlArtiest.SelectedValue);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
The image now looks like this in the database :
It is stored as a varbinary.
http://puu.sh/ikF83/6a03b52520.png <--- database
Now I want to display my image on my asp.net page that I have inserted.
But I have no idea how I can achieve this.
You could use SqlDataReader to go over the results and get back what you need
var reader = cmd.ExecuteReader();
while (reader.Read())
{
byte[] myImage = (byte[])reader["MyImageColumn"];
}
This is from top of my head so make your adjustments as required. But it should give you the basic idea.
Furthermore, It's strongly recommended to use the using block as it ensures the object is disposed correctly.
So you could change your code to
using(var connection = new SqlConnection(connectionString))
{
//Your command
connection.Open();
//Datareader here
}//Object disposed here
Reference to SqlDataReader
Reference to using block
Extending the answer provided by Izzy, below is how we have implemented the same.
public HttpResponseMessage RenderImage(RenderRequest renderRequest, HttpRequestMessage request)
{
var httpResponse = new HttpResponseMessage();
try
{
var reader = cmd.ExecuteReader();
while (reader.Read())
{
byte[] imagebytes = (byte[])reader["MyImageColumn"];
}
return httpResponse = request.CreateResponse<byte[]>(HttpStatusCode.OK, imagebytes, new ImageMediaFormatter("image/png"));
}
catch ()
{
throw;
}
}

populate list with images from database

I want to retrieve the images from SQL table and save it in the list and show it in my listview. But I don't know how to do this. I need your help.
I am using SQL Server 2008, Visual Studio 2008, C# Window Application.
Here is my code:
cmd = new SqlCommand("Select ScanImage from ScanDocuments", con);
dr = cmd.ExecuteReader();
List<ImageList> lstitem = new List<ImageList>();
while (dr.Read())
{
ImageList _image = new ImageList();
byte[] data = (byte[])dr["ScanImage"];
MemoryStream ms = new MemoryStream(data);
Image bmp = new Bitmap(ms);
lstitem.Add(bmp);
}
Your code has several flaws - you need to use something like this instead:
// define connection string and select statement
// I used AdventureWorks 2012 database - change to match *YOUR* environment
string connectionString = "server=.;database=AdventureWorks2012;integrated security=SSPI;";
string query = "SELECT ThumbNailPhoto FROM Production.ProductPhoto";
// define a list of "Image" objects
List<Image> listOfImages = new List<Image>();
// using the SqlConnection and SqlCommand ....
using(SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand selectCmd = new SqlCommand(query, conn))
{
// open connection
conn.Open();
// execute SqlCommand to return a SqlDataReader
using (SqlDataReader rdr = selectCmd.ExecuteReader())
{
// iterate over the reader
while (rdr.Read())
{
// load the bytes from the database that represent your image
var imageBytes = (byte[]) rdr[0];
// put those bytes into a memory stream and "rewind" the memory stream
MemoryStream memStm = new MemoryStream(imageBytes);
memStm.Seek(0, SeekOrigin.Begin);
// create an "Image" from that memory stream
Image image = Image.FromStream(memStm);
// add image to list
listOfImages.Add(image);
}
}
conn.Close();
}
This works just fine for me - it loads the 101 ThumbNailPhoto from the AdventureWorks2012 database

open word document (saved as binary) from database

I'm making a program with c# and sql server and I have a problem , I hope if anyone help me .
I will but the database on pc and the program will be installed in other PCs , and app pcs' program connected to that database.
the program saving documents (word -excel) as binary ,using this code:
byte[] ReadFile(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null;
//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);
//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
}
private void button1_Click(object sender, EventArgs e)
{
string dt = dateTimePicker1.Value.ToShortDateString();
byte[] red = ReadFile(textBox3.Text);
con.Open();
string qry = "insert into documents ([Account no],Name,[Phone number],Date,[Document name],Document,Type) values(#accon,#name,#phone,#date,#docname,#doc,#type)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, con);
//We are passing Original Image Path and Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("#accon", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("#name", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("#phone", textBox3.Text));
SqlCom.Parameters.Add(new SqlParameter("#date", dt));
SqlCom.Parameters.Add(new SqlParameter("#docname", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("#doc", (object)red));
SqlCom.Parameters.Add(new SqlParameter("#type", (object)textBox2.Text));
SqlCom.ExecuteNonQuery();
con.Close();
MessageBox.Show("done");
}
the problem : that I don't know how to retrieve saved documents in database and open it with Microsoft word or Microsoft Excel according to their types.
I want to select specific document form database and open it
Thanks in advance
String connStr = "connection string";
// add here extension that depends on your file type
string fileName = Path.GetTempFileName() + ".doc";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
// you have to distinguish here which document, I assume that there is an `id` column
cmd.CommandText = "select document from documents where id = #id";
cmd.Parameters.Add("#id", SqlDbType.Int).Value = 1;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
int size = 1024 * 1024;
byte[] buffer = new byte[size];
int readBytes = 0;
int index = 0;
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
{
fs.Write(buffer, 0, readBytes);
index += readBytes;
}
}
}
}
}
}
// open your file, the proper application will be executed because of proper file extension
Process prc = new Process();
prc.StartInfo.FileName = fileName;
prc.Start();
After you have retrieved your document from the database (or any type of storage you care to use on the server) you should save the document in the windows temporary folder (Path.GetSpecialFolder) and use the Word Interop library to start word (or excel using its own interop library) with the document you just saved.
var temporayFileName = Path.GetRandomFileName();
var temporaryFileStream = File.Open(temporaryFileName, FileMode.Create);
var memoryStream = documentRepository.Get(...);
memoryStream.CopyTo(temporaryFileStream);
// Word App
dynamic wordApp = new Application { Visible = true };
var doc = wordApp.Documents.Add(TemplateName);
templatedDocument.Activate();
(See this document for more information about starting and manipulating word:
http://msdn.microsoft.com/en-us/magazine/ff714583.aspx)
.
The crux is Response.ContentType:
Response.ContentType = "application/vnd.xls"; // for excel
Response.ContentType = "application/ms-word"; // for word
Response.ContentType = "image/jpg";//for jpg images
It is advised to store content type also in database so that your code will be generic and can display/store any type of file
System.Data.SqlClient.SqlDataReader rdr = null;
System.Data.SqlClient.SqlConnection conn = null;
System.Data.SqlClient.SqlCommand selcmd = null;
try
{
conn = new System.Data.SqlClient.SqlConnection(
System.Configuration.ConfigurationManager
.ConnectionStrings["ConnectionString"].ConnectionString);
selcmd = new System.Data.SqlClient.SqlCommand(
"select pic1 from msg where msgid=" + Request.QueryString["imgid"],
conn);
conn.Open();
rdr = selcmd.ExecuteReader();
while (rdr.Read())
{
Response.ContentType = "image/jpg";
Response.BinaryWrite((byte[])rdr["pic1"]);
}
if (rdr != null)
rdr.Close();
}
finally
{
if (conn != null)
conn.Close();
}

Categories