Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a web-based training matrix that shows the training record for each employee in each division in my department in the company. Everything works well except one thing; when the Admin updates the training record for some employees and he clicks on the Update button, he will see his latest updates immediately, but after a period of time when he updates the matrix again, he will not see his previous entered data. They will be disappeared and I don't know why. I checked the database and there was no data, too and there is no body touched the database. It is really strange.

I developed the Updating functionality to be like (delete and insert) instead of using Update. I think the problem now is with the deleting functionality. I need to modify it in such a way to be specific for a determined employee not for everyone as displayed below, so how to do that?

Code-Behind (C# code):
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 employeeID=@employeeID";
    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");
}



***The problem is here:***
C#
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 employeeID=@employeeID";
        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");
Posted

Hi,
There's no fault in your code as such. To understand where exactly the problem is you have to debug your code thoroughly.
1) Check Whether you are getting the correct value when you are trying to extract the Employee ID and Updated Course ID from HTML.
2) Also check that your insert command is working properly or not.

Two problems that is with your code is:

1) You havn't closed the connection object anywhere.
2) You can do delete and insert within a Transaction.

Try these tips and do let me know whether problem gets resolved or not.

Thanks,
Mateen
 
Share this answer
 
After many tries and by a great help from one of the developers in StackOverflow Community, I could be able to solve it. The solution is:

C#
//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);
                   }
                }
            }</string></string>
 
Share this answer
 
Hi,
If u r using .net means there is 1 option ini grid veiw r data adoptor. Using this properties option u hv to insert n delete the table, rows at the smaetime.
 
Share this answer
 
Comments
Mycroft Holmes 13-Feb-12 3:55am    
learn to use a keyboard, textspeak is not appreciated and should NEVER be used in an answer. We are trying to educate and help developers not devolve them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900