I am trying to pass a parameter to dynamically populate drop down list. I am not able to pass the parameter from my .aspx file. Here is the grid view and related ObjectDataSource. In srcLocation, the Company paramter being passed is blank or null, if I put default value (hardcoding what is expected), the page display fine when I click on edit button. But with no default value it gives above error.
<asp:GridView
id="grdCAPAs"
DataSourceID="srcCAPAs"
DataKeyNames="CAPA_Number"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
AutoGenerateColumns="false"
CssClass="products"
GridLines="none"
Runat="server" >
<Columns>
<asp:BoundField
DataField="CAPA_Number"
ReadOnly="true"
HeaderText="CAPA Number" />
<asp:TemplateField HeaderText="Company" >
<ItemTemplate>
<%#Eval("Company", "{0}")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
ID = "ddlCompany"
DataSourceID = "srcCompany"
DataTextField="C9_Company"
DataValueField = "C9_Company"
SelectedValue = '<%# Bind("Company", "{0}") %>'
Runat = "server"
Autopostback = "true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" >
<ItemTemplate>
<%#Eval("Location", "{0}")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
ID = "ddlLocation"
DataSourceID = "srcLocation"
DataTextField="C9_Location"
DataValueField = "C9_Location"
SelectedValue = '<%# Bind("Location", "{0}") %>'
Runat = "server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField
DataField="Area"
HeaderText="Area" />
<asp:ObjectDataSource
id="srcCAPAs"
TypeName="CAPAs.BLL.MaintainCAPA"
SelectMethod="SelectAll"
UpdateMethod="Update"
InsertMethod="Insert"
DeleteMethod="Delete"
Runat="server" />
<asp:ObjectDataSource
id = "srcCompany"
TypeName="CAPAs.BLL.MaintainCAPA"
SelectMethod="SelectCompany"
Runat = "server" />
<asp:ObjectDataSource
id = "srcLocation"
TypeName="CAPAs.BLL.MaintainCAPA"
SelectMethod="SelectLocation"
Runat = "server" >
<SelectParameters>
<asp:ControlParameter ControlID="grdCAPAs" Name = "Company" PropertyName ="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
Public Shared Function SelectLocation(ByVal Company As String)
Dim daLayer As DAL1 = New DAL1()
Return daLayer.SelectAllLocation(Company)
End Function
Public Function SelectAllLocation(ByVal Company As String) As SqlDataReader
'Create Connection
Dim con As New SqlConnection(_connectionString)
'Create Command
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "SELECT DISTINCT C9_LOCATION FROM CAPA_LOCATION WHERE C9_COMPANY = @Company"
'Add parameters
cmd.Parameters.AddWithValue("@Company", Company)
'Return DataReader
con.Open()
Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Let me know what you think