Updating gridview with SqlDataSource in asp.net - 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

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!

2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control

Though this seems to be discussed before, I can't apply given help in my case:
I have 2 DropDownLists (DDL1, DDL2) inside the Edit Template of a Gridview, which is bound to a SQLDataSource. DDL2 depends on the selected value of DDL1. If I place the SQLDataSources that populate the DropDownLists on the top level, the ControlID of DDL2's ControlParameter isn't found even though I address it correctly (Gridview$DDL1ControlID). Therefore I placed the SQLDataSources inside the Edit Template of the Gridview. Now both DropDownLists are populated correctly if I enter in Edit Mode for a given Record; if I change the Index for DDL1, DDL2 is not updated automatically, as DDL2 has AutoPostBack set to false. Once I set AutoPostBack to true, the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the " is thrown.
<asp:GridView runat="server" ID="Gridview1"
DataSourceID="Milestones"
DataKeyNames="ID"
OnRowEditing="OnRowEditing"
OnRowDataBound="OnRowDataBoundMS"
OnSelectedIndexChanged="OnSelectedIndexChangedMS">
....
<asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="select function, 2 as ord
from Staff
where function is not null
group by function
union all
select 'select' as function, 0 as ord
union all
select '-----' as function, 1 as ord
order by ord, function">
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwnerGroup" Text='<%# Bind("DefaultOwnerGroup") %>' DataSourceID="OwnerGroups" DataTextField="function" DataValueField="function" Font-Size="12px" Width="80px" AutoPostBack="true"/>
</EditItemTemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="select Name, UserID
from Staff
where function = #OwnerGroup
union all
select Null as Name, Null as UserID
order by Name">
<SelectParameters>
<asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwner" Text='<%# Bind("DefaultOwner") %>' Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
</EditItemTemplate>
</asp:templatefield>
I bind the DropdownLists in the OnRowDataBound Event like this:
<asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroupEdit" Visible="false"/>
<asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="...">
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwnerGroup" DataSourceID="OwnerGroups" DataTextField="funcion" DataValueField="funcion" Font-Size="12px" Width="80px" AutoPostBack="true"/>
</EditItemTemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwnerEdit" Visible="false"/>
<asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="...."
<SelectParameters>
<asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwner" Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
</EditItemTemplate>
</asp:templatefield>
protected void OnRowDataBoundMS(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DDL1 = e.Row.FindControl("DDOwnerGroup") as DropDownList;
DropDownList DDL2 = e.Row.FindControl("DDOwner") as DropDownList;
Label LBL1 = e.Row.FindControl("LabelDefaultOwnerGroupEdit") as Label;
Label LBL2 = e.Row.FindControl("LabelDefaultOwnerEdit") as Label;
DDL1.SelectedValue = Convert.ToString(LBL1.Text);
DDL2.DataBind();
DDL2.SelectedValue = Convert.ToString(LBL2.Text);
}
}
}

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.

Can't get Formview to update, what am I doing wrong?

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">

Resources