473,243 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,243 software developers and data experts.

Unable to Display Image from database at runtime

Hi everyone,

Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).

The process after being displayed in the web control, user click insert/add
button, it converts the image(jpeg) file to bytes[] and store it the database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and then
convert this to a image (jpeg) file and used the filepath to display file.

The problem still unable to display image and create image file.

Expand|Select|Wrap|Line Numbers
  1. //Get Image Record Stored and display them
  2. private void GetImageRecord()
  3. {
  4. if (CheckFields(2))
  5. {
  6. string sqlText = "Select * From PictureImages Where ImageName =
  7. @ImgName";
  8. SqlCommand cmd = new SqlCommand(sqlText, conn);
  9. cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  10. "ImageName").Value = txtImgName.Text.Trim();
  11. if (conn.State == ConnectionState.Closed)
  12. conn.Open();
  13.  
  14. SqlDataReader dr = cmd.ExecuteReader();
  15. string filePath = Request.PhysicalApplicationPath +
  16. txtImgName.Text + ".jpg";
  17. FileStream fs = new FileStream(filePath, FileMode.Create,
  18. FileAccess.Write);
  19.  
  20. byte[] storedByte = null;
  21. //byte storedByte = null;
  22. //MemoryStream ms = new MemoryStream(storedByte);
  23. //StreamWriter sw = new StreamWriter(filePath);
  24.  
  25. while (dr.Read())
  26. {
  27. lblImgNo.Text = dr["ImageNo"].ToString();
  28. txtImgDesc.Text = dr["Description"].ToString();
  29. txtImgUrl.Text = dr["ImageUrl"].ToString();
  30. storedByte = (byte[])dr["ImageData"];
  31. //long lRes = dr.GetBytes(5,0,storedByte,0,100);
  32. }
  33. MemoryStream ms = new MemoryStream(storedByte, 0,
  34. storedByte.Length);
  35. System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  36. <<--- Error Occurred Here
  37. System.Drawing.Image otherImg = img.GetThumbnailImage(100, 100,
  38. null, new IntPtr());
  39. //otherImg.Save(ms, img.RawFormat);
  40. otherImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  41.  
  42. //byte[] byteImg = new byte[ms.Length];
  43. //ms.Position = 0;
  44. //ms.Read(byteImg, 0, byteImg.Length);
  45.  
  46. //System.Drawing.Image img = System.Drawing.Image.FromStream(new
  47. MemoryStream(storedByte, 0, storedByte.Length));
  48. //img.Save(filePath);
  49.  
  50. //fs.Write(storedByte, 0, storedByte.Length);
  51. //fs.Flush();
  52. //fs.Close();
  53. if (File.Exists(filePath))
  54. PicImg2.ImageUrl = filePath;
  55.  
  56. if (conn.State == ConnectionState.Open)
  57. conn.Close();
  58. }
  59. }
  60.  
  61. private void InsertProcess()
  62. {
  63. if (CheckFields(1))
  64. {
  65. FileStream fs = new
  66. FileStream(txtImgFile.Text.Trim(),FileMode.OpenOrCreate,FileAccess.Read);
  67.  
  68. byte[] imgByte = new byte[fs.Length];
  69. fs.Read(imgByte,0,(int)fs.Length);
  70. fs.Close();
  71. //Testing storing image as binary in database
  72.  
  73. string sqlText = "Insert PictureImages
  74. Values(@ImgName,@Desc,@Url,@Img)";
  75. SqlCommand cmd = new SqlCommand(sqlText,conn);
  76. cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  77. "ImageName").Value = txtImgName.Text.Trim();
  78. cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  79. "Description").Value = txtImgDesc.Text.Trim();
  80. cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  81. "ImageUrl").Value = txtImgUrl.Text.Trim();
  82. cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  83. "ImageData").Value = imgByte;
  84. if (conn.State == ConnectionState.Closed)
  85. conn.Open();
  86.  
  87. int iRes = cmd.ExecuteNonQuery();
  88.  
  89. if (conn.State == ConnectionState.Open)
  90. conn.Close();
  91.  
  92. if (iRes 0)
  93. lblMessage.Text = "Successfully Added New Record.";
  94. else
  95. lblMessage.Text = "Failed to add new record.";
  96. }
  97. }
  98.  
  99. private void InsertProcess2()
  100. {
  101. if (CheckFields(0))
  102. {
  103. HttpPostedFile imgFile = txtFile1.PostedFile;
  104. int imgLen = imgFile.ContentLength;
  105. string imgType = imgFile.ContentType;
  106. byte[] imgByte = new byte[imgLen];
  107. imgFile.InputStream.Read(imgByte, 0, imgLen);
  108.  
  109. //Testing storing image as binary in database
  110.  
  111. string sqlText = "Insert PictureImages
  112. Values(@ImgName,@Desc,@Url,@Img)";
  113. SqlCommand cmd = new SqlCommand(sqlText, conn);
  114. cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  115. "ImageName").Value = txtImgName.Text.Trim();
  116. cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  117. "Description").Value = txtImgDesc.Text.Trim();
  118. cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  119. "ImageUrl").Value = txtImgUrl.Text.Trim();
  120. cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  121. "ImageData").Value = imgByte;
  122. if (conn.State == ConnectionState.Closed)
  123. conn.Open();
  124.  
  125. int iRes = cmd.ExecuteNonQuery();
  126.  
  127. if (conn.State == ConnectionState.Open)
  128. conn.Close();
  129.  
  130. if (iRes 0)
  131. lblMessage.Text = "Successfully Added New Record.";
  132. else
  133. lblMessage.Text = "Failed to add new record.";
  134. }
  135. }
  136.  
Anyone?

Thanks in Advanced...

den2005
--
MCP Year 2005, Philippines
Aug 2 '06 #1
3 5197

Hi Den,

just wanted to know why you store the images as such in the database.

I believe, it is recommended to save images in file system and store the
filepaths in database rather than storing the entire image in database.

Augustin

"den 2005" wrote:
Hi everyone,

Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).

The process after being displayed in the web control, user click insert/add
button, it converts the image(jpeg) file to bytes[] and store it the database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and then
convert this to a image (jpeg) file and used the filepath to display file.

The problem still unable to display image and create image file.

Expand|Select|Wrap|Line Numbers
  1. //Get Image Record Stored and display them
  2.     private void GetImageRecord()
  3.     {
  4.         if (CheckFields(2))
  5.         {
  6.             string sqlText = "Select * From PictureImages Where ImageName =
  7. @ImgName";
  8.             SqlCommand cmd = new SqlCommand(sqlText, conn);
  9.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  10. "ImageName").Value = txtImgName.Text.Trim();
  11.             if (conn.State == ConnectionState.Closed)
  12.                 conn.Open();
  13.             SqlDataReader dr = cmd.ExecuteReader();
  14.             string filePath = Request.PhysicalApplicationPath +
  15. txtImgName.Text + ".jpg";
  16.             FileStream fs = new FileStream(filePath, FileMode.Create,
  17. FileAccess.Write);
  18.             byte[] storedByte = null;
  19.             //byte storedByte = null;
  20.             //MemoryStream ms = new MemoryStream(storedByte);
  21.             //StreamWriter sw = new StreamWriter(filePath);
  22.             while (dr.Read())
  23.             {
  24.                 lblImgNo.Text = dr["ImageNo"].ToString();
  25.                 txtImgDesc.Text = dr["Description"].ToString();
  26.                 txtImgUrl.Text = dr["ImageUrl"].ToString();
  27.                 storedByte = (byte[])dr["ImageData"];
  28.                 //long lRes = dr.GetBytes(5,0,storedByte,0,100);
  29.             }
  30.             MemoryStream ms = new MemoryStream(storedByte, 0,
  31. storedByte.Length);
  32.             System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  33. <<--- Error Occurred Here
  34.             System.Drawing.Image otherImg = img.GetThumbnailImage(100, 100,
  35. null, new IntPtr());
  36.             //otherImg.Save(ms, img.RawFormat);
  37.             otherImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  38.             //byte[] byteImg = new byte[ms.Length];
  39.             //ms.Position = 0;
  40.             //ms.Read(byteImg, 0, byteImg.Length);
  41.             //System.Drawing.Image img = System.Drawing.Image.FromStream(new
  42. MemoryStream(storedByte, 0, storedByte.Length));
  43.             //img.Save(filePath);
  44.             //fs.Write(storedByte, 0, storedByte.Length);
  45.             //fs.Flush();
  46.             //fs.Close();
  47.             if (File.Exists(filePath))
  48.                 PicImg2.ImageUrl = filePath;
  49.             if (conn.State == ConnectionState.Open)
  50.                 conn.Close();
  51.         }
  52.     }
  53. private void InsertProcess()
  54.     {
  55.         if (CheckFields(1))
  56.         {
  57.             FileStream fs = new
  58. FileStream(txtImgFile.Text.Trim(),FileMode.OpenOrCreate,FileAccess.Read);
  59.             byte[] imgByte = new byte[fs.Length];
  60.             fs.Read(imgByte,0,(int)fs.Length);
  61.             fs.Close();
  62.             //Testing storing image as binary in database
  63.             string sqlText = "Insert PictureImages
  64. Values(@ImgName,@Desc,@Url,@Img)";
  65.             SqlCommand cmd = new SqlCommand(sqlText,conn);
  66.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  67. "ImageName").Value = txtImgName.Text.Trim();
  68.             cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  69. "Description").Value = txtImgDesc.Text.Trim();
  70.             cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  71. "ImageUrl").Value = txtImgUrl.Text.Trim();
  72.             cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  73. "ImageData").Value = imgByte;
  74.             if (conn.State == ConnectionState.Closed)
  75.                 conn.Open();
  76.             int iRes = cmd.ExecuteNonQuery();
  77.             if (conn.State == ConnectionState.Open)
  78.                 conn.Close();
  79.             if (iRes 0)
  80.                 lblMessage.Text = "Successfully Added New Record.";
  81.             else
  82.                 lblMessage.Text = "Failed to add new record.";
  83.         }
  84.     }
  85.     private void InsertProcess2()
  86.     {
  87.         if (CheckFields(0))
  88.         {
  89.             HttpPostedFile imgFile = txtFile1.PostedFile;
  90.             int imgLen = imgFile.ContentLength;
  91.             string imgType = imgFile.ContentType;
  92.             byte[] imgByte = new byte[imgLen];
  93.             imgFile.InputStream.Read(imgByte, 0, imgLen);
  94.             //Testing storing image as binary in database
  95.             string sqlText = "Insert PictureImages
  96. Values(@ImgName,@Desc,@Url,@Img)";
  97.             SqlCommand cmd = new SqlCommand(sqlText, conn);
  98.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  99. "ImageName").Value = txtImgName.Text.Trim();
  100.             cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  101. "Description").Value = txtImgDesc.Text.Trim();
  102.             cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  103. "ImageUrl").Value = txtImgUrl.Text.Trim();
  104.             cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  105. "ImageData").Value = imgByte;
  106.             if (conn.State == ConnectionState.Closed)
  107.                 conn.Open();
  108.             int iRes = cmd.ExecuteNonQuery();
  109.             if (conn.State == ConnectionState.Open)
  110.                 conn.Close();
  111.             if (iRes 0)
  112.                 lblMessage.Text = "Successfully Added New Record.";
  113.             else
  114.                 lblMessage.Text = "Failed to add new record.";
  115.         }
  116.     }
  117.  

Anyone?

Thanks in Advanced...

den2005
--
MCP Year 2005, Philippines
Aug 2 '06 #2
This article should be of help..

http://www.netomatix.com/development...splayBlob.aspx

"den 2005" <de*****@discussions.microsoft.comwrote in message
news:F1**********************************@microsof t.com...
Hi everyone,

Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file,
then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).

The process after being displayed in the web control, user click
insert/add
button, it converts the image(jpeg) file to bytes[] and store it the
database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and
then
convert this to a image (jpeg) file and used the filepath to display file.

The problem still unable to display image and create image file.

Expand|Select|Wrap|Line Numbers
  1. //Get Image Record Stored and display them
  2.    private void GetImageRecord()
  3.    {
  4.        if (CheckFields(2))
  5.        {
  6.            string sqlText = "Select * From PictureImages Where ImageName =
  7. @ImgName";
  8.            SqlCommand cmd = new SqlCommand(sqlText, conn);
  9.            cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  10. "ImageName").Value = txtImgName.Text.Trim();
  11.            if (conn.State == ConnectionState.Closed)
  12.                conn.Open();
  13.            SqlDataReader dr = cmd.ExecuteReader();
  14.            string filePath = Request.PhysicalApplicationPath +
  15. txtImgName.Text + ".jpg";
  16.            FileStream fs = new FileStream(filePath, FileMode.Create,
  17. FileAccess.Write);
  18.            byte[] storedByte = null;
  19.            //byte storedByte = null;
  20.            //MemoryStream ms = new MemoryStream(storedByte);
  21.            //StreamWriter sw = new StreamWriter(filePath);
  22.            while (dr.Read())
  23.            {
  24.                lblImgNo.Text = dr["ImageNo"].ToString();
  25.                txtImgDesc.Text = dr["Description"].ToString();
  26.                txtImgUrl.Text = dr["ImageUrl"].ToString();
  27.                storedByte = (byte[])dr["ImageData"];
  28.                //long lRes = dr.GetBytes(5,0,storedByte,0,100);
  29.            }
  30.            MemoryStream ms = new MemoryStream(storedByte, 0,
  31. storedByte.Length);
  32.            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  33. <<--- Error Occurred Here
  34.            System.Drawing.Image otherImg = img.GetThumbnailImage(100, 100,
  35. null, new IntPtr());
  36.            //otherImg.Save(ms, img.RawFormat);
  37.            otherImg.Save(filePath,
  38. System.Drawing.Imaging.ImageFormat.Jpeg);
  39.            //byte[] byteImg = new byte[ms.Length];
  40.            //ms.Position = 0;
  41.            //ms.Read(byteImg, 0, byteImg.Length);
  42.            //System.Drawing.Image img =
  43. System.Drawing.Image.FromStream(new
  44. MemoryStream(storedByte, 0, storedByte.Length));
  45.            //img.Save(filePath);
  46.            //fs.Write(storedByte, 0, storedByte.Length);
  47.            //fs.Flush();
  48.            //fs.Close();
  49.            if (File.Exists(filePath))
  50.                PicImg2.ImageUrl = filePath;
  51.            if (conn.State == ConnectionState.Open)
  52.                conn.Close();
  53.        }
  54.    }
  55. private void InsertProcess()
  56.    {
  57.        if (CheckFields(1))
  58.        {
  59.            FileStream fs = new
  60. FileStream(txtImgFile.Text.Trim(),FileMode.OpenOrCreate,FileAccess.Read);
  61.            byte[] imgByte = new byte[fs.Length];
  62.            fs.Read(imgByte,0,(int)fs.Length);
  63.            fs.Close();
  64.            //Testing storing image as binary in database
  65.            string sqlText = "Insert PictureImages
  66. Values(@ImgName,@Desc,@Url,@Img)";
  67.            SqlCommand cmd = new SqlCommand(sqlText,conn);
  68.            cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  69. "ImageName").Value = txtImgName.Text.Trim();
  70.            cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  71. "Description").Value = txtImgDesc.Text.Trim();
  72.            cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  73. "ImageUrl").Value = txtImgUrl.Text.Trim();
  74.            cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  75. "ImageData").Value = imgByte;
  76.            if (conn.State == ConnectionState.Closed)
  77.                conn.Open();
  78.            int iRes = cmd.ExecuteNonQuery();
  79.            if (conn.State == ConnectionState.Open)
  80.                conn.Close();
  81.            if (iRes 0)
  82.                lblMessage.Text = "Successfully Added New Record.";
  83.            else
  84.                lblMessage.Text = "Failed to add new record.";
  85.        }
  86.    }
  87.    private void InsertProcess2()
  88.    {
  89.        if (CheckFields(0))
  90.        {
  91.            HttpPostedFile imgFile = txtFile1.PostedFile;
  92.            int imgLen = imgFile.ContentLength;
  93.            string imgType = imgFile.ContentType;
  94.            byte[] imgByte = new byte[imgLen];
  95.            imgFile.InputStream.Read(imgByte, 0, imgLen);
  96.            //Testing storing image as binary in database
  97.            string sqlText = "Insert PictureImages
  98. Values(@ImgName,@Desc,@Url,@Img)";
  99.            SqlCommand cmd = new SqlCommand(sqlText, conn);
  100.            cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  101. "ImageName").Value = txtImgName.Text.Trim();
  102.            cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  103. "Description").Value = txtImgDesc.Text.Trim();
  104.            cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  105. "ImageUrl").Value = txtImgUrl.Text.Trim();
  106.            cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  107. "ImageData").Value = imgByte;
  108.            if (conn.State == ConnectionState.Closed)
  109.                conn.Open();
  110.            int iRes = cmd.ExecuteNonQuery();
  111.            if (conn.State == ConnectionState.Open)
  112.                conn.Close();
  113.            if (iRes 0)
  114.                lblMessage.Text = "Successfully Added New Record.";
  115.            else
  116.                lblMessage.Text = "Failed to add new record.";
  117.        }
  118.    }
  119.  

Anyone?

Thanks in Advanced...

den2005
--
MCP Year 2005, Philippines

Aug 2 '06 #3
Augustin,
That's the requirement what I am doing right now.

Winista,
Thanks for the link, but I am seen this approach, I am looking for
approach that converts stored sql binary/image datatype data back to an image
(jpeg) file.

dennis
--
MCP Year 2005, Philippines
"Winista" wrote:
This article should be of help..

http://www.netomatix.com/development...splayBlob.aspx

"den 2005" <de*****@discussions.microsoft.comwrote in message
news:F1**********************************@microsof t.com...
Hi everyone,

Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file,
then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).

The process after being displayed in the web control, user click
insert/add
button, it converts the image(jpeg) file to bytes[] and store it the
database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and
then
convert this to a image (jpeg) file and used the filepath to display file.

The problem still unable to display image and create image file.

Expand|Select|Wrap|Line Numbers
  1.  //Get Image Record Stored and display them
  2.     private void GetImageRecord()
  3.     {
  4.         if (CheckFields(2))
  5.         {
  6.             string sqlText = "Select * From PictureImages Where ImageName =
  7.  @ImgName";
  8.             SqlCommand cmd = new SqlCommand(sqlText, conn);
  9.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  10.  "ImageName").Value = txtImgName.Text.Trim();
  11.             if (conn.State == ConnectionState.Closed)
  12.                 conn.Open();
  13.  
  14.             SqlDataReader dr = cmd.ExecuteReader();
  15.             string filePath = Request.PhysicalApplicationPath +
  16.  txtImgName.Text + ".jpg";
  17.             FileStream fs = new FileStream(filePath, FileMode.Create,
  18.  FileAccess.Write);
  19.  
  20.             byte[] storedByte = null;
  21.             //byte storedByte = null;
  22.             //MemoryStream ms = new MemoryStream(storedByte);
  23.             //StreamWriter sw = new StreamWriter(filePath);
  24.  
  25.             while (dr.Read())
  26.             {
  27.                 lblImgNo.Text = dr["ImageNo"].ToString();
  28.                 txtImgDesc.Text = dr["Description"].ToString();
  29.                 txtImgUrl.Text = dr["ImageUrl"].ToString();
  30.                 storedByte = (byte[])dr["ImageData"];
  31.                 //long lRes = dr.GetBytes(5,0,storedByte,0,100);
  32.             }
  33.             MemoryStream ms = new MemoryStream(storedByte, 0,
  34.  storedByte.Length);
  35.             System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  36.  <<--- Error Occurred Here
  37.             System.Drawing.Image otherImg = img.GetThumbnailImage(100, 100,
  38.  null, new IntPtr());
  39.             //otherImg.Save(ms, img.RawFormat);
  40.             otherImg.Save(filePath,
  41.  System.Drawing.Imaging.ImageFormat.Jpeg);
  42.  
  43.             //byte[] byteImg = new byte[ms.Length];
  44.             //ms.Position = 0;
  45.             //ms.Read(byteImg, 0, byteImg.Length);
  46.  
  47.             //System.Drawing.Image img =
  48.  System.Drawing.Image.FromStream(new
  49.  MemoryStream(storedByte, 0, storedByte.Length));
  50.             //img.Save(filePath);
  51.  
  52.             //fs.Write(storedByte, 0, storedByte.Length);
  53.             //fs.Flush();
  54.             //fs.Close();
  55.             if (File.Exists(filePath))
  56.                 PicImg2.ImageUrl = filePath;
  57.  
  58.             if (conn.State == ConnectionState.Open)
  59.                 conn.Close();
  60.         }
  61.     }
  62.  
  63.  private void InsertProcess()
  64.     {
  65.         if (CheckFields(1))
  66.         {
  67.             FileStream fs = new
  68.  FileStream(txtImgFile.Text.Trim(),FileMode.OpenOrCreate,FileAccess.Read);
  69.  
  70.             byte[] imgByte = new byte[fs.Length];
  71.             fs.Read(imgByte,0,(int)fs.Length);
  72.             fs.Close();
  73.             //Testing storing image as binary in database
  74.  
  75.             string sqlText = "Insert PictureImages
  76.  Values(@ImgName,@Desc,@Url,@Img)";
  77.             SqlCommand cmd = new SqlCommand(sqlText,conn);
  78.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  79.  "ImageName").Value = txtImgName.Text.Trim();
  80.             cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  81.  "Description").Value = txtImgDesc.Text.Trim();
  82.             cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  83.  "ImageUrl").Value = txtImgUrl.Text.Trim();
  84.             cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  85.  "ImageData").Value = imgByte;
  86.             if (conn.State == ConnectionState.Closed)
  87.                 conn.Open();
  88.  
  89.             int iRes = cmd.ExecuteNonQuery();
  90.  
  91.             if (conn.State == ConnectionState.Open)
  92.                 conn.Close();
  93.  
  94.             if (iRes 0)
  95.                 lblMessage.Text = "Successfully Added New Record.";
  96.             else
  97.                 lblMessage.Text = "Failed to add new record.";
  98.         }
  99.     }
  100.  
  101.     private void InsertProcess2()
  102.     {
  103.         if (CheckFields(0))
  104.         {
  105.             HttpPostedFile imgFile = txtFile1.PostedFile;
  106.             int imgLen = imgFile.ContentLength;
  107.             string imgType = imgFile.ContentType;
  108.             byte[] imgByte = new byte[imgLen];
  109.             imgFile.InputStream.Read(imgByte, 0, imgLen);
  110.  
  111.             //Testing storing image as binary in database
  112.  
  113.             string sqlText = "Insert PictureImages
  114.  Values(@ImgName,@Desc,@Url,@Img)";
  115.             SqlCommand cmd = new SqlCommand(sqlText, conn);
  116.             cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
  117.  "ImageName").Value = txtImgName.Text.Trim();
  118.             cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
  119.  "Description").Value = txtImgDesc.Text.Trim();
  120.             cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
  121.  "ImageUrl").Value = txtImgUrl.Text.Trim();
  122.             cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
  123.  "ImageData").Value = imgByte;
  124.             if (conn.State == ConnectionState.Closed)
  125.                 conn.Open();
  126.  
  127.             int iRes = cmd.ExecuteNonQuery();
  128.  
  129.             if (conn.State == ConnectionState.Open)
  130.                 conn.Close();
  131.  
  132.             if (iRes 0)
  133.                 lblMessage.Text = "Successfully Added New Record.";
  134.             else
  135.                 lblMessage.Text = "Failed to add new record.";
  136.         }
  137.     }
  138.  
Anyone?

Thanks in Advanced...

den2005
--
MCP Year 2005, Philippines


Aug 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Satish | last post by:
Dear all, Iam unable to laod images at runtime from the file folder / database through C# code with asp. net and Crystal reports All the solutions given are through RDC COM Component.. but no...
3
by: hussein | last post by:
'm not bale to run my projec on the 2000 server, he error was: error while trying to run project: unable to start debugging on the web server.erver side error occurred on sending debug HTTP request...
1
by: Eric Keung | last post by:
Hi all, my case is I want to get an image from access database and I just know it's "OLE object" field type at access I also don't know how to insert it into access here is my code and it just...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
0
by: Nachi | last post by:
I have installed SQL Server 2000 with VS 2005 - did not install SqlExpress\Sql 2005. I have a remote Sql Server Instance locally. The normal application connectes to this Sql Server. When I run...
7
by: eholz1 | last post by:
Hello Group, Perhaps you can help me. I have a mysql db, that holds images. Images are encoded using base64_decode/encode, etc. Image data seems fine. I have a view.php page that is supposed...
1
by: Erik Lautier | last post by:
Hello, I'm using a multipart/form-data upload to put an image into SQL Server, and it appears to be working, but I'm unable to display the image. All I'm getting is the text of the URL we're on in...
3
by: kulkanuja | last post by:
i am using image control in asp.net in c# now i want to display binary(format) image from database to image control. how do i do iot pls help.
1
by: swethak | last post by:
Hi, I write the code to display images.But it will not display image.And also gives the error like that error : Notice: Undefined index: gim in F:\Facebook\pic_up.php on line 59 plz tell...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.