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 !
Related
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!
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
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
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.
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.