Danh mục

Practical Database Programming With Visual C#.NET- P14

Số trang: 50      Loại file: pdf      Dung lượng: 855.76 KB      Lượt xem: 17      Lượt tải: 0    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 20,000 VND Tải xuống file đầy đủ (50 trang) 0
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu practical database programming with visual c#.net- p14, công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Practical Database Programming With Visual C#.NET- P14 8.5 Develop Web Applications to Update and Delete Data in SQL Server Databases 673 Faculty cmdSelect_Click() protected void cmdSelect_Click(object sender, EventArgs e) { string cmdString = SELECT faculty_id, faculty_name, office, phone, college, title, email FROM Faculty ; cmdString += WHERE faculty_name LIKE @name; SqlCommand sqlCommand = new SqlCommand(); SqlDataReader sqlDataReader;A Application[oldFacultyName] = ComboName.Text; sqlCommand.Connection = (SqlConnection)Application[sqlConnection]; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = cmdString; sqlCommand.Parameters.Add(@name, SqlDbType.Char).Value = ComboName.Text; string strName = ShowFaculty(ComboName.Text); sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows == true) FillFacultyReader(sqlDataReader); else Response.Write(alert(No matched faculty found!)); sqlDataReader.Close(); sqlCommand.Dispose(); }Figure 8.41 Modified Select button’s Click method. Faculty ShowFaculty() ……… if (FacultyImage != No Match) PhotoBox.ImageUrl = FacultyImage; elseA if (((string)Application[FacultyImage] == string.Empty) || (string)Application[FacultyImage] == null) FacultyImage = Default.jpg; else FacultyImage = (string)Application[FacultyImage]; PhotoBox.ImageUrl = FacultyImage; return FacultyImage; }Figure 8.42 Modified codes of the ShowFaculty method. Another modification to this Faculty page is to add one more statement to the ifcondition in the ShowFaculty() method, which is shown in step A in Figure 8.42, to displaya default faculty image if no data insertion action is performed. The new added instruc-tion has been highlighted in bold. The purpose of adding this or condition is to display a default image if no data inser-tion action is performed since this ShowFaculty() method will be used by different dataactions, including data selection, insertion, and updating, as the project runs. Without thisor condition, no faculty image will be displayed if no data insertion occurred, even whendata updating is performed with a default faculty image selected by the user. Now let’sdevelop the codes for the Update button’s Click method.674 Chapter 8 Accessing Data in ASP.NET Faculty cmdUpdate_Click() protected void cmdUpdate_Click(object sender, EventArgs e) {A string cmdString = UPDATE Faculty SET faculty_name = @name, office = @office, phone = @phone, + college = @college, title = @title, email = @email + WHERE (faculty_name LIKE @oldName);B SqlCommand sqlCommand = new SqlCommand(); int intUpdate = 0;C txtID.Text = string.Empty; //clean up the faculty_id textboxD if (txtName.Text != (string)Application[oldFacultyName]) //the faculty name is updated { ComboName.Items.Add(txtName.Text); ComboName.Items.Remove((string)Application[oldFacultyName]); }E sqlCommand.Connection = (SqlConnection)Application[sqlConnection]; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = cmdString;F UpdateParameters(ref sqlCommand);G intUpdate = sqlCommand.ExecuteNonQuery();H sqlCommand.Dispose();I if (intUpdate == 0) Response.Write(alert(The data updating is failed)); }Figure 8.43 Coding for the Update button’s Click method.8.5.3 Develop Codes for Update Button Click MethodOpen this method by double-clicking on the Update button from the Faculty Web formwindow and enter the codes shown in Figure 8.43 into this method. Let’s take a closer look at this piece of code to see how it works. A. An updating query string is declared first with the oldName as the name of the dynamic parameter. This is because when you want to update the faculty name, the original name stored in the combobox control ComboName becomes the old name, and we need to distinguish this old name from the updated name. B. The data component, Command object, used in this method is created here. A local integer variable intUpdate is also created, and it is used as a value holder to keep the returned data from executing the ExecutNonQuery() method later ...

Tài liệu được xem nhiều: