PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 277 lines | 120 code | 47 blank | 110 comment | 3 complexity | 12cc4128fdee0435d5498230cdd65a10 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: Default.aspx.cs
  3. * Project: CSASPNETFormViewUpload
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * This page populates a FromView control with data from a SQL Server
  7. * database and provides UI for data manipulation.
  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.Configuration;
  26. using System.Data;
  27. #endregion Using directives
  28. namespace CSASPNETFormViewUpload
  29. {
  30. public partial class Default : System.Web.UI.Page
  31. {
  32. protected void Page_Load(object sender, EventArgs e)
  33. {
  34. // The Page is accessed for the first time.
  35. if (!IsPostBack)
  36. {
  37. // Enable the FormView paging option and
  38. // specify the PageButton count.
  39. fvPerson.AllowPaging = true;
  40. fvPerson.PagerSettings.PageButtonCount = 15;
  41. // Populate the FormView control.
  42. BindFormView();
  43. }
  44. }
  45. private void BindFormView()
  46. {
  47. // Get the connection string from Web.config.
  48. // When we use Using statement,
  49. // we don't need to explicitly dispose the object in the code,
  50. // the using statement takes care of it.
  51. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
  52. {
  53. // Create a DataSet object.
  54. DataSet dsPerson = new DataSet();
  55. // Create a SELECT query.
  56. string strSelectCmd = "SELECT PersonID,LastName,FirstName FROM Person";
  57. // Create a SqlDataAdapter object
  58. // SqlDataAdapter represents a set of data commands and a
  59. // database connection that are used to fill the DataSet and
  60. // update a SQL Server database.
  61. SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
  62. // Open the connection
  63. conn.Open();
  64. // Fill the DataTable named "Person" in DataSet with the rows
  65. // returned by the query.
  66. da.Fill(dsPerson, "Person");
  67. // Bind the FormView control.
  68. fvPerson.DataSource = dsPerson;
  69. fvPerson.DataBind();
  70. }
  71. }
  72. // FormView.PageIndexChanging Event
  73. protected void fvPerson_PageIndexChanging(object sender, FormViewPageEventArgs e)
  74. {
  75. // Set the index of the new display page.
  76. fvPerson.PageIndex = e.NewPageIndex;
  77. // Rebind the FormView control to show data in the new page.
  78. BindFormView();
  79. }
  80. // FormView.ItemInserting Event
  81. protected void fvPerson_ItemInserting(object sender, FormViewInsertEventArgs e)
  82. {
  83. // Get the connection string from Web.config.
  84. // When we use Using statement,
  85. // we don't need to explicitly dispose the object in the code,
  86. // the using statement takes care of it.
  87. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
  88. {
  89. // Create a command object.
  90. SqlCommand cmd = new SqlCommand();
  91. // Assign the connection to the command.
  92. cmd.Connection = conn;
  93. // Set the command text
  94. // SQL statement or the name of the stored procedure.
  95. cmd.CommandText = "INSERT INTO Person ( LastName, FirstName, Picture ) VALUES ( @LastName, @FirstName, @Picture )";
  96. // Set the command type
  97. // CommandType.Text for ordinary SQL statements;
  98. // CommandType.StoredProcedure for stored procedures.
  99. cmd.CommandType = CommandType.Text;
  100. // Get the first name and last name from the
  101. // InsertItemTemplate of the FormView control.
  102. string strLastName = ((TextBox)fvPerson.Row.FindControl("tbLastName")).Text;
  103. string strFirstName = ((TextBox)fvPerson.Row.FindControl("tbFirstName")).Text;
  104. // Append the parameters to the SqlCommand and set values.
  105. cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = strLastName;
  106. cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = strFirstName;
  107. FileUpload uploadPicture = (FileUpload)fvPerson.FindControl("uploadPicture");
  108. if (uploadPicture.HasFile)
  109. {
  110. // Append the Picture parameter to the SqlCommand.
  111. // If a picture is specified, set the parameter with
  112. // the value of bytes in the specified picture file.
  113. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
  114. }
  115. else
  116. {
  117. // Append the Picture parameter to the SqlCommand.
  118. // If no picture is specified, set the parameter's
  119. // value to NULL.
  120. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
  121. }
  122. // Open the connection.
  123. conn.Open();
  124. // Execute the command.
  125. cmd.ExecuteNonQuery();
  126. }
  127. // Switch FormView control to the ReadOnly display mode.
  128. fvPerson.ChangeMode(FormViewMode.ReadOnly);
  129. // Rebind the FormView control to show data after inserting.
  130. BindFormView();
  131. }
  132. // FormView.ItemUpdating Event
  133. protected void fvPerson_ItemUpdating(object sender, FormViewUpdateEventArgs e)
  134. {
  135. // Get the connection string from Web.config.
  136. // When we use Using statement,
  137. // we don't need to explicitly dispose the object in the code,
  138. // the using statement takes care of it.
  139. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
  140. {
  141. // Create a command object.
  142. SqlCommand cmd = new SqlCommand();
  143. // Assign the connection to the command.
  144. cmd.Connection = conn;
  145. // Set the command text
  146. // SQL statement or the name of the stored procedure.
  147. cmd.CommandText = "UPDATE Person SET LastName = @LastName, FirstName = @FirstName, Picture = ISNULL(@Picture,Picture) WHERE PersonID = @PersonID";
  148. // Set the command type
  149. // CommandType.Text for ordinary SQL statements;
  150. // CommandType.StoredProcedure for stored procedures.
  151. cmd.CommandType = CommandType.Text;
  152. // Get the person ID, first name and last name from the
  153. // EditItemTemplate of the FormView control.
  154. string strPersonID = ((Label)fvPerson.Row.FindControl("lblPersonID")).Text;
  155. string strLastName = ((TextBox)fvPerson.Row.FindControl("tbLastName")).Text;
  156. string strFirstName = ((TextBox)fvPerson.Row.FindControl("tbFirstName")).Text;
  157. // Append the parameters to the SqlCommand and set values.
  158. cmd.Parameters.Add("@PersonID", SqlDbType.Int).Value = strPersonID;
  159. cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = strLastName;
  160. cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = strFirstName;
  161. // Find the FileUpload control in the EditItemTemplate of
  162. // the FormView control.
  163. FileUpload uploadPicture = (FileUpload)fvPerson.FindControl("uploadPicture");
  164. if (uploadPicture.HasFile)
  165. {
  166. // Append the Picture parameter to the SqlCommand.
  167. // If a picture is specified, set the parameter with
  168. // the value of bytes in the specified picture file.
  169. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
  170. }
  171. else
  172. {
  173. // Append the Picture parameter to the SqlCommand.
  174. // If no picture is specified, set the parameter's
  175. // value to NULL.
  176. cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
  177. }
  178. // Open the connection.
  179. conn.Open();
  180. // Execute the command.
  181. cmd.ExecuteNonQuery();
  182. }
  183. // Switch FormView control to the ReadOnly display mode.
  184. fvPerson.ChangeMode(FormViewMode.ReadOnly);
  185. // Rebind the FormView control to show data after updating.
  186. BindFormView();
  187. }
  188. // FormView.ItemDeleting Event
  189. protected void fvPerson_ItemDeleting(object sender, FormViewDeleteEventArgs e)
  190. {
  191. // Get the connection string from Web.config.
  192. // When we use Using statement,
  193. // we don't need to explicitly dispose the object in the code,
  194. // the using statement takes care of it.
  195. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
  196. {
  197. // Create a command object.
  198. SqlCommand cmd = new SqlCommand();
  199. // Assign the connection to the command.
  200. cmd.Connection = conn;
  201. // Set the command text
  202. // SQL statement or the name of the stored procedure.
  203. cmd.CommandText = "DELETE FROM Person WHERE PersonID = @PersonID";
  204. // Set the command type
  205. // CommandType.Text for ordinary SQL statements;
  206. // CommandType.StoredProcedure for stored procedures.
  207. cmd.CommandType = CommandType.Text;
  208. // Get the PersonID from the ItemTemplate of the FormView
  209. // control.
  210. string strPersonID = ((Label)fvPerson.Row.FindControl("lblPersonID")).Text;
  211. // Append the parameter to the SqlCommand and set value.
  212. cmd.Parameters.Add("@PersonID", SqlDbType.Int).Value = strPersonID;
  213. // Open the connection.
  214. conn.Open();
  215. // Execute the command.
  216. cmd.ExecuteNonQuery();
  217. }
  218. // Rebind the FormView control to show data after deleting.
  219. BindFormView();
  220. }
  221. // FormView.ModeChanging Event
  222. protected void fvPerson_ModeChanging(object sender, FormViewModeEventArgs e)
  223. {
  224. // Switch FormView control to the new mode
  225. fvPerson.ChangeMode(e.NewMode);
  226. // Rebind the FormView control to show data in new mode.
  227. BindFormView();
  228. }
  229. }
  230. }