2012-02-13 20 views
0

私は、私の部門の各部門の各従業員の訓練記録を示すWebベースのトレーニングマトリックスを開発しました。 1つのこと以外はすべてうまくいく。管理者が一部の従業員の訓練記録を更新し、更新ボタンをクリックすると直ちに最新の更新が表示されますが、再度更新すると以前入力したデータは表示されません。彼らは姿を消してしまい、理由は分かりません。私はデータベースをチェックし、データもなく、データベースに触れた体がない。それは本当に奇妙です。DeleteとInsertコマンドを同時に使用してテーブルを更新しますか?

アップデート機能を開発する代わりに、(削除と挿入)のように更新機能を開発しました。私は今問題が削除機能にあると思う。特定の従業員が以下に示すように全員ではなく特定の従業員になるように変更する必要があります。

コードビハインド(C#コード):

protected void Page_Load(object sender, EventArgs e) 
{ 

    DataView dv2 = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
    foreach (DataRowView group in dv2) 
    { 
     SqlDataSource2.SelectParameters[0].DefaultValue = group[0].ToString(); 
     //create a new HtmlTable object 
     HtmlTable table = new HtmlTable(); 

     DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty); 
     int columns = dv.Table.Columns.Count; 
     int rows = dv.Count; 

     //table's formating-related properties 
     table.Border = 2; 
     table.CellPadding = 3; 
     table.CellSpacing = 3; 
     table.Width = "900px"; 

     //to get the css style 
     table.Attributes["class"] = "uGrid"; 

     //create a new HtmlTableRow and HtmlTableCell objects 
     HtmlTableRow row; 
     HtmlTableRow header = new HtmlTableRow(); 
     HtmlTableCell cell; 


     //for adding the headers to the table 
     foreach (DataColumn column in dv.Table.Columns) 
     { 
      HtmlTableCell headerCell = new HtmlTableCell("th"); 
      headerCell.InnerText = column.Caption; 
      header.Cells.Add(headerCell); 
     } 
     table.Rows.Add(header); 

     //loop for adding rows to the table 
     foreach (DataRowView datarow in dv) 
     { 
      row = new HtmlTableRow(); 
      //row.BgColor = "yellow"; 


      //loop for adding cells 
      for (int j = 0; j < columns; j++) 
      { 
       cell = new HtmlTableCell(); 
       if (j < 4) 
       { 
        cell.InnerText = datarow[j].ToString(); 
       } 
       else 
       { 

        CheckBox checkbox = new CheckBox(); 

        int checkBoxColumns = dv.Table.Columns.Count - 5; 
        string fieldvalue = datarow[j].ToString(); 
        string yes = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[1]; 
        string courseid = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0]; 
        checkbox.ID = row.Cells[3].InnerText + "," + courseid.Trim(); 
        checkbox.Checked = yes.Equals("Yes"); 
        cell.Controls.Add(checkbox); 

       } 

       //add the cell to the current row 
       row.Cells.Add(cell); 
      } 

      //add the row to the table 
      table.Rows.Add(row); 
     } 

     //add the table to the page 
     PlaceHolder1.Controls.Add(table); 

    } 
} 


protected void updateButton_Click(object sender, EventArgs e) 
{ 
    string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True"; 
    string deleteCommand = "DELETE FROM employee_courses where [email protected]"; 
    string insertCommand = "INSERT INTO employee_courses (employeeId, CourseID) values(@employeeId, @CourseID)"; 

    using (SqlConnection conn = new SqlConnection(connString)) 
    { 
     conn.Open(); 
     using (SqlCommand cmd = new SqlCommand(deleteCommand, conn)) 
     { 
      cmd.ExecuteNonQuery(); 
     } 
    } 

    foreach (Control ctrl in PlaceHolder1.Controls) 
    { 
     if (ctrl is HtmlTable) 
     { 
      HtmlTable table = (HtmlTable)ctrl; 
      foreach (HtmlTableRow row in table.Rows) 
      { 
       foreach (HtmlTableCell cell in row.Cells) 
       { 
        foreach (Control c in cell.Controls) 
        { 
         if (c is CheckBox) 
         { 
          CheckBox checkbox = (CheckBox)c; 
          if (checkbox.Checked) 
          { 
           string fieldvalue = checkbox.ID; 
           string employeeID = fieldvalue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0]; 
           string courseID = fieldvalue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[1]; 

           using (SqlConnection conn = new SqlConnection(connString)) 
           { 
            conn.Open(); 
            using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) 
            { 
             //Now the insert 
             cmd.CommandText = insertCommand; 
             cmd.Parameters.Clear(); //need this because still has params from del comm 
             cmd.Parameters.AddWithValue("@employeeId", employeeID); 
             cmd.Parameters.AddWithValue("@CourseID", courseID); 
             cmd.ExecuteNonQuery(); 
            } 
           } 

          } 

         } 
        } 
       } 
      } 

     } 

    } 

    Response.Redirect("KPIReport.aspx"); 
} 

問題はここにある:

protected void updateButton_Click(object sender, EventArgs e) 
    { 
     string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True"; 
     string deleteCommand = "DELETE FROM employee_courses where [email protected]"; 
     string insertCommand = "INSERT INTO employee_courses (employeeId, CourseID) values(@employeeId, @CourseID)"; 

     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(deleteCommand, conn)) 
      { 
       cmd.ExecuteNonQuery(); 
      } 
     } 

     foreach (Control ctrl in PlaceHolder1.Controls) 
     { 
      if (ctrl is HtmlTable) 
      { 
       HtmlTable table = (HtmlTable)ctrl; 
       foreach (HtmlTableRow row in table.Rows) 
       { 
        foreach (HtmlTableCell cell in row.Cells) 
        { 
         foreach (Control c in cell.Controls) 
         { 
          if (c is CheckBox) 
          { 
           CheckBox checkbox = (CheckBox)c; 
           if (checkbox.Checked) 
           { 
            string fieldvalue = checkbox.ID; 
            string employeeID = fieldvalue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0]; 
            string courseID = fieldvalue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[1]; 

            using (SqlConnection conn = new SqlConnection(connString)) 
            { 
             conn.Open(); 
             using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) 
             { 
              //Now the insert 
              cmd.CommandText = insertCommand; 
              cmd.Parameters.Clear(); //need this because still has params from del comm 
              cmd.Parameters.AddWithValue("@employeeId", employeeID); 
              cmd.Parameters.AddWithValue("@CourseID", courseID); 
              cmd.ExecuteNonQuery(); 
             } 
            } 

           } 

          } 
         } 
        } 
       } 

      } 

     } 

     Response.Redirect("KPIReport.aspx"); 
+0

なぜ変更を更新できませんか? 2回の往復取引を保存します。 1つは削除用、もう1つは挿入用 –

+0

更新できます。問題は今、なぜ私が2回目にマトリックスを更新すると、初めての更新が消えてしまうのですか? –

+0

あなたの更新コードを表示 –

答えて

1

では、次の行のようにdeleteコマンドのため@EmployeeIDを設定していないようだ。

protected void updateButton_Click(object sender, EventArgs e) 
{ 
    //COMMAND DEFINED 
    string deleteCommand = "DELETE FROM employee_courses where [email protected]"; 

    using (SqlConnection conn = new SqlConnection(connString)) 
    { 
     conn.Open(); 

     //COMMAND CREATED 
     using (SqlCommand cmd = new SqlCommand(deleteCommand, conn)) 
     { 
      //COMMAND EXECUTED, BUT @employeeID NOT SET ANYWHERE 
      cmd.ExecuteNonQuery(); 
     } 
    } 

以下を試してください:

//TO KEEP TRACK OF EMPLOYEE IDS FOR WHICH WE HAVE ALREADY EXECUTED 
//DELETE STATEMENT 
IList<string> _deleted=new List<string>(); 

foreach (Control ctrl in PlaceHolder1.Controls) 
{ 
    ........... 
    ......... 

    if (c is CheckBox) 
    { 
     CheckBox checkbox = (CheckBox)c; 
     if (checkbox.Checked) 
     { 
      string fieldvalue = checkbox.ID; 
      string employeeID = fieldvalue.Split(new string[] { "," }, 
            StringSplitOptions.RemoveEmptyEntries)[0]; 
      string courseID = fieldvalue.Split(new string[] { "," }, 
            StringSplitOptions.RemoveEmptyEntries)[1]; 

      if(!_deleted.Contains(employeeID)) 
      { 
       using (SqlConnection conn = new SqlConnection(connString)) 
       { 
        conn.Open(); 
        using (SqlCommand cmd = new SqlCommand(deleteCommand, conn)) 
        { 
         cmd.Pararmeters.AddWithValue("@employeeId", employeeID); 
         cmd.ExecuteNonQuery(); 
         //DON'T EXECUTE DELETE FOR ALL CHECKED ROWS 
         _deleted.Add(employeeID); 
        } 
       } 
      } 

      using (SqlConnection conn = new SqlConnection(connString)) 
      { 
       conn.Open(); 
       using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) 
       { 
        //Now the insert 
        cmd.CommandText = insertCommand; 
        //DON'T NEED TO CLEAR AS YOU HAVE CREATED NEW COMMAND IN 
        //USING STATEMENT 
        //cmd.Parameters.Clear(); 
        cmd.Parameters.AddWithValue("@employeeId", employeeID); 
        Cmd.Parameters.AddWithValue("@CourseID", courseID); 
        cmd.ExecuteNonQuery(); 
       } 
      } 
     } 
     ..................... 
     ..................... 
    } 
    ..................... 
    ..................... 
} 
+0

助けてくれてありがとう、私はあなたのコードを使用したとき、何も更新されず、データベース内のすべての挿入されたデータが削除されました!!!!!!!!なぜ??? –

+0

申し訳ありませんが、それは私のせいでした。すべて今はうまくいく。私はあなたの助けに本当に感謝しています。 –

関連する問題