PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 2ms 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
18#region Using directives
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Web;
23using System.Web.UI;
24using System.Web.UI.WebControls;
25using System.Data.SqlClient;
26using System.Data;
27using System.Configuration;
28#endregion Using directives
29
30namespace CSASPNETFormViewUpload
31{
32    public partial class Image : System.Web.UI.Page
33    {
34        protected void Page_Load(object sender, EventArgs e)
35        {
36            if (Request.QueryString["PersonID"] != null)
37            {
38                // Get the connection string from Web.config. 
39                // When we use Using statement, 
40                // we don't need to explicitly dispose the object in the code, 
41                // the using statement takes care of it.
42                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
43                {
44                    // Create a command object.
45                    SqlCommand cmd = new SqlCommand();
46
47                    // Assign the connection to the command.
48                    cmd.Connection = conn;
49
50                    // Set the command text
51                    // SQL statement or the name of the stored procedure. 
52                    cmd.CommandText = "SELECT Picture FROM Person WHERE PersonID = @PersonID AND Picture IS NOT NULL";
53
54                    // Set the command type
55                    // CommandType.Text for ordinary SQL statements; 
56                    // CommandType.StoredProcedure for stored procedures.
57                    cmd.CommandType = CommandType.Text;
58
59                    // Append the parameter to the SqlCommand and set value.
60                    cmd.Parameters.Add("@PersonID", SqlDbType.Int).Value = Request.QueryString["PersonID"];
61
62                    // Open the connection.
63                    conn.Open();
64
65                    // Convert the returned result to a bytes array.
66                    Byte[] bytes = (byte[])cmd.ExecuteScalar();
67
68                    if (bytes != null)
69                    {
70                        // Set the HTTP MIME type of the output stream.
71                        Response.ContentType = "image/jpeg";
72                        // Write a string of Binary characters to the HTTP 
73                        // output stream.
74                        Response.BinaryWrite(bytes);
75                        // Send all currently buffered output to the client. 
76                        Response.End();
77                    }
78                }
79            }
80        }       
81    }
82}