Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 113.6k

Create dynamic table based on the result of a query

Apr 8 2018 12:03 PM
Dear Code Master,
 
I hope this question meet you all in good health.
Please i am trying to create a table that will display price trend base on the result of a query from price table the following table
 
 
Description Unit Rate Mnths Yrs States
Velox beam unit size 250mm x 140mm x 115mm high Nr 12.00 FEB 2014 Abuja
Masonia Length 13.00 FEB 2014 Abuja
533 x 210mm x 83kg/m Length 34.20 FEB 2014 Abuja
32mm x 4mm thick Length 1,190.90 FEB 2015 Abuja
20.0mm Thick Sheet 2,100.10 FEB 2015 Abuja
600mm x 600mm Nr 1,200.10 FEB 2015 Abuja
Gloss (white) Gal 1,200.00 FEB 2016 Abuja
Deep-well submersible pump 147 meters suction depth, 7.5kw collection output Nr 2,000.00 FEB 2016 Abuja
6.0mm2 Cable Coil 2,200.00 FEB 2016 Abuja
5 Amp two gang, ditto Nr 3,200.00 FEB 2017 Abuja
FM 200 discharge nozzle Nr 3,200.00 FEB 2017 Abuja
Clear glass block size 145mm 145mm 95mm Nr 1,000.00 FEB 2017 Abuja
Ribbed glass block size 145mm 145mm 95mm Nr 9,821.00 FEB 2017 Abuja
28 swg (Effective 18m2/Bundle) Bundle 10,234.00 FEB 2017 Abuja
 
I want the table to become as follows
 
Description 2014 2015 2016 2017
533 x 210mm x 83kg/m 12.00 12.10 12.11 12.12
Masonia 31.00 22.00 33.00 21.00
533 x 210mm x 83kg/m 22.00 21.00 233.00 33.00
 
 My tried code is below
  1. if (DrpMonth.Text != "" && DrpYear.Text != "" && DrpYear1.Text != "")  
  2.             {  
  3.                 var DT = new DataTable();  
  4.                 DT = ConnectAll.GetDataTable(string.Concat("select *  from tbl_SubBasicPrice where Category ='" + DropDownList4.SelectedItem.Text.Trim() + "' ",  
  5.                               " and subCategory='" + DropDownList5.SelectedItem.Text.Trim() + "' and mnths='" + DrpMonth.SelectedItem.Text.Trim() + "' ",  
  6.                               " and yrs BETWEEN '" + DrpYear.SelectedItem.Text.Trim() + "' and '" + DrpYear1.SelectedItem.Text.Trim() + "' ",  
  7.                               " and states= '" + DropDownList3.SelectedItem.Text.Trim() + "'"));  
  8.   
  9.                 using (SqlConnection cn = new SqlConnection(ConnectAll.ConnectMe()))  
  10.                 {  
  11.                     cn.Open();  
  12.   
  13.                     List<string> Col = new List<string>();  
  14.                     List<string> Ro = new List<string>();  
  15.   
  16.                     foreach (DataRow r in DT.Rows)  
  17.                     {  
  18.                         foreach (DataColumn c in DT.Columns)  
  19.                         {  
  20.                             //Col.Add(c.ColumnName.ToString());  
  21.                             if (c.ColumnName.Trim() == "Yrs")  
  22.                             {  
  23.                                 Ro.Add(r["Yrs"].ToString());  
  24.                             }  
  25.   
  26.                         }  
  27.                     }  
  28.                     string SQL = "if object_id(N'Temp_SubBasicPrice',N'U') is not null ";  
  29.                     SQL += " begin ";  
  30.                     SQL += "    DROP table Temp_SubBasicPrice ";  
  31.                     SQL += " end ";  
  32.                     SQL += " else ";  
  33.                     SQL += " begin ";  
  34.                     SQL += " DECLARE @SQL1 NVARCHAR (4000) ";  
  35.                     SQL += " SELECT @SQL1 ='CREATE TABLE Temp_SubBasicPrice( ";  
  36.                     SQL += "    [ID] int identity(1,1) not null,";  
  37.                     SQL += "    Description nvarchar(max), ";  
  38.                       
  39.                     foreach (string c in Ro)  
  40.                     {  
  41.                         SQL += " " + c.ToString() + "  numeric(8,2), ";  
  42.                     }  
  43.                     SQL.Replace(",""");  
  44.                     SQL += "  )' ";  
  45.                     SQL += " exec (@SQL1)";  
  46.                     SQL += "  end ";  
  47.   
  48.                     SqlCommand cmdA = new SqlCommand(SQL, cn);  
  49.                     cmdA.ExecuteNonQuery();  
  50.                     cmdA.Dispose();  
  51.                     cn.Close();  
  52.                 }  
  53.   
  54.             }  
 the code is working but not creating the table.
 
Any solution will be apreciated.
 
Thank you and God bless you all

Answers (4)