Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//i want this code should be run so that i will get values in dropdownlist which is listbox control //the values are available in database but how i will fetch in this dropdownlist which i have taken //in listbox


C#
protected void gvUserInfo_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
       
      if (e.Item.ItemType == DataControlRowType.DataRow)//error
        {
            con.Open();
            DropDownList ddl = (DropDownList)e.Item.FindControl("ddsponcor");
       
           SqlCommand cmd = new SqlCommand("select sponcorid from Registration_Master", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "sponcorid";
            ddl.DataValueField = "sponcorid";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }

//Error
//Operator '==' cannot be applied to operands of type //'System.Web.UI.WebControls.ListViewItemType' and //'System.Web.UI.WebControls.DataControlRowType'

//so what should i use instead.. to execute my code. please help me...
Posted
Updated 2-Dec-12 3:32am
v2
Comments
[no name] 2-Dec-12 9:49am    
So you want ListBox items in DropDownList ??
bbirajdar 2-Dec-12 13:07pm    
nice catch.. copy paste does not work always..
[no name] 2-Dec-12 23:28pm    
I didn't get you..

control you are using should be datagrid and use following code to compare rows
SQL
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{

}
 
Share this answer
 
Comments
sumit kausalye 3-Dec-12 11:29am    
Control is ListView
[no name] 4-Dec-12 0:09am    
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
C#
foreach (ListItem li in ListBox1.Items)
  {
      DropDownList1.Items.Add(li);
  }
 
Share this answer
 
I cant be sure but in VB we use TYPEOF
so your IF statement would be written as......

if (TypeOf(e.Item) == DataControlRowType.DataRow)


It may not work (as i dont know c#) but theres a chance it may.
 
Share this answer
 
Comments
sumit kausalye 3-Dec-12 1:43am    
no it is not working
Zaf Khan 3-Dec-12 5:08am    
The reason its not working is because yur trying to make an object type comparision the difference being your comparing a DATAROW to a LISTITEMEVNTARG to see if they are the same type, which obviously they're not.

And to clarify with you...
What your saying is that you have a drop down list and a listbox.
The dropdownlist is popultaed from the Database and you wish to populate the list from the dropdownlist, a type of filtering so-to-speak.

I dont think you need to COMPARE the "e as ListViewEventArgs" to a DATAROW, but to simply check that the parameter "e asLIstViewEventArgs") exists.

As Im not sure of C coding i canot tell you how
but if i was doing it in VB then i would write...
<pre>
If ((e is nothing) = False) Then
</pre>

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