protected void Page_Load(object sender, EventArgs e) { //表示ラベル #region //追加 Add_Number.Text = STUDENT_ID; Add_Name.Text = STUDENT_NAME; Add_Point.Text = STUDENT_POINT; //更新 UP_Number.Text = STUDENT_ID; UP_Name.Text = STUDENT_NAME; UP_Point.Text = STUDENT_POINT; //削除 Del_Number.Text = STUDENT_ID; #endregion //SQL読み込み SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=ReportDB;Integrated Security=SSPI"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT REPORT_ID,NUMBER,NAME,POINT , iif(RESULT=1,'合格','不合格')AS RESULT FROM Reportmst ORDER BY NUMBER;", con); SqlDataReader dr = cmd.ExecuteReader(); DB_Grid.DataSource = dr; DB_Grid.DataBind(); con.Close(); //ヘッダー文字 DB_Grid.HeaderRow.Cells[2].Text = STUDENT_ID; DB_Grid.HeaderRow.Cells[3].Text = STUDENT_NAME; DB_Grid.HeaderRow.Cells[4].Text = STUDENT_POINT; DB_Grid.HeaderRow.Cells[5].Text = STUDENT_RESULT; }
//更新ボタンクリック protected void Update_Button_Click(object sender, EventArgs e) { select_hash = (Hashtable)ViewState["select_hash"]; try { id = (int)select_hash["ID"]; number = UP_Number_Text.Text; name = UP_Name_Text.Text; //例外処理 #region //出席番号が未入力の場合 if (number == null || number == "") { number = DB_Grid.SelectedRow.Cells[2].Text; } //名前が未入力の場合 if (name == null || name == "") { name = DB_Grid.SelectedRow.Cells[3].Text; } //成績のチェック if (UP_Point_Text.Text != null && UP_Point_Text.Text != "" && int.Parse(UP_Point_Text.Text) <= 100) { point = int.Parse((UP_Point_Text.Text)); } else { point = int.Parse(DB_Grid.SelectedRow.Cells[4].Text); } #endregion //成績判定 #region if (point >= 60) { result = true; } else { result = false; } #endregion //SQL読み込み SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=ReportDB;Integrated Security=SSPI"); con.Open(); SqlCommand cmd = new SqlCommand("UPDATE Reportmst SET NUMBER = @NUMBER, NAME = @NAME, POINT = @POINT, RESULT = @RESULT WHERE REPORT_ID = @REPORT_ID;", con); cmd.Parameters.Add(new SqlParameter("@REPORT_ID", id)); cmd.Parameters.Add(new SqlParameter("@NUMBER", number)); cmd.Parameters.Add(new SqlParameter("@NAME", name)); cmd.Parameters.Add(new SqlParameter("@POINT", point)); cmd.Parameters.Add(new SqlParameter("@RESULT", result)); SqlDataReader dr = cmd.ExecuteReader(); DB_Grid.DataSource = dr; DB_Grid.DataBind(); con.Close(); Response.Redirect(Request.Url.OriginalString); } catch { return; } }