You must Sign In to post a response.
  • Category: .NET

    How to save values of dynamically created controls to database

    hi all

    below is my code i have created dynamic controls when saving into database its throwing error "object reference not set to instance of an object"

    so any body helpme
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

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

    }

    private void loadDynamicGrid()
    {
    int i = 0;
    Table tbldynamic = new Table();

    tbldynamic.ID = "table1";
    tbldynamic.CellPadding = 4;
    TableCell tc = new TableCell();
    TableRow tr1 = new TableRow();

    TableCell tc1 = new TableCell();
    TableRow tr2 = new TableRow();
    TableCell tc2 = new TableCell();
    TableRow tr3 = new TableRow();
    TableCell tc3 = new TableCell();
    TableRow tr4 = new TableRow();
    TableCell tc4 = new TableCell();
    TableRow tr5 = new TableRow();
    TableCell tc5 = new TableCell();
    TableRow tr6= new TableRow();



    TextBox TxtBoxU = new TextBox();

    TextBox TxtBoxE = new TextBox();

    TextBox txt = new TextBox();
    CheckBox chk = new CheckBox();

    CheckBox chka = new CheckBox();

    Label lblU = new Label();
    Label lblE = new Label();
    Label lbh = new Label();
    Label lba = new Label();
    Label grade = new Label();
    DropDownList dd = new DropDownList();
    Button btnSubmit = new Button();
    btnSubmit.ID = "btnSubmit";
    btnSubmit.Text = "Submit";
    btnSubmit.Click += new System.EventHandler(btnSubmit_click);
    ListItem[] items = new ListItem[3];
    items[0] = new ListItem("A Grade", "1");
    items[1] = new ListItem("B Grade", "2");

    items[2] = new ListItem("C Grade", "3");
    dd.Items.AddRange(items);
    dd.DataBind();
    TxtBoxU.ID = "TextBoxU" + i.ToString();
    TxtBoxE.ID = "TextBoxE" + i.ToString();
    txt.ID = "txt" + i.ToString();
    chk.ID = "chk" + i.ToString();
    chka.ID = "chka" + i.ToString();
    lblU.ID = "LabelU" + i.ToString();
    lblE.ID = "LabelE" + i.ToString();
    lbh.ID = "label h" + i.ToString();
    lba.ID = "labela" + i.ToString();
    grade.ID = "labelb" + i.ToString();

    //TxtBoxE.ID = "TextBoxE" ;
    //txt.ID = "txt";
    //chk.ID = "chk" ;
    //chka.ID = "chka";
    //lblU.ID = "LabelU" ;
    //lblE.ID = "LabelE";
    //lbh.ID = "label h";
    //lba.ID = "labela" ;
    //grade.ID = "labelb";
    TxtBoxU.Attributes.Add("runat", "Server");
    TxtBoxE.Attributes.Add("runat", "Server");
    chk.Attributes.Add("runat", "Server");
    chka.Attributes.Add("runat", "Server");
    txt.Attributes.Add("runat", "Server");
    btnSubmit.Attributes.Add("runat", "Server");
    dd.ID = "da" + i.ToString();
    lblU.Text = "Children firstname: ";
    Panel1.Controls.Add(lblU);
    Panel1.Controls.Add(TxtBoxU);
    lblE.Text = "children lastname : ";
    Panel1.Controls.Add(lblE);
    Panel1.Controls.Add(TxtBoxE);
    lbh.Text = "Going to school?:";
    Panel1.Controls.Add(lbh);

    Panel1.Controls.Add(chk);
    Panel1.Controls.Add(chka);
    lba.Text = "school name:";
    grade.Text = "Grade:";
    Panel1.Controls.Add(lba);
    Panel1.Controls.Add(txt);
    Panel1.Controls.Add(grade);
    Panel1.Controls.Add(dd);

    tc5.Controls.Add(btnSubmit);
    tc.Controls.Add(lblU);
    tc.Controls.Add(TxtBoxU);
    chk.Text = "yes";
    chka.Text = "no";
    tc4.Controls.Add(grade);
    tc4.Controls.Add(dd);

    tc1.Controls.Add(lblU);
    tc1.Controls.Add(TxtBoxU);
    tc2.Controls.Add(lbh);
    tc2.Controls.Add(chk);
    tc2.Controls.Add(chka);
    tc3.Controls.Add(lba);
    tc3.Controls.Add(txt);
    tr1.Cells.Add(tc);
    tr1.Cells.Add(tc);
    tr2.Cells.Add(tc1);
    tr2.Cells.Add(tc1);
    tr3.Cells.Add(tc2);
    tr3.Cells.Add(tc2);
    tr3.Cells.Add(tc2);
    tr4.Cells.Add(tc3);
    tr4.Cells.Add(tc3);
    tr5.Cells.Add(tc4);
    tr5.Cells.Add(tc4);
    tr6.Cells.Add(tc5);




    tbldynamic.Controls.Add(tr1);
    tbldynamic.Controls.Add(tr1);
    tbldynamic.Controls.Add(tr2);
    tbldynamic.Controls.Add(tr2);
    tbldynamic.Controls.Add(tr3);
    tbldynamic.Controls.Add(tr3);
    tbldynamic.Controls.Add(tr3);
    tbldynamic.Controls.Add(tr4);
    tbldynamic.Controls.Add(tr4);
    tbldynamic.Controls.Add(tr5);
    tbldynamic.Controls.Add(tr5);
    tbldynamic.Controls.Add(tr6);
    i = i + 1;
    //btn1.Click += new EventHandler(this.btn1_Click);
    Panel1.Controls.Add(tbldynamic);





    }
    protected void btnSubmit_click(object sender, EventArgs e)
    {
    String str = string.Empty;
    TextBox txtfirstname = (TextBox)Panel1.FindControl("TxtBoxU");
    // str = txtUserName.Text;
    TextBox txtlastname = (TextBox)Panel1.FindControl("TextBoxE");
    CheckBox scholind = (CheckBox)Panel1.FindControl("chk");
    CheckBox scholinda = (CheckBox)Panel1.FindControl("chka");
    TextBox schoolname = (TextBox)Panel1.FindControl("txt");
    DropDownList adddd = (DropDownList)Panel1.FindControl("dd");
    SqlConnection con = new SqlConnection("Data Source=HOME;Initial Catalog=login;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("insert into studentdetails values('" + txtfirstname.Text + "','" + txtlastname.Text + "','" + scholind.Text + "','" + schoolname.Text + "','" + adddd.SelectedItem.Value.ToString() + "')", con);

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();


    }

    protected void Button4_Click(object sender, EventArgs e)
    {
    int rowCount = Convert.ToInt32(TextBox6.Text);

    for (int i = 0; i < rowCount; i++)
    {
    loadDynamicGrid();

    }


    }


    protected void Button3_Click(object sender, EventArgs e)
    {

    TextBox txtfirstname = (TextBox)Panel1.FindControl("TxtBoxU");
    // str = txtUserName.Text;
    TextBox txtlastname = (TextBox)Panel1.FindControl("TextBoxE");
    CheckBox scholind = (CheckBox)Panel1.FindControl("chk");

    TextBox schoolname = (TextBox)Panel1.FindControl("txt");
    DropDownList adddd = (DropDownList)Panel1.FindControl("dd");
    SqlConnection con = new SqlConnection("Data Source=HOME;Initial Catalog=login;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("insert into studentdetails values('" + txtfirstname.Text + "','" + txtlastname.Text + "','" + scholind.Text + "','" + schoolname.Text + "','" + adddd.SelectedItem.Text+ "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();


    }
  • #762871
    Hi,

    I guess the problem is finding the control. May be your control is null when you are trying to pass some extension to that obviously it's throwing object reference not set to an instance of an object error only.

    EX:

    txt.Text;

    in above example "txt" is the Textbox control Id; Assume that txt is of type null, if you are trying to access Text based on null object you are getting the above error.

    This type of situations we can't say the exact problem, my request is better to debug your program and check in which line you got this error, and find the root cause of this and then try to fix it.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762873
    Hello Velidi Viram,

    Refer the below code to see how to get value of dynamic created textbox :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div style="font-family: 'Segoe UI'">
    <br />
    <asp:TextBox ID="TextBoxcount" Style="width: 250px" runat="server" placeholder="Enter no. of textboxes you want to create"></asp:TextBox>
    <br />
    <asp:Button ID="ButtonCreate" runat="server" Text="Create Textboxes"
    onclick="ButtonCreate_Click" />
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    <br />
    <asp:Label ID="Label2" runat="server" Text="Values :"></asp:Label>
    <br />
    <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
    </div>
    </form>
    </body>
    </html>

    Code - behind :

    protected void ButtonCreate_Click(object sender, EventArgs e)
    {
    Label1.Text = "";
    int count = Convert.ToInt32(TextBoxcount.Text);
    for (int i = 0; i < count; i++)
    {
    TextBox txt = new TextBox();
    txt.BorderStyle = BorderStyle.Solid;
    txt.ID = "TextBox" + i;
    txt.Text = "Dynamic TextBox" + i;
    Panel1.Controls.Add(txt);
    }

    for (int i = 0; i < count; i++)
    {
    TextBox box1 = (TextBox)Panel1.FindControl("TextBox" + i);
    Label1.Text += "<br>" + box1.Text;
    }
    }

    See the attached image. It's an output i have got using above code.


    Hope this will help you to understand that where you are creating a mistake.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment


Sign In to post your comments