Home » C# ProgrammingRSS

drop down list

hi all...
i have done a simple drop down list...and it's working good...
i take "name' from table student...

separately i write coding for split string into multiple...here's my problem.Now i want to add that split into drop down list to split data into drop down list...

please let me know how to add that method into drop down list..Here's the codes i wrote..

 

<code>

//Connection to database and column to split***********************************
protected void Page_Load(object sender, EventArgs e)
{


String strConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\jeeva\\My Documents\\Visual Studio 2008\\WebSites\\WebSite2\\App_Data\\tempdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

String strQueryString = "select * from student";

using (SqlConnection sqlConnection = new SqlConnection(strConnection))
{
SqlCommand sqlCommand = new SqlCommand(strQueryString, sqlConnection);
sqlConnection.Open();

SqlDataReader reader = sqlCommand.ExecuteReader();


while (reader.Read())
{
dropDownList.Items.Add(reader["name"].ToString());
}



}



}


//method for split data*********************************
private void splitData (String strData,ref ArrayLit alData)
{
String strTemp = null;
int nCount = 0;

alData = new ArrayList();

foreach (char ch in strData)
{
if (ch != ' ')
strTemp = strTemp + ch;

else if (ch == ' ')
{
if (!String.IsNullOrEmpty(strTemp))
{
alData.Add(strTemp);
strTemp = null;
nCount++;
}
}
}
if (strTemp != null)
alData.Add(strTemp);

}
</code>

 

 

2 Answers Found

 

Answer 1

Welcome to the MSDN Forums.

Actually, .NET Framework already has the needed code for spliting. Let's see how:

foreach(object o in dropDownList.Items)
{
 string[] splitted = o.ToString().Split(newchar[]{' '});//split by ' '(space)foreach(string s in splitted)
 dropDownList2.Items.Add(s);//add them to another drop  down
}

Hope this helps.

Sincerely,

Yasser

 

Answer 2

if i do the connection using sql data  source...where i can add  this coding...

i try to add this codes  into page load area and it's nt working..

please guide me..

 

 

[code]

<form id="form1" runat="server">
    <div>
   
        <asp:DropDownList ID="dropDownList" runat="server"
            DataSourceID="SqlDataSource1" DataTextField="Value" DataValueField="Value"
            onselectedindexchanged="dropDownList_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
            AutoPostBack="True" Height="34px"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="111px">
        </asp:DropDownList>
   
        <br />
        <br />
   
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:AED_Panasonic_DataConnectionString %>"
        SelectCommand="SELECT [Value] FROM [UDFList]"></asp:SqlDataSource>
       
       
    </form>

[code]

 
 
Search for  drop down list
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter