1

I'm Prateek, trying to create an application that takes user inputs from the controls that are being generated dynamically such as a TextBox or a DropDownList.

These controls are generated from the database values itself i.e., I'm storing all the values that are to be created in a table named 'usertasks'.

Now, everything is working fine and controls are dynamically generated, the user inputs are stored in another table named 'taskEntries'. The problem I'm facing is with the values being fetched:

enter image description here

The above image is of a table to save what kind of controls the user wants to create and their labels.

Another image to show how data is being stored right now: enter image description here

How I want the data to be fetched: enter image description here

Kindly let me know how do I do it in asp.net c#.

What I tried?

//Below code to get all data into a DataTable
protected void getData()
{
    using (SqlConnection con = new SqlConnection(ConnectionManager.ConString))
    {
        con.Open();
        using (SqlDataAdapter sda = new SqlDataAdapter("select (cdate + ', ' + ctime) as 'Date', taskID as 'Task ID', deptID as 'Department ID', empID as 'Employee ID', question as 'Question', userInput as 'Input' from taskEntries", con))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ViewState["dt"] = dt;
            BindGrid(dt, false);
        }
        con.Close();
    }
    ConvertRowsToColumns();
}
//Below code to bind the gridview
private void BindGrid(DataTable dt, bool rotate)
{
    grdUserData.ShowHeader = !rotate;
    grdUserData.DataSource = dt;
    grdUserData.DataBind();
    if (rotate)
    {
        foreach (GridViewRow row in grdUserData.Rows)
        {
            row.Cells[0].CssClass = "header";
        }
    }
}
//Below code to convert the rows to columns
private void ConvertRowsToColumns()
{
    DataTable dt = (DataTable)ViewState["dt"];
    DataTable dt2 = new DataTable();
    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        String Question = Convert.ToString(dt.Rows[i]["question"]);
        String InputType = Convert.ToString(dt.Rows[i]["inputType"]);
        dt2.Columns.Add(Question);
        dt2.Columns.Add(InputType);
    }
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        dt2.Rows.Add();
        dt2.Rows[i][0] = dt.Columns[i].ColumnName;
    }
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        for (int j = 0; j < dt.Rows.Count; j++)
        {
            dt2.Rows[i][j + 1] = dt.Rows[j][i];
        }
    }
    BindGrid(dt2, true);
}
  • is this for some kind of a CMS? Are the values to pivot constant or they can change (dynamic) ? – kshkarin Mar 25 '21 at 12:20
  • The values can change and controls are generated dynamically in which user enters the surveys like google forms – Prateek Jahagirdar Mar 25 '21 at 13:01
  • then why not use pivot in SQL? it's possible to transform the rows to columns, and also do it dynamically https://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query – kshkarin Mar 25 '21 at 14:48
  • ya but I'm not using any aggregation function here. – Prateek Jahagirdar Mar 26 '21 at 09:00
  • https://stackoverflow.com/questions/21630650/pivot-with-varchar-datatype – kshkarin Mar 26 '21 at 09:14
  • Kindly note, here I'm generating dynamic controls so I don't know what will be the entries made by the users. All I know is the "Questions" column in which all the questions will be stored and the "UserInput" column in which all the answers will be stored as shown in the image. So I want the questions to be the column headers and answers to be the rows. – Prateek Jahagirdar Mar 26 '21 at 11:52
  • `Select taskID, [Store Name],[DMS Code],[Zone] from (select taskID, question, userInput from dbo.taskEntries) as Source_Table PIVOT (Max(userInput) FOR question in ([Store Name],[DMS Code],[Zone]) ) as Pivot_Table;` So, this is the query I use for pivot but in my case I don't even know what questions will be For example, they can be **Mobile Number**, **Address**, **Pin Code** instead of **[Store Name]**, **[DMS Code]**, **[Zone]** – Prateek Jahagirdar Mar 26 '21 at 12:44
  • Here users will create the questions and users only will answers line **Google Forms**. So in what way can, I modify my above query to get this [link](https://i.stack.imgur.com/q5Vl0.png) as output. – Prateek Jahagirdar Mar 26 '21 at 12:49

0 Answers0