Passing parameter from gridview to sqldatasource error - c#

I'm using gridview with edit/delete. When the user clicks on "Edit" I need to pass a value from one column (100mPLot) to a datasource that would populate a dropdownlist of another column (SubPlot). This is what I have:
<asp:BoundField DataField="100mPlot" HeaderText="100m plot"
SortExpression="100mPlot" ReadOnly="True" />
<asp:TemplateField HeaderText="SubPlot" SortExpression="SubPlot">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="dsSubPlotNames"
DataTextField="SiteName" DataValueField="SiteID"
SelectedValue='<%# Bind("SiteID") %>'
>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSubPlotNames" runat="server"
ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand='exec [339_PPM].usp_SubPlotNames_Select null, #100mPlotName;'
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue='<%# Eval("100mPlot") '
Name="100mPlotName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Unfortunately, I get this error with the Eval("100mPlot"):
Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ControlParameter does not have a DataBinding event.
How do I fix this? Thanks,
EDIT:
I moved it to code behind and handled it in RowDataBound after creating a hidden label tag containing the value of the previous column.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
Label lbl100mPlot = (Label)e.Row.FindControl("lbl100mPlot");
SqlDataSource dsPlotNames = (SqlDataSource)e.Row.FindControl("dsSubPlotNames");
dsPlotNames.SelectParameters.Clear();
dsPlotNames.SelectParameters.Add("100mPlotSiteID", null);
dsPlotNames.SelectParameters.Add("100mPlotName", lbl100mPlot.Text);
}
}
My next problem is this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Source Error:
[No relevant source lines]
I'm not sure what's causing it.

I dont think you can do it this way. You can handle RowEditing event in code behind and manually adjust your SqlDataSource to display correct data. Also, move SqlDataSoruce out of your grid.

Related

my ASP.NET GridView does not bind correctly

I have an ASP.NET GridView supposed to exec a SQL Query.
The query is never executed so I strongly suspect there is a binding issue somewhere.
Here is the aspx code:
<asp:SqlDataSource ID="i_sdsGv" runat="server" ConnectionString="<%$ ConnectionStrings:XXX %>"
SelectCommand="INCOHERENT QUERY IN ORDER TO TRIGGER AN EXCEPTION"
SelectCommandType="Text"
<SelectParameters>
<asp:QueryStringParameter Name="MyTableId" QueryStringField="MyTableId" />
</SelectParameters>
</asp:SqlDataSource>
...
<asp:GridView ID="i_gv" runat="server" AutoGenerateColumns="False" DataKeyNames="MyTableId"
DataSourceID="i_sdsGv" OnRowDataBound="i_gvOptionChoix_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="i_tbName" runat="server" Text='<%# Bind("MyTableName") %>' />
</ItemTemplate>
</Columns>
<EmptyDataTemplate>
NO ROWS TO DISPLAY
</EmptyDataTemplate>
</asp:GridView>
I am 100% sure that if the query string is inexact, an exception is triggered.
In my case, no exception is triggered so the query is not executed.
I get the NO ROWS TO DISPLAY text.
The only obvious binding that I can think about is between the DataSource and the GridView. It does look OK to me.
So I would suspect an issue with :
DataKeyNames="MyTableId" ?
My i_gvOptionChoix_RowDataBound method is empty at the moment but I think it does not matter at this time?
What am I missing so the app actually attempts to execute the query?
Thx in advance for your kind assistance

How to populate a textbox based on the selected value of a dropdown list in an asp.net c# web forms application?

I have a dropdown list (doc_rvw_sub_recip_list) that is populated with names from tbl_ad_users. tbl_ad_users contains a field titled "domain_user". I would like to populate an asp:hiddenfield (hdn_domain_user) with the "domain_user" based on the selected value in the doc_rvw_sub_recip_list dropdown list. For testing I used a dropdown list (domain_user_ddl) instead of a hidden field, and it works as needed. But I don't know how to get the value into a hidden field as opposed to using a dropdown list.
The following is doc_rvw_sub_recip_list:
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
The following is the sql data source for domain_user_ddl:
<asp:SqlDataSource ID="sdc_domain_user_ddl" runat="server" ConnectionString='<%$ ConnectionStrings:idrfConnectionString %>' SelectCommand="SELECT [domain_user] FROM [tbl_ad_users] WHERE ([email_address] = #email_address)">
<SelectParameters>
<asp:ControlParameter ControlID="doc_rvw_sub_recip_list" PropertyName="SelectedValue" Name="email_address" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
The following is domain_user_ddl:
<asp:DropDownList ID="domain_user_ddl" runat="server" DataSourceID="sdc_domain_user_ddl" DataTextField="domain_user" DataValueField="domain_user" AutoPostBack="true"></asp:DropDownList>
How do I get this to work for hdn_domain_user just as it works for domain_user_ddl?
ASPX
Add a OnSelectedIndexChanged event to your dropdown...
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="doc_rvw_sub_recip_list_SelectedIndexChanged"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
Add your hidden field...
<asp:HiddenField runat="server" ID="hdn_domain_user" />
Code Behind
Handle the SelectedIndexChanged event...
protected void doc_rvw_sub_recip_list_SelectedIndexChanged(Object sender, EventArgs e)
{
// Populate the hidden field if the dropdown has a selected value
if (doc_rvw_sub_recip_list.SelectedValue != null)
hdn_domain_user.Value = doc_rvw_sub_recip_list.SelectedValue;
}

Error when changing selectedValue in dropdown

I have 2 dropdown menus which are populated using tableAdapters, the list_model dropdown menu is fully dependent on the list_make dropdown menu as it determines which models to show depending on which make has been clicked on, here is the dropdown menus:
<asp:DropDownList ID="list_make" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="makeODS" DataTextField="make" DataValueField="ID" onselectedindexchanged="list_make_SelectedIndexChanged" SelectedValue='<%# Bind("makeID") %>'></asp:DropDownList>
<asp:ObjectDataSource ID="makeODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="getMakes" TypeName="list_dataTableAdapters.MakesTableAdapter"></asp:ObjectDataSource>
<asp:DropDownList ID="list_model" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="modelODS" DataTextField="model" DataValueField="ID" SelectedValue='<%# Bind("modelID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="modelODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetModelsByMakeID" TypeName="list_dataTableAdapters.ModelsTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="list_make" Name="make_id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Note that these dropdown lists are in my EditItemTemplate and when the edit button is clicked the SelectedValue is correctly retrieved however when I select a new item in the list_make I get this error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
Does anyone know why I am getting this error?
Also if it helps I have this code in the list_make_SelectedIndexChanged event:
protected void list_make_SelectedIndexChanged(object sender, EventArgs e)
{
var listView = listview_car.Items[0].FindControl("list_model") as DropDownList;
listView.DataBind();
}

GridView row update and DropList

I have a gridview with column that have droplist when enter edit mode:
<asp:TemplateField HeaderText="genre" SortExpression="genre">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now i want to take it to UpdateParameters of SqlDataSource:
<UpdateParameters>
<asp:ControlParameter ControlID="DropDownList2" Type="string" PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
But when i pressed he give me Error msg
Could not find control 'DropDownList2' in ControlParameter 'genre'.
Any idea why?
DropDownList2 is a nested control located under the Grid; therefore, your SqlDataSource control does not have visibility at all of the DropDownList2.
You could try assigning the value on the code behind by using the Updating event:
protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#genre"].Value = GetDropDownListValue();
}
Note: You will need to use FindControl("DropDownList2") in GetDropDownListValue()

Value is not found in boundfield in gridview?

I am trying to assing value to hiddenfield on indexchange event of dropdownlist ! Actually the problem is when I am trying to update my record i can not find value of that hidden field ! Kindly give me solution or suggest any another option ! Thank You !
My grid view is
<asp:TemplateField HeaderText="LocCode" SortExpression="LocCode">
<EditItemTemplate>
<ajax:UpdatePanel ID="upEditsLocation" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlLocation" runat="server"
DataSourceID="sdsLocation"
OnDataBound="ddlLocation_DataBound"
DataValueField="LocCode" AppendDataBoundItems="false"
DataTextField="LocCode"
AutoPostBack="true"
onselectedindexchanged="ddlLocation_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ccConnString %>"
ProviderName="<%$ ConnectionStrings:CCConnString.ProviderName %>" SelectCommand="Select LocCode from Location">
</asp:SqlDataSource>
</ContentTemplate>
</ajax:UpdatePanel>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%# Bind("LocCode") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
and my indexchange event is
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = ddlLocation.SelectedItem.Text;
}
And my hidden field is
<asp:HiddenField ID="hdloc" runat="server" />
From the code I can see the HiddenField is not part of your update panel. Hence if you assign any value to it, it will not reflect on client machine. Increase the scope of panel to include the hidden field, and then try.
OR you can try this solution from ASP.net Forum
Here is a small tutorial on update panel (MSDN)
Hope this helps you.
GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)cancel.FindControl("lblid");
If you can't access hdloc from code behind, either is not added by Visual Studio on aspx.designer.cs (try delete it and add it back or change the id and then back to original value) or the hidden field is placed in other template of another binding control, which means you need to use ctrl.FindControl("hdloc") then cast to HiddenField.
Also you need to place this hidden field into an UpdatePanel with UpdateMode="Always".
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = (sender as DropDownList).SelectedItem.Text;
}
I'm sure that ddlLocation.SelectedItem.Text, like you use it, it gives a compilation error, because ddlLocation is not visible on code behind, since is inside of EditItemTemplate.

Categories