Click here to Skip to main content
15,878,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is successful the first time you enter a number, but if you remove that number again and enter it is adding.
Like first input 4 in textbox and enter. Remove that number textbox input 6 and enter.
It "adding 4+6" 4 al right plus new 6 more will display means totally 10.
C#
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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack && DDLCount > 0)
        {
            CreateDropDownLists();
        }

    }
    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; }
    }
    private void CreateDropDownLists()
    {
        int a = Convert.ToInt32(TextBox4.Text);
        // 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 = a;   //i   have  to  ask  HOw  DDLCount  is  0
        // I  can  not   understand  it 


        for (int i = 0; i < DDLCount; i++)
        {
            // Create the dropdownlists
            DropDownList ddl = new DropDownList();
            DropDownList dd2 = new DropDownList();
            ddl.ID = "Text" + i;
            dd2.ID = "Texts" + i;
            LiteralControl l1 = new LiteralControl("<br></br>");

            //ddl.Items.Add(new ListItem("TestText_" + i, "TestValue_" + i));
            //ddl.Items.Add("anant");
            //ddl.Items.Add("pratibha");
            //ddl.Items.Add("rahul");
            SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con1.Open();
            string st = "select  doe_name from   Doe_detail ";
            SqlCommand cmd = new SqlCommand(st, con1);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
               ddl.Items.Add(dr.GetString(0).ToString());


            }
            con1.Close();

            SqlConnection con2 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con2.Open();
            string std = "select  phone from   Doe_detail ";
            SqlCommand cmd1 = new SqlCommand(std, con2);
            SqlDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                dd2.Items.Add(dr1.GetString(0).ToString());


            }
            con2.Close();
            
            // Add it to the panel
            Panel3.Controls.Add(ddl);
            Panel3.Controls.Add(dd2);

        }
    }



   
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

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

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

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            value = ddl.SelectedValue.ToString();
            DropDownList dd2 = Panel3.FindControl("Texts" + i) as DropDownList;

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            ids = dd2.SelectedValue.ToString();
            SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");

            con.Open();

            SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "','"+ids+"')", con);
            cmd1.ExecuteNonQuery();
            con.Close();
        }
    }
}
Posted
Updated 1-Nov-11 6:09am
v2
Comments
hzawary 1-Nov-11 14:42pm    
Where is your question?

1 solution

Hello friend what is your question....

How to create a dynamic drop down list..

But in your existing code where you got error or what you want please specify that thing instaed of giving your whole code...
 
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