Gridview Edit Item using Dropdownlist - asp.net

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>

Related

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.

Cascading DropDownList in a DetailsView

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.

<%# Bind([ReqPo!ItemId])%> doesnt work

In Dynamics ax Enterprise Portal I have created a templatefield in AxGridView. Seems everything ok, but when i try to enter some value to this textbox (manually or through lookup), it doesnt bind to ReqPo!ItemId field. Checked that with info(strfmt("%1", ReqPo.ItemId))); in validateWrite method on ReqPo dataset - it prints nothing; What i'm missing?
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="<%$ AxLabel:#SYS12836 %>" Visible="true">
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBoxFilterItemId" CssClass="AxInputField"
Columns="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.DisplayLength %>"
Enabled="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.AllowEdit %>"
MaxLength="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.StringSize %>"
Text='<%# Bind("[ReqPo!ItemId]") %>'>
</asp:TextBox>
<dynamics:AxLookup
ID="AxLookup3" runat="server" OnLookup="Item_lookup" TargetControlId="TextBoxFilterItemId"
CssClass="AxLookupButtonBF" HoverCssClass="AxLookupButtonHoverBF" ShowFilter="True">
</dynamics:AxLookup>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemIdLabel" runat="server" Text='<%# Bind("[ReqPo!ItemId]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I assume you are using the ItemId field from the table ReqPo. If you locate that field in the Data Dictionary you will discover that the field does not allow editing.
As AxGridView only supports editing, which leads me to the conclusion that you are trying the break the data integrity rules of the application.
if this is the case, it is obviously not possible.

Resources