akash  singh

akash singh

  • NA
  • 122
  • 72.2k

Object reference not set to an instance of an object.

Dec 24 2014 5:52 AM
i want to save the value of text box ..bt it gives error.. 
code is here...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


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


    }
    protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (ListBox2.SelectedIndex != -1)
        {
            Table tbl = new Table();
            tbl.ID = "Tabl";




            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                 //TableRow tr = new TableRow();


                if (ListBox2.Items[i].Selected)
                {
                    //++ind;
                   TableRow tr = new TableRow();
                    TableCell col = new TableCell();
                    col.Width = 100;
                    Label Lbl = new Label();
                    Lbl.Text = ListBox2.Items[i].Text;
                    Lbl.ID = "lbl" + i.ToString();
                    Lbl.ForeColor = System.Drawing.Color.Red;
                    col.Controls.Add(Lbl);
                    // arr[ind] = Lbl.Text;                                
                    TableCell col1 = new TableCell();
                    TextBox txt = new TextBox();
                    txt.ID = "txt" + i.ToString();
                    // txt.ID = ListBox2.Items[i].Text;
                    txt.Width = 150;
                    txt.Height = 20;
                    col1.Controls.Add(txt);
                    tr.Cells.Add(col);
                    tr.Cells.Add(col1);
                    //Lbl1.Text = "txt" + i.ToString(); 
                    //  txt.TextChanged += new EventHandler(adddff);
                    //  LabCHK.Text += tr.Cells[2];


                   // tbl.Rows.Add(tr);
                    tbl.Rows.Add(tr);




                }
               


            }


            Panle1.Controls.Add(tbl);


        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Table t = (Table)Page.FindControl("Panle1").FindControl("Tabl");
        TextBox tbox;
        foreach (TableRow tr in t.Rows)
        {
            foreach (TableCell tc in tr.Cells)
            {
                foreach (Control c in tc.Controls)
                {
                    if (c.GetType() == typeof(TextBox))
                    {
                        tbox = (TextBox)c;


                        Response.Write("TextBox ID = " + tbox.ID + "<br/>");
                        Response.Write("TextBox Text = " + tbox.Text + "<br/>");
                    }
                }
            }
        }
    }
       
}

Answers (3)