Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
need an insert query for my code , i wrote code to add table rows having textboxes, dynamically, e.g yoh have only passed HSSC exam then you have to fill 2 levels of education SSC, HSSC......or say you have passed Phd , then SSC,HSSC,BS, MS, Phd, you have to fill all, for which i worte code , given below but how an insert statement would look like for it ,i have no idea anymore
Using asp.net 3.5(c#),sql server 2008

Please check:

C#
  using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class trying : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int cnt = Convert.ToInt32(DropDownList1.SelectedValue);
        GenerateTable(5, cnt);
    }
    //Here’s the code block for the generating the Tables with TextBoxes.

    private void GenerateTable(int colsCount, int rowsCount)
    {
        //Creat the Table and Add it to the Page
        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);

        // Now iterate through the table and add your controls 
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                TableCell lblcell = new TableCell();
                TextBox tb = new TextBox();
                Label lbl = new Label();

                // Set a unique ID for each TextBox added
                tb.ID = "TextBoxRow_" + i + "Col_" + j;
                lbl.ID = "Label_" + i + "Col_" + j;
                lbl.Text = DropDownList1.Items[i].ToString();
                // Add the control to the TableCell
                lblcell.Controls.Add(lbl);
                cell.Controls.Add(tb);

                // Add the TableCell to the TableRow
                row.Cells.Add(lblcell);
                row.Cells.Add(cell);
            }

            // Add the TableRow to the Table
            table.Rows.Add(row);
        }
        Panel1.Controls.Add(table);
    }
}

<pre lang="HTML">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="trying.aspx.cs" Inherits="trying" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <div id="pnlTextBox">
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="150px">
                <asp:ListItem Value="1">SSLC</asp:ListItem>
                <asp:ListItem Value="2">HSC</asp:ListItem>
                <asp:ListItem Value="3">Graduate</asp:ListItem>
                <asp:ListItem Value="4">Post Graduate</asp:ListItem>
                <asp:ListItem Value="5">Ph.D</asp:ListItem>
            </asp:DropDownList>
            <asp:Panel ID="Panel1" runat="server">
            </asp:Panel>
        </div>

        </ContentTemplate>
        </asp:UpdatePanel>

    </form>
</body>
</html>
Posted
Comments
Shahriar Iqbal Chowdhury/Galib 20-Oct-12 14:39pm    
what is your database table name where you want to insert? I see no code for database insertion

1 solution

In a same for loop, u can take two string variables ine for column names and other for values,
u can execute that string through sql client..
 
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