Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am trying to develop two dynamic tables where the user can add a new row directly in the first table, when he finished adding it, he will be able to select it to open the second table underneath the first table. I have a probelm now in selecting on the rows in the first table in order to show the second table. So how to get it?

FYI, I have the following database design:
SQL
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
QuestionImage Table: ID, QuestionID, URL
Answer Table: AnswerID, Answer
QuizContent Table: ID, QuizID, QuestionID, AnswerID


In addition, the first table will be for inserting the questions and the second table will be for inserting the answers. So how I can select a question to show the second table and being able to insert the answers for that question

ASP.NET Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Table ID="questionsList" runat="server" Width="70%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366; font-size:17px; font-weight:bold;">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell>ID</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Question</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Question Order</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Answer Explanation</asp:TableHeaderCell>
                    </asp:TableHeaderRow>
        </asp:Table>

        <asp:Table ID="Table1" runat="server" Width="70%" CellPadding="5" CellSpacing="0"
                    style="border:2px solid #003366;">
            <asp:TableFooterRow>
                <asp:TableCell>Add</asp:TableCell>

                <asp:TableCell >
                    <asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox>

                    <%--For the txtQuestion TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1"  runat="server"
                                    TargetControlID="txtQuestion" WatermarkText="Type the Question here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:TextBox ID="txtQuestionOrder" runat="server"></asp:TextBox>

                    <%--For the txtQuestionOrder TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2"  runat="server"
                                    TargetControlID="txtQuestionOrder" WatermarkText="Type the Question Order here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:TextBox ID="txtAnswerExplanation" runat="server"></asp:TextBox>

                    <%--For the txtAnswerExplanation TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender3"  runat="server"
                                    TargetControlID="txtAnswerExplanation" WatermarkText="Type the Answer Explanation here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <span style="margin:3px 10px 0px 0px;">
                        <asp:ImageButton ID="addButton" runat="server" ImageUrl="Images/tick_small.png" OnClick="addQuestion" />
                    </span>
                </asp:TableCell>
            </asp:TableFooterRow>
        </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel>


Code-Behind:
C#
public void fill_Questions()
    {

        string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string selectQuery = "SELECT * FROM Question";
        SqlCommand cmd = new SqlCommand(selectQuery, conn);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;

        Table table = (Table)UpdatePanel1.FindControl("questionsList");
        try
        {
            table.Rows.Clear();
            //open the connection
            conn.Open();
            //filling the dataset with the data
            sda.Fill(ds);
            //close the connection
            conn.Close();
        }
        catch
        {
            table.Rows.Clear();
        }


        //Loading the data into the table
        if (ds.Tables.Count > 0)
        {
            int rowsNum = ds.Tables[0].Rows.Count;
            for (int i = 0; i < rowsNum; i++)
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);

                for (int colCount = 0; colCount < 5; colCount++)
                {
                    TableCell cell = new TableCell();
                    row.Cells.Add(cell);

                    if (colCount == 0)
                    {
                        //int n = i + 1;
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["QuestionID"].ToString()));
                    }
                    else if (colCount == 1)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["Question"].ToString()));
                    }
                    else if (colCount == 2)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["QuestionOrder"].ToString()));
                    }
                    else if (colCount == 3)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["AnswerExplanation"].ToString()));
                    }
                    else if (colCount == 4)
                    {
                        ImageButton deleteButton = new ImageButton();
                        deleteButton.ImageUrl = "Images/DeleteRed.png";
                        deleteButton.ID = ds.Tables[0].Rows[i]["QuestionID"].ToString() + "_" + i;
                        deleteButton.Click += new ImageClickEventHandler(deleteQuestion);
                        cell.Controls.Add(deleteButton);

                    }
                } //End for loop
            }//End OUTER for loop

        }//End if statement

    }


    //For deleting question
    public void deleteQuestion(object sender, EventArgs e)
    {
        string id = ((ImageButton)sender).ID.ToString().Split('_')[0];
        try
        {
            string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string deleteQuery = "DELETE FROM Question WHERE QuestionID = @id";
            SqlCommand cmd = new SqlCommand(deleteQuery,conn);
            cmd.Parameters.AddWithValue("@id", id);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            fill_Questions();
        }
        catch(SqlException se){}

        UpdatePanel1.Update();

    }


    //For adding questions
    public void addQuestion(object sender, EventArgs e)
    {
        try
        {
            TextBox txtQuestion = (TextBox)UpdatePanel1.FindControl("txtQuestion");
            TextBox txtQuestionOrder = (TextBox)UpdatePanel1.FindControl("txtQuestionOrder");
            TextBox txtAnswerExplanation = (TextBox)UpdatePanel1.FindControl("txtAnswerExplanation");

            string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string insertQuery = "INSERT INTO Question (Question, QuestionOrder, AnswerExplanation) VALUES (@Question, @QuestionOrder, @AnswerExplanation)";
            SqlCommand cmd = new SqlCommand(insertQuery,conn);
            cmd.Parameters.AddWithValue("@Question", txtQuestion.Text);
            cmd.Parameters.AddWithValue("@QuestionOrder", txtQuestionOrder.Text);
            cmd.Parameters.AddWithValue("@AnswerExplanation", txtAnswerExplanation.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            fill_Questions();
        }
        catch(SqlException se){}
    }


**UPDATE:**

I created two tables for the Answers as I did for the questions. Now, **how am I gonna be able to show these table after selecting one question?**

ASP.NET code:
<div align="center">
    <h3>Answers List</h3>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Table ID="answersList" runat="server" Width="70%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366; font-size:17px; font-weight:bold;">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell>Answer</asp:TableHeaderCell>
                    </asp:TableHeaderRow>
        </asp:Table>

        <asp:Table ID="Table2" runat="server" Width="70%" CellPadding="5" CellSpacing="0"
                    style="border:2px solid #003366;">
            <asp:TableFooterRow>
                <asp:TableCell>Add</asp:TableCell>

                <asp:TableCell >
                    <asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>

                    <%--For the txtQuestion TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender4"  runat="server"
                                    TargetControlID="txtAnswer" WatermarkText="Type the Answer here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:CheckBox ID="isCorrectCheckBox" runat="server" />
                </asp:TableCell>

                <asp:TableCell>
                    <span style="margin:3px 10px 0px 0px;">
                        <asp:ImageButton ID="ImageButton1" runat="server" ToolTip="Add" ImageUrl="Images/tick_small.png" OnClick="addAnswer" />
                    </span>
                </asp:TableCell>
            </asp:TableFooterRow>
        </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel>
    </div>
Posted
Comments
Sebastian T Xavier 21-Jul-12 0:32am    
why this code dump?
matrix388 21-Jul-12 0:34am    
what do you mean?
Kenneth Haugland 21-Jul-12 18:14pm    
Too much info I think. So your problem is binding the selected row in on table, to update the entire table 2?
matrix388 22-Jul-12 0:24am    
Kenneth, yes, you're right.

1 solution

 
Share this answer
 
v2

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