Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is what I use to generate a table based on a query:
It outputs the cells and checkbox but not the values.

C#
SqlConnection con1 = new SqlConnection(main.ConnString);
            SqlCommand cmd1 = new SqlCommand();
            cmd1.CommandText = "SELECT * FROM [main_settings] WHERE [usertype] = 'webadmin' AND [status] = 'active'";
            cmd1.Connection = con1;

            List<string> l1 = new List<string>(), l2 = new List<string>();
            List<bool> l3 = new List<bool>();
            int counter = 0;

            con1.Open();
            SqlDataReader r1 = cmd1.ExecuteReader();
            while (r1.Read())
            {
                l1.Add(r1.GetString(2).ToString());
                l2.Add(r1.GetString(4).ToString());

                string tempstr = r1.GetString(5).ToString();
                if (tempstr.ToLower() == "true")
                {
                    l3.Add(true);
                }
                else
                {
                    l3.Add(false);
                }
                counter++;
            }

            string[] temptooltip = new string[counter], tempmainstring = new string[counter];
            bool[] tempstat = new bool[counter];

            TableHeaderRow h1 = new TableHeaderRow();
            TableHeaderCell hc1 = new TableHeaderCell();
            TableHeaderCell hc2 = new TableHeaderCell();

            hc1.Text = "Web Page";
            hc2.Text = "Status";

            h1.Cells.Add(hc1);
            h1.Cells.Add(hc2);

            webadminTable.Controls.Add(h1);

            for (int i = 0; i < counter; i++)
            {
                CheckBox ctemp = new CheckBox();
                TableRow temptr = new TableRow();
                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();

                ctemp.Checked = tempstat[i];
                ctemp.ToolTip = temptooltip[i];

                tc1.Text = tempmainstring[i];
                tc2.Controls.Add(ctemp);

                temptr.Cells.Add(tc1);
                temptr.Cells.Add(tc2);

                webadminTable.Rows.Add(temptr);

                ctemp.Dispose();
                tc1.Dispose();
                tc2.Dispose();
                temptr.Dispose();
            }


What I have tried:

I tried casting the results of the query nothing happened still.
Posted
Updated 1-Jun-21 0:30am
Comments
BillWoodruff 1-Jun-21 6:17am    
If Table means DataTable, bind it to an appropriate UI control, like a DataGridView, or, create a special UI like a UserControl.

1 solution

I'm not surprised "nothing happened" - that is some odd looking code.
You've got one loop filling three lists you never use and which go out of scope at the end of the method, and a second loop throwing away the rows it creates - you can't use a row in your webadminTable that you just called Dispose on!

And as for the rest of it, it doesn't make much sense at all.

So, start again: look at your database table and decide what you want to display and where - then use a DataAdapter to load that into a DataTable which you can set as the DataSource for a UI table-based control that is appropriate for your environment (a WinForms DataGridView won;t work in a web based app for example).
 
Share this answer
 

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