Cascading DropDownList in a DetailsView - asp.net

I have two dropdown within a DetailsView and I want to work in cascade, the idea is that an area contains various issues and these issues depend on the selected area, but I get the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
in the next line:
<asp:DropDownList ID="ddlIssue" runat="server"
DataTextField="NameIssue" DataValueField="IdIssue"
The complete code is:
<asp:TemplateField HeaderText="Area" SortExpression="IdArea">
<EditItemTemplate>
<asp:DropDownList ID="ddlArea" runat="server"
DataSourceID="SqlDataSource145711" DataTextField="NameArea"
DataValueField="IdArea" AutoPostBack="true"
AppendDataBoundItems="true" SelectedValue='<%# Bind("IdArea") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource145711" runat="server"
ConnectionString="<%$ ConnectionStrings:BaseDatosCJConnectionString %>"
SelectCommand="SELECT IdArea, NameArea FROM AREA">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server"
Text='<%# Bind("IdArea") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Issue">
<EditItemTemplate>
**<asp:DropDownList ID="ddlIssue" runat="server"
DataTextField="NameIssue" DataValueField="IdIssue"**
DataSourceID="SqlDataSource22" SelectedValue='<%# Bind("IdIssue") %>'
</asp:DropDownList>
<asp:SqlDataSource runat="server"
ID="sqlDataSource22"
ConnectionString="<%$ ConnectionStrings:BaseDatosCJConnectionString %>"
SelectCommand="SELECT IdIssue, NameAIssue, IdArea FROM ISSUE"
FilterExpression="IdArea = '{0}'">
<FilterParameters>
<asp:ControlParameter Name="Param" ControlID="ddlArea"
PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
Can you help with the solution to this error?
I have to do something from the code?
blessings

Don't you want the problematic area to look like this:
<asp:DropDownList ID="ddlIssue" runat="server"
DataTextField="NameIssue" DataValueField="IdIssue"
DataSourceID="SqlDataSource22" SelectedValue='<%# Bind("IdIssue") %'>
Note that apostrophe ' goes before the closing paren.

Related

Dropdown list change based on other dropdown list in details view ASP.NET

I have two dropdown lists in a detailsview one called College and the other is Department. If the user select a college, the department dropdown list should generate all the departments for the selected college.
Here is the detailsview and the dropdown lists:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="600px" AutoGenerateRows="False" CssClass="table table-bordered mtop" DataKeyNames="ID" DataSourceID="SqlDataSource1" OnDataBound="DetailsView1_DataBound">
<FieldHeaderStyle CssClass="DetailsViewHeader" Width="200px" />
<Fields>
<asp:TemplateField HeaderText="College" SortExpression="Colleges">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="ArName" DataValueField="Code"></asp:DropDownList>
<asp:HiddenField ID="HiddenColl" runat="server" value='<%# Eval("Colleges") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Text='<%# Bind("Colleges") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" SortExpression="ArName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ArName" DataValueField="Code"></asp:DropDownList>
<asp:HiddenField ID="HiddenDep" runat="server" value='<%# Eval("ArName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("ArName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:Button ID="btnDelete" runat="server" CausesValidation="False" Text="Delete" OnClientClick="return confirm('Do you want to delete ?');" OnClick="btnDelete_Click" />
</ItemTemplate>
<ControlStyle CssClass="btn-login" />
<ItemStyle CssClass="text-center" />
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Here is the SqlDataSources:
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:UEDConnectionStringMarwaMarwa %>" SelectCommand="SELECT [ArName], [Code] FROM [College]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:UEDConnectionStringMarwaMarwa %>" SelectCommand="SELECT [Code], [ArName] FROM [Department] WHERE ([CollegeCode] = #CollegeCode)">
<SelectParameters>
<asp:ControlParameter ControlID="DetailsView1$DropDownList2" Name="CollegeCode" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
This is the error message:
Could not find control 'DetailsView1$DropDownList2' in
ControlParameter 'CollegeCode'
I did this DetailsView1$DropDownList2 so it can access the dropdown list that is inside the detailsview
What is the problem ?
I am unfamiliar with the DetailsView and not experienced with the SqlDataSource, but I will try to help.
In my experience, I have learned that you shouldn't try to assume a control's ClientID. So, first I would recommend getting the ClientID from the server control.
Using an inline expression, I would try something like this:
<asp:ControlParameter ControlID='<%= DetailsView1.Rows(0).FindControl("DropDownList2").ClientID %>' Name="CollegeCode" PropertyName="SelectedValue" Type="Int32" />
For more about asp.net inline expressions see Introduction to ASP.NET inline expressions in the .NET Framework.
Because I am unfamiliar with the DetailsView control, I don't know if there will be a row at index 0 in the DetailsView when it executes this inline expression (which would throw an exception).
If that code doesn't work, I would suggest dynamically setting the ControlID on the back end that executes after the DetailsView1 rows are bound (such as inside the DetailsView1's DataBound event method).
VB:
If DetailsView1.Rows.Count > 0 Then
Dim objDropDownList2 as Control = DetailsView1.Rows(0).FindControl("DropDownList2")
If objDropDownList2 IsNot Nothing Then
SqlDataSource1.SelectParameters("CollegeCode").ControlID = objDropDownList2.ClientID
End If
End If
C#:
if (DetailsView1.Rows.Count > 0) {
Control objDropDownList2 = DetailsView1.Rows(0).FindControl("DropDownList2");
if (objDropDownList2 != null) {
SqlDataSource1.SelectParameters("CollegeCode").ControlID = objDropDownList2.ClientID;
}
}
I hope this helps!

GridView, SQL and Parameters

I present to you my issues (And i apologize for my english ...):
I have a GridView containing the data of my database, from two SqlDataSource, one for my simple items and the second for a dropdown list.
They are:
<asp:SqlDataSource ID="source_personnes" runat="server"
ConnectionString="<%$ ConnectionStrings:Formation_2014ConnectionString %>"
SelectCommand="SELECT Personnes.id_personne, Personnes.nom_personne, Personnes.prenom_personne, Personnes.actif_personne, Personnes.agence_personne, Organismes.raison_sociale FROM Personnes INNER JOIN Organismes ON Personnes.agence_personne = Organismes.id_organisme WHERE (Personnes.actif_personne = 1)"
UpdateCommand="UPDATE Personnes SET nom_personne=#nom_personne, prenom_personne=#prenom_personne, agence_personne = #id_organisme WHERE (id_personne = #id_personne) "
DeleteCommand="UPDATE Personnes SET actif_personne = 0 WHERE (id_personne = #id_personne)">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="actif_personne" Type="Int16" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name ="id_organisme" ControlId="id_organisme" />
</UpdateParameters>
</asp:SqlDataSource>
The second one :
<asp:SqlDataSource ID="source_organisme" runat="server"
ConnectionString="<%$ ConnectionStrings:Formation_2014ConnectionString %>"
SelectCommand="SELECT * FROM [Organismes]">
</asp:SqlDataSource>
To explain things, I have the following two tables in my database :
Personnes ( id_personne(PK), nom_personne, prenom_personne, actif_personne, #agence_personne)
Organismes ( id_organisme(PK), raison_sociale, actif_organisme)
agence_personne related to id_organisme.
My goal is by the integrated update function of the GridView, change my entry in the database for the table Personnes.
Thanks to Tuto found on MSDN, here or here, I managed to built my dropdown list, however once you get to step 19 to 20 not for me to make a data bindings as explained. So I have a completely independent list.
I now give you the code for my GridView:
<asp:GridView ID="GridModif" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id_personne" DataSourceID="source_personnes" SkinID="dataGrid">
<Columns>
<asp:TemplateField HeaderText="id" SortExpression="id_personne">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id_personne") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id_personne") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="nom_personne" HeaderText="Nom" SortExpression="nom_personne" />
<asp:BoundField DataField="prenom_personne" HeaderText="Prénom" SortExpression="prenom_personne" />
<asp:TemplateField HeaderText="Agence" SortExpression="raison_sociale">
<EditItemTemplate>
<asp:DropDownList ID="id_organisme" runat="server"
DataSourceID="source_organisme" DataTextField="raison_sociale"
DataValueField="id_organisme" AutoPostBack="True">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="id_organisme" runat="server" Text='<%# Bind("raison_sociale") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" DeleteText="Désactiver" />
</Columns>
</asp:GridView>
My ultimate goal: When I click on my "Edit" button, the selected item in my list is the ID from the table Personne, I would select an element from the "Organismes" table and use it in my update parameters to update the "Personnes" table .
I posted that on other french forum, but i have no answer, I hope you could help me more !
-- Edit --
I've forgot to post my error ><
-> System.InvalidOperationException: Can not find control 'id_organisme' in ControlParameter 'id_organisme
Thanks a lot,
Krishnak
I finally found !
So on my DropDown List, I'd Bind a selected value :
<asp:TemplateField HeaderText="Agence" SortExpression="raison_sociale">
<EditItemTemplate>
<asp:DropDownList ID="agence_personne" runat="server"
DataSourceID="source_organisme" DataTextField="raison_sociale"
DataValueField="id_organisme" AutoPostBack="True"
SelectedValue='<%# Bind("agence_personne") %>' >
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="agence_personne" runat="server" Text='<%# Bind("raison_sociale") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And I just remove the Update Parameters in my SqlDataSource. After that, my update works perfectly !

Bind data from SP to textbox thats inside gridview

How do I bind data from a SP to my gridview which all have textboxes for each cell. My asp for is below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Year">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Year") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Period">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Period") %>'></asp:TextBox>
</ItemTemplate>
Use a SQL datasource: Heres an example of mine. I have my connections in it so you would need to substitue yours.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ProjectCISConnectionString %>" ProviderName="<%$ConnectionStrings:ProjectCISConnectionString.ProviderName %>"
<SelectParameters>
<asp:ControlParameter name="Textbox1"
ControlID="txttextbox1" Propertyname="Text"/>
</SelectParameters>
</asp:SqlDataSource>
http://www.aspnettutorials.com/tutorials/database/db-storedprocs-aspnet2-csharp.aspx

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'M'

I am getting this exception at runtime which says it cannot find a column 'M' in datasource, but i am not using 'M' anywhere. I am trying to bind data to dropdownlist inside gridview.
I need to do this in .aspx page rather than code behind.
Here's the code i am using:
<asp:GridView ID="grdDrpDownlistSample" runat="server" AutoGenerateColumns="false" DataSourceID="sqlDS1">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged In Status">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%# Eval("LoggedIn") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:DropDownList ID="drpSex" DataSourceID="sqlDS1" runat="server" DataTextField='<%# Eval("Sex") %>' DataValueField='<%# Eval("id") %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from Users"> </asp:SqlDataSource>
It's working now.
I have got this working by removing Eval from dropdownlist, as it was expecting just the column name not the Eval expression.

Gridview Edit Item using Dropdownlist

I am using ASP.NET Gridview and Oracle 9i data to display in it. I have a joining condition of Zone No from Table Zones . I used the DropdownList to display the Zone Names and returning its value to update in GridView but when I update the grid it display error
'DropDownList1' has a SelectedValue which is invalid because it does
not exist in the list of items. Parameter name: value
I have attached the the Dropdown with DB Table Now My Complete code for tamplate for Zone_no is here
<asp:TemplateField HeaderText="Zone No" SortExpression="ZONE_NO">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="157px" AppendDataBoundItems="True" SelectedValue='<%# BIND("ZONE_NO") %>' DataSourceID="SqlDataSource3" DataTextField="ZONE_DESC" DataValueField="ZONE_NO">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT "ZONE_NO", "ZONE_DESC",NULL FROM "MLK_00_05" ORDER BY "ZONE_DESC", "ZONE_NO"'>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ZONE_NO") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Resources