Can't get Formview to update, what am I doing wrong? - asp.net

I'm messing around with a Formview in asp.net. I know that the UpdateCommand needs to be specified so that the Formview knows what SQL query to run.
Do I need to write an event in addition to the code the Formview sets up in order to properly fire off the SQL query?
In addition, I don't get a SQL error or anything, it just does not bind back to the database.
<EditItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel1" runat="server"
Text='<%# Eval("ProductID") %>' />
<br />
ProductName:
<asp:TextBox ID="ProductNameTextBox" runat="server"
Text='<%# Bind("ProductName") %>' />
<br />
UnitPrice:
<asp:TextBox ID="UnitPriceTextBox" runat="server"
Text='<%# Bind("UnitPrice") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>

Make sure you set the DataKeys property (usually to the column(s) that are your primary key). If you want more help, you'll need to post your code.

I was having similar problem, then I studied MSDN and it said use UpdateCommand with SqlDataSource, and it worked.
Here is my code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT DISTINCT * FROM [employees] WHERE ([username] = #username)"
UpdateCommand="UPDATE [employees] SET first_name = #first_name WHERE username = #username">

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 !

DeleteCommand not working in asp.net 2.0

I have the following SqlDataSource:
<asp:SqlDataSource ID="LDetails" runat="server" ConnectionString="<%$ ConnectionStrings:NormCon %>"
SelectCommand ="SELECT l.Link_ID,l.Link_Name,l.Link_Path,l.Link_Desc
FROM HyperLinks l
WHERE l.link_id = #Link_ID;"
UpdateCommand ="UPDATE Hyperlinks SET Link_Name = #Link_Name, Link_Path = #Link_Path, Link_Desc = #Link_Desc WHERE Link_ID = #Link_ID;"
DeleteCommand ="DELETE FROM Hyperlinks_Groups WHERE Join_Link = #Link_ID; DELETE FROM Hyperlinks WHERE Link_ID = #Link_ID;"
<SelectParameters>
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Link_Name" />
<asp:Parameter Name="Link_Path" />
<asp:Parameter Name="Link_Desc" />
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</DeleteParameters>
</asp:SqlDataSource>
this is the detailsview I have:
<asp:DetailsView ID="LinkDetails" runat="server" DataSourceID="LDetails"
DataKeyNames="Link_ID" AutoGenerateRows="False" OnItemDeleted="deleteLinkReload" >
<Fields>
<asp:BoundField DataField="Link_ID" />
<asp:TemplateField HeaderText="Link Name">
<ItemTemplate>
<asp:Label ID="lblLinkName" runat="server" Text='<%# Bind("Link_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkName" runat="server" Text='<%# Bind("Link_Name") %>' MaxLength="50" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="lnkLinkPath" runat="server" Text='<%# Bind("Link_Path") %>' NavigateUrl='<%# Bind("Link_Path") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkActual" runat="server" Text='<%# Bind("Link_Path") %>' MaxLength="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:Label ID="lblLinkComment" runat="server" Text='<%# Bind("Link_Desc") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkComment" runat="server" Text='<%# Bind("Link_Desc") %>' MaxLength="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="linkDetailsEdit" runat="server" CommandName="edit" Text="Edit" />
<asp:LinkButton ID="linkDetailsDelete" runat="server" OnClientClick="return confirm('OK to delete link?');" CommandName="Delete" Text="Delete Link" /> <%--OnClick="DeleteLink" --%>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="linkDetailsAccept" runat="server" CommandName="update" Text="Accept" />
<asp:LinkButton ID="linkDetailsCancel" runat="server" CommandName="Cancel" CausesValidation="false" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
When I click a delete button in the detailsview which is linked to this, the page refreshes but the item is not deleted. I think the DeleteCommand is returning 0 rows or erroring without me being able to catch the error. I've tried seeing if its a case of the DeleteCommand even firing by switching the SQL to this:
DeleteCommand ="INSERT INTO mis_test (string) VALUES ('Hi');"
and it still didn't do anything. Any ideas?
Try using SQL Server Profiler to see what is run on the database, and what error it gives.
I would bet that your connection string includes things like User Instance=true and AttachDbFileName=..., and that after you run the delete command, you are connecting via SSMS or Visual Studio (not your app) and checking the table.
If this is the case, then you need to stop using this User Instance feature (which has been deprecated). What this does is create a copy of your database for each instance of any application that connects.
What you need to do is properly attach your database to a real instance of SQL Server, and connect to that copy of the database from all applications.

Updating gridview with SqlDataSource in asp.net

i want to update the records from the gridview using SqlDataSource, here is what i am doing.
below is my gridview markup
<asp:GridView ID="grdManageFaculties" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="LocalServerDataSource" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
Width="100%" OnRowUpdating="grdManageFaculties_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="MANAGE">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMAIL">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditEmail" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOBILE">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditMobileNumber" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LOCKED">
<ItemTemplate>
<asp:CheckBox ID="chkIsLocked" runat="server" Enabled="false" Checked='<%# Eval("Locked") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkEditIsLocked" runat="server" Checked='<%# Bind("Locked") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED">
<ItemTemplate>
<asp:Label ID="lblCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
below is my markup for the SqlDataSource
<asp:SqlDataSource ID="LocalServerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
SelectCommand="users_GetAllUsers" SelectCommandType="StoredProcedure" UpdateCommand="users_UpdateFaculty" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="EMAIL" Type="String" />
<asp:Parameter Name="ISLOCKEDOUT" Type="Boolean" />
<asp:Parameter Name="MOBILENUMBER" Type="Int64" />
<asp:Parameter Name="USERNAME" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
below is my code-behind for the Row_Updating function
protected void grdManageFaculties_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox email = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditEmail") as TextBox;
Label username = grdManageFaculties.Rows[e.RowIndex].FindControl("lblEditUserName") as Label;
CheckBox locked = grdManageFaculties.Rows[e.RowIndex].FindControl("chkEditIsLocked") as CheckBox;
TextBox mobilenumber = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditMobileNumber") as TextBox;
LocalServerDataSource.UpdateParameters["EMAIL"].DefaultValue = email.Text;
LocalServerDataSource.UpdateParameters["ISLOCKEDOUT"].DefaultValue = locked.Checked.ToString();
LocalServerDataSource.UpdateParameters["MOBILENUMBER"].DefaultValue = mobilenumber.Text;
LocalServerDataSource.UpdateParameters["USERNAME"].DefaultValue = username.Text;
LocalServerDataSource.Update();
}
catch { }
}
below is my Stored Procedure for Update
ALTER PROCEDURE users_UpdateFaculty
#EMAIL NVARCHAR(100),
#ISLOCKEDOUT BIT,
#MOBILENUMBER BIGINT,
#USERNAME nvarchar(100)
AS
BEGIN
UPDATE aspnet_Users SET MOBILENUMBER=#MOBILENUMBER where USERNAME=#USERNAME
UPDATE ASPNET_MEMBERSHIP SET EMAIL = #EMAIL, LOWEREDEMAIL = LOWER(#EMAIL), ISLOCKEDOUT=#ISLOCKEDOUT WHERE USERID = (SELECT USERID FROM ASPNET_USERS WHERE USERNAME=#USERNAME)
END
my records in database is getting updated, but when i click on update button, i gets the below error:
Procedure or function users_UpdateFaculty has too many arguments specified.
Can anyone help me what could be causing this issue, i am using all the parameters properly.
Found the solution:
The Select Columns and the Update Parameters should match in order to update using SqlDataSource, that means if you select(query or procedure) is returning 3 fields in gridview, then all of them should be the parameter for the update, you can miss out the actual updations in database if not required, but the <UpdateParameters> should have all the fields: say for example if below is my select query
SELECT USERNAME, MOBILENUMBER, EMAIL FROM USERS
then the update parameters should be
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="MobileNumber" Type="Int64" />
<asp:Parameter Name="Email" Type="String" />
<UpdateParameters>
you cannot skip any of the parameters, even though you do not intend to update that field
Hope this will help others, as i wasted lots of time researching on this.
I dont think your solution is OK, since I have to use the atribute OldValuesParameterFormatString="original_{0}" that means that I would have doble parameters, the ones with the original values and the ones with edited values. So there is no way to match the order.
I have 4 parameters, Im getting the right values for 2 of them, an null at the others.
I tried your solution and did not work. Thanks anyway.
Read this:
The order of the parameters in the UpdateParameters collection might be important, depending on the ADO.NET provider. The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order that the parameters appear in the parameterized SQL query. The System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with a placeholder alias in the SQL query. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updateparameters(v=vs.110).aspx

FormView.FindControl works for one form but not the other on the same page

I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)

Resources