update gridview form dropdownlist

I have ddl in gridview that is bound separately than the rest of the gridview. I want to update the sql database based on the ddl, but I get the error, "Could not find control 'ddlUsers' in ControlParameter ''."

Public Sub BindGridView()
    'bind gridview
    SqlDataSource1.SelectCommand = "Select * From Leadership Left Outer Join Customer On Leadership.CustomerId = Customer.CustomerId"
    SqlDataSource1.DataBind()

    'binding dropdownlist inside of gridview
    SqlDataSource2.SelectCommand = "SELECT LName + ', ' + FName As Name, CustomerId From Customer Where Active = 'True' And Probation = 'False' Order by LName ASC"
    SqlDataSource2.DataBind()

End Sub

Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
    Dim ddl As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlUsers"), DropDownList)

    Dim conStr As String = "Data Source=mydatasource"
    Dim cn As New SqlConnection(conStr)
    cn.Open()
    Dim updCmd As New SqlCommand("updateLeadership", cn)
    updCmd.CommandType = CommandType.StoredProcedure
    updCmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = ddl.SelectedValue.ToString()
    updCmd.Parameters.Add("@Position", SqlDbType.NVarChar).Value = ddl.SelectedItem.ToString()

    SqlDataSource1.DataBind()
    updCmd.Parameters.Clear()
    bindGridView()
End Sub

Open in new window

<asp:GridView id="GridView1" runat="server" OnRowUpdating="GridView1_RowUpdating" DataSourceID="SqlDataSource1" DataKeyNames="Id">              
             <Columns>
                    <asp:TemplateField>
                        <HeaderStyle Width="5%" />
                        <ItemStyle Width="5%" />
                        <EditItemTemplate>
                            <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
                          <asp:LinkButton ID="btnUpdate" Runat="server" CommandName="Update" OnClientClick="return confirm('Are you sure you're sure you want to update this position?');">Update</asp:LinkButton>

                        </EditItemTemplate>

                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick="return confirm('Are you sure you want to update this position?');"
                                CommandName="Edit">change</asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>     
                    <asp:TemplateField HeaderText="Position" >

                        <ItemTemplate>
                            <asp:Label runat="server" ID="President" Text ='<%# Eval("Position")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                   <asp:TemplateField HeaderText="Member Name" >
                        <EditItemTemplate>
                            <asp:dropdownlist id="ddlUsers" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true" DataTextField="Name" DataValueField="CustomerId" AutoPostBack="true"></asp:dropdownlist>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblFirstVice" Text ='<%# Eval("FName") & " " & Eval("LName")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            </asp:GridView> 

        asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NCOAConnectionString %>" UpdateCommand="UpdateLeadership" UpdateCommandType="StoredProcedure">                   
            <UpdateParameters>
                <asp:controlparameter controlid="ddlUsers" propertyname="SelectedValue" Type="Int32" />

            </UpdateParameters>    
        </asp:SqlDataSource>

        <asp:SqlDataSource id="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NCOAConnectionString %>">                   

    </asp:SqlDataSource>

Open in new window

mlg74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Have you checked the value of e.Rowindex at runtime? Maybe something is interfering with the RowUpdate, or it may be firing more than once.

Do you have anything else in your code that is rebinding the grid?
0
sammySeltzerCommented:
That's because your ddlUsers to associated with sqldatasource2 instead of sqldatasource1.

Look at this:

        asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NCOAConnectionString %>" UpdateCommand="UpdateLeadership" UpdateCommandType="StoredProcedure">                  
            <UpdateParameters>
                <asp:controlparameter controlid="ddlUsers" propertyname="SelectedValue" Type="Int32" />

            </UpdateParameters>    
        </asp:SqlDataSource>


                        <EditItemTemplate>
                            <asp:dropdownlist id="ddlUsers" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true" DataTextField="Name" DataValueField="CustomerId" AutoPostBack="true"></asp:dropdownlist>
                        </EditItemTemplate>
0
mlg74Author Commented:
@sammyseltzer: That is because the dropdownlist is populated from a different query. updating the table has nothing to do with how each is populated when gridview is bound.

@carl_tawn: there is nothing else in code rebinding gridview
0
guru_samiCommented:
You have added the control parameter in the sqlDatasource's markup:
<asp:controlparameter controlid="ddlUsers" propertyname="SelectedValue" Type="Int32" />

The ddlUsers is inside of the GV and won't be accessible by SqlDS. So either remove it all together. And since you are adding the parameter explicitly in GridView1_RowUpdating it should work.

Or
Instead of ControlParameter you can use the Parameter to declare it in UpdateParamters and then set the value in GridView1_RowUpdating.
 <asp:Parameter Name="SomeName" Type="String" />

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlg74Author Commented:
I took the dropdownlist out of the gridview and got it to work. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.