0
ddlSub.Items.Add(new ListItem("--Select --", ""));

gives following error

cannot convert from 'ListItem' to 'string' what is the correct syntax

thanx...

2
  • I think you're missing something here, the Add method supports two overloads - Add(string) and Add(ListItem). I suspect the error is somewhere else in your code. Commented Nov 15, 2009 at 19:47
  • Can you post some more of the code? The line you have referenced above cannot be causing that error. Commented Nov 15, 2009 at 19:54

3 Answers 3

1

Are you sure this is the exact line of code?

I just added a dropdownlist to a webform and then named it the same thing as yours and copy and pasted your line of code into the PageLoad.

// in aspx
<asp:DropDownList ID="ddlSub" runat="server"></asp:DropDownList>

// in .cs codebehind
ddlSub.Items.Add(new ListItem("--Select --", ""));

Compiles just fine. You must be leaving something out.

Sign up to request clarification or add additional context in comments.

1 Comment

hey... thnx for reply.. just missed the System.Web.UI.WebControls in namespaces list...
1

I solved the issue myself by adding System.Web.UI.WebControls to the list of namespaces I was importing.

3 Comments

YOu may want to read the FAQ with StackOverflow. If your question was answered you need to mark it as answered.
Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful.
@kmatyaszek, this answer was from the OP and isn't a 3rd party "thank you" answer =)
0

It's correct. The error must be when you try to read the value out and expect a ListItem or something similar.

Include more code and we can identify the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.