473,714 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5238

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*****@discus sions.microsoft .comwrote in message
news:F1******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .comwrote in message
news:F1******** *************** ***********@mic rosoft.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
1360
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 information is available for CR for VS.net this is with File server based image loading using OLE object. Thanks in advance. -satish
3
3047
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 mak sure that the server is running correctly . verify that there are o syntax errors in the web.config... i check the IIS, it is running correctly, also the webconfig so what am i sposed to to do be able to run it
1
2895
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 can display a invalid image try dim strConn as string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath
1
1468
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. Database.cs DatabaseFactory.cs DatabaseProviderFactory.cs DBCommandWrapper.cs
0
2010
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 Asp.Net Configuration from VS 2005 it gives this error. I have removed 'LocalSqlServer' in machine.Config and added in web.config as following.
7
5406
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 to show the image, and an image.php page that accesses the database, retrives the image data, and then (theoretically) prints the decoded data to the page. below is the view.php page code: problem area the img tag src no
1
2350
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 Firefox and a missing image icon in IE! Any ideas why this isn't working? Here's the image upload code: Dim intImageSize As Int64 Dim strImageType As String Dim ImageStream As Stream
3
4830
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
2043
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 that what is the problem in that code <?php
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.