Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Seleted value from dynamic dropdownlist can't acess

    Main error are coming in button 2 while retrving the value .Button is ok. create the dynamically dropdownlist as entered in textbox .
    Error is
    " Object reference not set to an instance of an object."
    " value = dr.SelectedValue.ToString();"

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
     
    public partial class assigned : System.Web.UI.Page
    {
        DropDownList d;
        DropDownList dr;
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {
           
     
     
        }
                
     
       
        protected void Button1_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text);
           
            try
            {  
               
                for (int i = 0; i < a; i++)
                {
                    d = new DropDownList();
                    d.ID = "Text" + i;
                    LiteralControl l1 = new LiteralControl("<br></br>");
                  
                    d.Items.Add("Rahul");
                    d.Items.Add("Gautum");
                    d.Items.Add("anant");
                    
                   
                    Panel3.Controls.Add(d);
                    Panel3.Controls.Add(l1);
              
                    
                }
                
            }
            catch (Exception e1)
            {
                Label4.Text = "" + e1.Message.ToString();
            }
        }
      
     
        protected void Button2_Click(object sender, EventArgs e)
        {
     
     
     
          int a = Convert.ToInt32(TextBox1.Text);
                string value = "";
                for (int i = 0; i < a; i++)
                {
                    string str  ="Text" + i;
                    dr = new DropDownList();
                    dr = (DropDownList)(Panel3.FindControl(str));
                   
                    value = dr.SelectedValue.ToString();
                         SqlConnection con = new SqlConnection("Data Source=ABC-0D78787890A;Initial Catalog=Amit;Integrated Security=True");
                   
                    con.Open();
     
                    SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);
                    cmd1.ExecuteNonQuery();
                    con.Close();
                   
                    
                }
                    
                }
     
        }
    }

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by goltu View Post
    Main error are coming in button 2 while retrving the value .Button is ok. create the dynamically dropdownlist as entered in textbox .
    Error is
    " Object reference not set to an instance of an object."
    " value = dr.SelectedValue.ToString();"

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
     
    public partial class assigned : System.Web.UI.Page
    {
        DropDownList d;
        DropDownList dr;
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {
           
     
     
        }
                
     
       
        protected void Button1_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text);
           
            try
            {  
               
                for (int i = 0; i < a; i++)
                {
                    d = new DropDownList();
                    d.ID = "Text" + i;
                    LiteralControl l1 = new LiteralControl("<br></br>");
                  
                    d.Items.Add("Rahul");
                    d.Items.Add("Gautum");
                    d.Items.Add("anant");
                    
                   
                    Panel3.Controls.Add(d);
                    Panel3.Controls.Add(l1);
              
                    
                }
                
            }
            catch (Exception e1)
            {
                Label4.Text = "" + e1.Message.ToString();
            }
        }
      
     
        protected void Button2_Click(object sender, EventArgs e)
        {
     
     
     
          int a = Convert.ToInt32(TextBox1.Text);
                string value = "";
                for (int i = 0; i < a; i++)
                {
                    string str  ="Text" + i;
                    dr = new DropDownList();
                    dr = (DropDownList)(Panel3.FindControl(str));
                   
                    value = dr.SelectedValue.ToString();
                         SqlConnection con = new SqlConnection("Data Source=ABC-0D78787890A;Initial Catalog=Amit;Integrated Security=True");
                   
                    con.Open();
     
                    SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);
                    cmd1.ExecuteNonQuery();
                    con.Close();
                   
                    
                }
                    
                }
     
        }
    }
    Hello,

    Can you please be more specific of your requirements. Where is the code to bind the dropdown list?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •