By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Unable to Display Image from database at runtime

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.