Dynamic Table Creation and Insert using Database in ASP.NET

Dynamic table creation and insert using database in asp.net :

DataBase

Select * From size

Image1.jpg

Select * From Attributelist

Image2.jpg

Aspx :

<div>
<asp:Panel ID="pnl" runat="server">
</asp:Panel>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Button" />
</div>

aspx.cs :

SqlConnection Conn = new

SqlConnection(ConfigurationManager.AppSettings["Connection"]);

SqlDataAdapter da = new SqlDataAdapter();

DataSet ds = new DataSet();

DataSet dsTest = new DataSet();

DataSet dsSize = new DataSet();

DataSet dsAttr = new DataSet();

int Count;

int CountSize, CountAttr;

string SelectAttr, SelectSize;

 

protected void Page_Load(object sender, EventArgs e)

{

--Load Table Dynamic Creation

tblCreation();

}

--Dynamic Creation Table

public void tblCreation()

{

--Select Attributelist

SelectAttr = "Select * From Attributelist";

da = new SqlDataAdapter(SelectAttr, Conn);

da.Fill(dsAttr);

CountAttr = dsAttr.Tables[0].Rows.Count;

--Select Size

SelectSize = "Select * From size";

da = new SqlDataAdapter(SelectSize, Conn);

da.Fill(dsSize);

CountSize = dsSize.Tables[0].Rows.Count;

--Create Table <table>

Table tbldynamicTest = new Table();

--Create TableRow <tr>

TableRow trTest = new TableRow();

--Create TableCell tableData <td>

TableCell tcTest = new TableCell();

--Create Label

Label lblHead = new Label();

trTest.Height = 20;

Label lblID1 = new Label();

tcTest.Width = 100;

lblID1.Height = 20;

lblID1.ID = "lb1";

lblID1.Text = "Name";

lblID1.Width = 100;

--TableCell Add Label

tcTest.Controls.Add(lblID1);

--TableRow Add TableCell

trTest.Cells.Add(tcTest);

--Table Add TableRow

tbldynamicTest.Rows.Add(trTest);

--ColumnCreation

for (int i = 0; i < CountSize; i++)

{

 

trTest.Height = 20;

Label lblName = new Label();

lblName.ID =

"lblName" + dsSize.Tables[0].Rows[i]["size"].ToString()+i;

lblName.Text =

dsSize.Tables[0].Rows[i]["size"].ToString();

lblName.CssClass = "lbl";

lblName.Width = 100;

--Table Cell Add Label

tcTest.Controls.Add(lblName);

--Table Row Add TableCell

trTest.Cells.Add(tcTest);

}

--Table Add TableRow

tbldynamicTest.Rows.Add(trTest);

--Panel Add Table

pnl.Controls.Add(tbldynamicTest);

for (int i = 0; i < CountAttr; i++)

{

TableRow tr = new TableRow();

TableCell tc = new TableCell();

tr.Height = 20;

Label lblName = new Label();

lblName.ID = "lblName" + i;

lblName.Text = dsAttr.Tables[0].Rows[i]["name"].ToString();

lblName.CssClass = "lbl";

lblName.Width = 100;

tc.Controls.Add(lblName);

tr.Cells.Add(tc);

for (int j = 0; j < CountSize; j++)

{

--Checkbox Creation

CheckBox chk = new CheckBox();

chk.ID =

"chk" +dsAttr.Tables[0].Rows[i]["name"].ToString()+ j;

chk.Height = 25;

chk.CssClass = "txt";

chk.Width = 100;

tc.Controls.Add(chk);

tr.Cells.Add(tc);

}

--Table Add TR

tbldynamicTest.Rows.Add(tr);

pnl.Controls.Add(tbldynamicTest);

}

--Panel Add Table

pnl.Controls.Add(tbldynamicTest);

}

 
Image3.jpg

 

--Save

protected void Button1_Click(object sender, EventArgs e)

{

for (int i = 0; i < CountAttr; i++)

{

string opid1 =

"lblName" + dsAttr.Tables[0].Rows[i]["name"].ToString() + i;

Label lblName = (Label)pnl.FindControl(opid1);

string Attr;

Attr = dsAttr.Tables[0].Rows[i]["name"].ToString();

for (int m = 0; m < CountSize; m++)

{

string aa =

"chk" + dsAttr.Tables[0].Rows[i]["name"].ToString() + m;

CheckBox chk = (CheckBox)pnl.FindControl(aa);

string opid = "lblName1" + m;

--(pnl)Panel Get label ID

Label lblName1 = (Label)pnl.FindControl(opid);

string Size = dsSize.Tables[0].Rows[m]["size"].ToString();

--Checkbox Checked

if (chk.Checked == true)

{

Conn.Open();

--Insert Checked Values

SqlCommand cmd = new

SqlCommand("insert into test values

('" + Size.ToString() + "','" + Attr.ToString() + "')", Conn);

cmd.ExecuteNonQuery();

Conn.Close();

}

}

}

}