PageRenderTime 498ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETFormViewUpload/Image.aspx.cs

#
C# | 82 lines | 39 code | 9 blank | 34 comment | 4 complexity | feb4d1a994e29244a1928a09b2623b33 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: Image.aspx.cs
  3. * Project: CSASPNETFormViewUpload
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * This page is used to retrieve the image from a SQL Server database and
  7. * display it in the Web page.
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. \***************************************************************************/
  17. #region Using directives
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Web;
  22. using System.Web.UI;
  23. using System.Web.UI.WebControls;
  24. using System.Data.SqlClient;
  25. using System.Data;
  26. using System.Configuration;
  27. #endregion Using directives
  28. namespace CSASPNETFormViewUpload
  29. {
  30. public partial class Image : System.Web.UI.Page
  31. {
  32. protected void Page_Load(object sender, EventArgs e)
  33. {
  34. if (Request.QueryString["PersonID"] != null)
  35. {
  36. // Get the connection string from Web.config.
  37. // When we use Using statement,
  38. // we don't need to explicitly dispose the object in the code,
  39. // the using statement takes care of it.
  40. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
  41. {
  42. // Create a command object.
  43. SqlCommand cmd = new SqlCommand();
  44. // Assign the connection to the command.
  45. cmd.Connection = conn;
  46. // Set the command text
  47. // SQL statement or the name of the stored procedure.
  48. cmd.CommandText = "SELECT Picture FROM Person WHERE PersonID = @PersonID AND Picture IS NOT NULL";
  49. // Set the command type
  50. // CommandType.Text for ordinary SQL statements;
  51. // CommandType.StoredProcedure for stored procedures.
  52. cmd.CommandType = CommandType.Text;
  53. // Append the parameter to the SqlCommand and set value.
  54. cmd.Parameters.Add("@PersonID", SqlDbType.Int).Value = Request.QueryString["PersonID"];
  55. // Open the connection.
  56. conn.Open();
  57. // Convert the returned result to a bytes array.
  58. Byte[] bytes = (byte[])cmd.ExecuteScalar();
  59. if (bytes != null)
  60. {
  61. // Set the HTTP MIME type of the output stream.
  62. Response.ContentType = "image/jpeg";
  63. // Write a string of Binary characters to the HTTP
  64. // output stream.
  65. Response.BinaryWrite(bytes);
  66. // Send all currently buffered output to the client.
  67. Response.End();
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }