0

I was debugging and I got the strange error listed above. It was weird because my Dropdownlist is called DropDownlist1. When I set it equal to that earlier, the debugger told me it could not cast it to a string.

So here is my C#, where the problem is.

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       Gridsource.ConnectionString = "Data Source=CATALOGSERVER;Initial Catalog=UACOrders;Persist Security Info=True;User ID=Catalog;Password=pass";
        Gridsource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        Gridsource.SelectCommand = "GetOrderHistory";
        Gridsource.DataSourceMode = SqlDataSourceMode.DataSet;
        GridView1.DataSource = Gridsource;

        ControlParameter cp = new ControlParameter();
        cp.ControlID = DropDownList1.ClientID;
        cp.Name = "Company";
        cp.PropertyName = "SelectedValue";
        cp.Type = System.TypeCode.String;
        Gridsource.SelectParameters.Add(cp);
        Gridsource.Page = this;
        this.Controls.Add(Gridsource);
        GridView1.DataBind();
        updatepanel1.Update();
 }

And here is the small bit of asp.net involved

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="contentarea">     
    <asp:ScriptManager runat="server"></asp:ScriptManager>
    <asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" ID="updatepanel1">
        <ContentTemplate>

            <div>
                <asp:Label Text="Company: " runat="server"></asp:Label>
                <asp:DropDownList ID="DropDownList1" runat="server"
                     DataSourceID="company" DataTextField="COMPANY" DataValueField="COMPANY" 
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                     AutoPostBack="true" >
                </asp:DropDownList>
                <asp:SqlDataSource ID="company"
                     runat="server" ConnectionString="<%$ ConnectionStrings:UACOrdersConnectionString %>"
                     SelectCommand="SELECT [Customer] AS [COMPANY] FROM [Accounts] ORDER BY [Customer]">
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="Gridsource" runat="server"></asp:SqlDataSource>

So oddly enough I can get this sort of thing working in the asp.net code normally. But I can't seem to get it to work in the C# codebehind which this particular webpage requires. So again, I need to find out what cp.ControlID shoudld really be equal to.

The specific Exception is "InvalidOperationException was unhandled by user code." "Could not find control 'DropDownList1' in ControlParameter 'Company'."

VS putting the Exception at line 39 which is

         GridView1.DataBind();

If you need to see more code just leave a comment and I will post more.

Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61

2 Answers2

1

Based on your updated code in your question, I can see that the problem is that DropDownList and the SqlDataSource (to which this ControlParameter belongs) are not in the same Naming Container. You have a couple of options:

  1. Add the SqlDataSource to your markup, instead of creating only in code-behind, and then adding it late in the Page lifecylce. This is the simplest option.
  2. Add the SqlDataSource to the Page.Controls collection programatically early in the Page lifecycle. Like during the Init event.

Like this:

protected void Page_Init(object sender, EventArgs e)
{
    Page.Controls.Add(Gridsource);
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • That changes the problem, but does not fix it. The new error is Could not find control 'MainContent_DropDownList1' in ControlParameter 'Company'. I think you are close. Maybe something else is wrong too. Do you need to see more of my code? – Alexander Ryan Baggett Jul 03 '13 at 19:09
  • @AlexanderRyanBaggett Yes, I do. Where is the SqlDataSource "Gridsource" in relation to "DropDownList1"? – Josh Darnell Jul 03 '13 at 19:10
  • Gridsource1 is right before the pageload like so SqlDataSource Gridsource= new SqlDataSource(); protected void Page_Load(object sender, EventArgs e) { } – Alexander Ryan Baggett Jul 03 '13 at 19:13
  • 1
    @Alex Ah, that's the problem. They are not in the same naming container (because it's not in the Page's Controls collection), so the cp can't find DropDownList1. Is there a reason you don't just have the SqlDataSource in the markup after your DropDownList? – Josh Darnell Jul 03 '13 at 19:15
  • There is, I have another sqldatasource that is bound to the Dropdown right below it. I suppose I could have it there too. – Alexander Ryan Baggett Jul 03 '13 at 19:17
  • @AlexanderRyanBaggett It will make your life a lot easier =) If you don't want it in the markup, you can *try* adding the SqlDataSource to the Page's controls collection during Page_Init. That might be enough to get it where it needs to be to have visibility to the DropDownList. – Josh Darnell Jul 03 '13 at 19:20
  • Hmm, still doesn't work, but I will update my code in the description – Alexander Ryan Baggett Jul 03 '13 at 19:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32843/discussion-between-alexander-ryan-baggett-and-jadarnel27) – Alexander Ryan Baggett Jul 03 '13 at 19:27
  • @AlexanderRyanBaggett You need to have the control parameter in the markup as well to you use that approach. – Josh Darnell Jul 03 '13 at 19:27
0

Alexander: why are you giving the cp.ControlID = "DropDownList1";, DropDownList1 is a object of DropDownList so you can not pass this name as string, And cp.Type = System.TypeCode.String; is string type so cp can not contains object.

If you want to add control into cp then cp.ControlId=DropDownList1.ClientID;

cmant
  • 11
  • 1
  • 3