First in web form THere will appear one textbox and button. enter the number in textbox and press the button.Pressing button create Dynamically t number of dropdownlist as entered in textbox.I successful do it .But when i what use the seletedvalue of dropdownlist I cannot through anthor buttton .

I have globally declare dropdownlist object d;

Dropdownlist    d;
    protected void Button1_Click(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBox1.Text);
       
                 
            for (int i = 0; i < a; i++)

            {
                d = new DropDownList();
                d.ID = "Text" + i;
                LiteralControl l1 = new LiteralControl("<br></br>");
                SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");

                con1.Open();
                d.Items.Add("amit");
                d.Items.Add("asas");
               
                Panel3.Controls.Add(d);
                Panel3.Controls.Add(l1);

                         
                con1.Close();
            }
            
        }
    
    
    protected void Button2_Click(object sender, EventArgs e)
    {



       
            int a = Convert.ToInt32(TextBox1.Text);

            string value = "";
            for (int i = 0; i < a; i++)
            {
                        //d = new DropDownList();
                value = d.SelectedValue.ToString();
                //DropDownList d = new DropDownList();

                //DropDownList dr = (DropDownList)Session["Product" + i];
                //dr = (DropDownList)Session["Productid" + i];
                //dr.ID = "Text" + i;

                //value = dr.SelectedValue();

                SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=IT;Integrated Security=True");
              
                con.Open();

                SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);

                cmd1.ExecuteNonQuery();
                con.Close();

            }

     
    
}

Hmm.. that should be quite simple.

you have to find the control by the ID in the page.. I think this should work:

for (int i = 0; i < a; i++)
            {
                DropDownList d = (DropDownList)this.Page.FindControl("Text" + i);
                value = d.SelectedValue.ToString();


                SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=IT;Integrated Security=True");
              
                con.Open();

                SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);

                cmd1.ExecuteNonQuery();
                con.Close();

            }

this error in run time
DropDownList d = (DropDownList)this.Page.FindControl("Text" + i);
value = d.SelectedValue.ToString();
Object reference not set to an instance of an object.

Ups... didn't saw that panel...

Just try this:

DropDownList d = (DropDownList)Panel3.FindControl("Text" + i);
DropDownList d = (DropDownList)Panel3.FindControl("Text" + i);
                        value = d.SelectedValue.ToString();

sir same error
Object reference not set to an instance of an object.

The problem is probably that you're dropdownlist controls are not being rebuilt when the page loads. Before the second button's event gets fired the page will attempt to reload the page using the values saved in viewstate, because you are adding these controls dynamically they will not be available in viewstate. One option you might try is to maintain a count of the controls you add to the panel using a session variable or storing the count in a hidden control. Then during page_load add that number of dropdowns back into the panel.

Please send code for it I do not understand how to implement it Please
"
One option you might try is to maintain a count of the controls you add to the panel using a session variable or storing the count in a hidden control. Then during page_load add that number of dropdowns back into the panel."

Ok, I'll try to walk through a quick example for you. I've created a page that has two button controls, a Panel and a label. What it's going to do is on the click of the first button there will be 5 dropdownlists added to the panel. On the second button click it will get the values from those dropdownlists and display them all in the label. The first thing I need to do is to have a way to know how many dropdownlists I have created. In order to do this I will create a property that saves that count in viewstate.

public int DDLCount
{
    get
    {
        // Try to get an instance of the DDLCount ViewState object
        object temp = ViewState["DDLCount"];
        // If temp is not null then cast it to int and return it,
        // otherwise return 0
        return temp == null ? 0 : (int)temp;
    }
    set { ViewState["DDLCount"] = value; }
}

Next I'll create a method to add the dropdownlists to the page.

private void CreateDropDownLists()
        {
            // This is just to set the count to a default value
            // You may possibly need to generate the count of dropdownlists
            // in some other manner, depending on your project
            if (DDLCount == 0)
                DDLCount = 5;

            
            for (int i = 0; i < DDLCount; i++)
            {
                // Create the dropdownlists
                DropDownList ddl = new DropDownList();
                ddl.ID = "Text_" + i;
                ddl.Items.Add(new ListItem("TestText_" + i, "TestValue_" + i));

                // Add it to the panel
                Panel1.Controls.Add(ddl);
            }
        }

Now that we have a way to create the controls that will always generate the controls in the same order and with the same ID's as they had on previous page loads we can go ahead and place that method in both the page_load and the first button's click event.

protected void Page_Load(object sender, EventArgs e)
        {
            // We don't need to try to load the dropdownlists if
            // this is the first trip to the page or if the
            // count is 0
            if (IsPostBack && DDLCount > 0)
            {
                CreateDropDownLists();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CreateDropDownLists();
        }

And finally we add some code to the second dropdownlist to get the values from each of the dropdownlist controls and add those values to the label.

protected void Button2_Click(object sender, EventArgs e)
        {
            // Cycle through each dropdownlist
            for (int i = 0; i < DDLCount; i++)
            {
                // Find the dropdownlist inside the Panel
                DropDownList ddl = Panel1.FindControl("Text_" + i) as DropDownList;

                // Set the label to the SelectedValue of the dropdownlist
                Label1.Text += ddl.SelectedValue + " : ";
            }
        }

This is a very simplified example and you really should not try to just copy and paste it since it is not designed for your application but it should be enough, hopefully, to get you going in the right direction.

commented: Good knowledge of programming +0

Ok, I'll try to walk through a quick example for you. I've created a page that has two button controls, a Panel and a label. What it's going to do is on the click of the first button there will be 5 dropdownlists added to the panel. On the second button click it will get the values from those dropdownlists and display them all in the label. The first thing I need to do is to have a way to know how many dropdownlists I have created. In order to do this I will create a property that saves that count in viewstate.

public int DDLCount
{
    get
    {
        // Try to get an instance of the DDLCount ViewState object
        object temp = ViewState["DDLCount"];
        // If temp is not null then cast it to int and return it,
        // otherwise return 0
        return temp == null ? 0 : (int)temp;
    }
    set { ViewState["DDLCount"] = value; }
}

Next I'll create a method to add the dropdownlists to the page.

private void CreateDropDownLists()
        {
            // This is just to set the count to a default value
            // You may possibly need to generate the count of dropdownlists
            // in some other manner, depending on your project
            if (DDLCount == 0)
                DDLCount = 5;

            
            for (int i = 0; i < DDLCount; i++)
            {
                // Create the dropdownlists
                DropDownList ddl = new DropDownList();
                ddl.ID = "Text_" + i;
                ddl.Items.Add(new ListItem("TestText_" + i, "TestValue_" + i));

                // Add it to the panel
                Panel1.Controls.Add(ddl);
            }
        }

Now that we have a way to create the controls that will always generate the controls in the same order and with the same ID's as they had on previous page loads we can go ahead and place that method in both the page_load and the first button's click event.

protected void Page_Load(object sender, EventArgs e)
        {
            // We don't need to try to load the dropdownlists if
            // this is the first trip to the page or if the
            // count is 0
            if (IsPostBack && DDLCount > 0)
            {
                CreateDropDownLists();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CreateDropDownLists();
        }

And finally we add some code to the second dropdownlist to get the values from each of the dropdownlist controls and add those values to the label.

protected void Button2_Click(object sender, EventArgs e)
        {
            // Cycle through each dropdownlist
            for (int i = 0; i < DDLCount; i++)
            {
                // Find the dropdownlist inside the Panel
                DropDownList ddl = Panel1.FindControl("Text_" + i) as DropDownList;

                // Set the label to the SelectedValue of the dropdownlist
                Label1.Text += ddl.SelectedValue + " : ";
            }
        }

This is a very simplified example and you really should not try to just copy and paste it since it is not designed for your application but it should be enough, hopefully, to get you going in the right direction.

Thanks sir i have created dynamically dropdownlist and retrived its value.
Sir could please explain me i could understand this code confusion get ,set and viewsate Please describe me

public int DDLCount
{
    get
    {
        // Try to get an instance of the DDLCount ViewState object
        object temp = ViewState["DDLCount"];
        // If temp is not null then cast it to int and return it,
        // otherwise return 0
        return temp == null ? 0 : (int)temp;
    }
    set { ViewState["DDLCount"] = value; }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.