DeleteCommand not working in asp.net 2.0 - asp.net

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.

Related

Search button not working on deployed web application but works locally on testing machine

I have a gridview on my aspx page with 2 textboxes (Firstname and Lastname) that allows the users to filter the gridview when they click on the "Search" button.
When I run this locally on my dev machine it works perfect. Once I deployed it to my IIS server...when a user clicks on the search button nothing happens. I tried to open the page on several differnt computers and the same thing happens on all of them. I noticed when I load the page from the IIS server itself the search button works. Any idea what could cause this?
Below is the code for my textboxes, button and filter parameter:
<!DOCTYPE html>
<p>
Last Name: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br /><br />First Name:
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="Search" />
<asp:GridView ID="TDetails" DataSourceID="SqlDataSource1" DataKeyNames="PersonID" AutoGenerateColumns="false" runat="server" OnSelectedIndexChanged="Details_SelectedIndexChanged" AllowPaging="True" PageSize="7" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
connectionString="Data Source=Server\SQLEXPRESS;Initial Catalog=Test;persist security info=True;user id=user;password=password"
SelectCommand="SELECT * FROM Person ORDER BY LastName"
FilterExpression="[LastName] LIKE '{0}%' AND [FirstName] LIKE '{1}%'">
<FilterParameters>
<asp:ControlParameter Name="LastName" ControlID="TextBox3" PropertyName="Text" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter Name="FirstName" ControlID="TextBox4" PropertyName="Text" ConvertEmptyStringToNull="false"/>
</FilterParameters>
</asp:SqlDataSource>

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 update data corruption when database changed during edit

I'm having a little problem with data corrupting when updating tables from a GridView. I must admit this is my first attempt at asp.net but I am reasonably familiar with C#.
Firstly, some background. We have some 3rd party inventory/order processing software that stores its data in MSSQL. I am using a GridView and an sql query to extract current work orders and display them on a table. The sql query also combines another table in a database I created (tblProdStatus) that adds additional status information about the work order.
The web page allows editing so that the status of the work order can be updated and these changes need to be written back to the table (tblProdStatus). The asp.GridView has the UpdateCommand defined with appropriate parameters. I've also defined the DataKeyNames to identify the row being updated.
Everything works as expected, I can Edit a row, update fields, select Update and have the information correctly written to the table (tblProdStatus).
My problem is when the underlying data is changed in the 3rd party software while the edit is in progress. The data ends up getting associated with the wrong record, either shifted up or down a row depending if the 3rd software added or deleted a row.
The 3rd party software has an AllocID key that is associated with one work order, and does not change for the duration. I link this to the same number stored in my table (tblProdStatus) to tie the two tables together. When the corruption occurs the sql update is passed the wrong AllocID for the row being edited. It's almost as if the table is refreshed when the Update link is clicked and then data is submitted afterwards using the wrong row.
There is no real code behind for the logic, it's all in the aspx file. Which I have posted, and will hopefully not be too embarrassing for myself.
I use stackoverflow a lot and always seem to be able to find guidance from others in my problems, so this is my first post of an actual question. Many thanks in advance for any advice.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="prodStatus.aspx.cs" Inherits="ProdStatus.prodStatus" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvProdStatus" runat="server" AutoGenerateColumns="False" DataKeyNames="AllocID"
DataSourceID="SqlDS" onrowdatabound="gvProdStatus_RowDataBound"
CssClass="table" EnableViewState="False" >
<Columns>
<asp:BoundField DataField="AllocID" HeaderText="AllocID" InsertVisible="False"
ReadOnly="True" SortExpression="AllocID" Visible="false" />
<asp:BoundField DataField="AllocWONo" HeaderText="Works Order"
ReadOnly="True" SortExpression="Works Order" />
<asp:BoundField DataField="ShortNm" HeaderText="Cust"
ReadOnly="True" SortExpression="Cust" />
<asp:BoundField DataField="CustPONo" HeaderText="Cust PO"
ReadOnly="True" SortExpression="Cust PO" />
<asp:BoundField DataField="MasterPNo" HeaderText="Part Number"
ReadOnly="True" SortExpression="Part Number" />
<asp:BoundField DataField="ItemDescription" HeaderText="Description"
ReadOnly="True" SortExpression="Description" />
<asp:BoundField DataField="AllocQty" HeaderText="Qty"
ReadOnly="True" SortExpression="Qty" DataFormatString="{0:G29}" />
<asp:BoundField DataField="ReqdDate" HeaderText="Due"
ReadOnly="True" SortExpression="Due" DataFormatString="{0:d/MMM/yy}" />
<asp:TemplateField HeaderText="Done" SortExpression="state0">
<ItemTemplate>
<asp:CheckBox ID="state0" runat="server" Enabled="false"
Checked='<%#Eval("state0") %>'
Visible='<%#Eval("mask0") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state0" runat="server"
Checked='<%#Bind("state0") %>'
Visible='<%#Eval("mask0") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/lfp.jpg' alt='Laser Faceplate' />" SortExpression="state1">
<ItemTemplate>
<asp:CheckBox ID="state1" runat="server" Enabled="false"
Checked='<%#Eval("state1") %>'
Visible='<%#Eval("mask1") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state1" runat="server"
Checked='<%#Bind("state1") %>'
Visible='<%#Eval("mask1") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/lcv.jpg' alt='Laser Cover' />" SortExpression="state2">
<ItemTemplate>
<asp:CheckBox ID="state2" runat="server" Enabled="false"
Checked='<%#Eval("state2") %>'
Visible='<%#Eval("mask2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state2" runat="server"
Checked='<%#Bind("state2") %>'
Visible='<%#Eval("mask2") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/hdw.jpg' alt='Hardware Pack' />" SortExpression="state3">
<ItemTemplate>
<asp:CheckBox ID="state3" runat="server" Enabled="false"
Checked='<%#Eval("state3") %>'
Visible='<%#Eval("mask3") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state3" runat="server"
Checked='<%#Bind("state3") %>'
Visible='<%#Eval("mask3") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/str.jpg' alt='Strike Plate' />" SortExpression="state4">
<ItemTemplate>
<asp:CheckBox ID="state4" runat="server" Enabled="false"
Checked='<%#Eval("state4") %>'
Visible='<%#Eval("mask4") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state4" runat="server"
Checked='<%#Bind("state4") %>'
Visible='<%#Eval("mask4") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/box.jpg' alt='Box' />" SortExpression="state5">
<ItemTemplate>
<asp:CheckBox ID="state5" runat="server" Enabled="false"
Checked='<%#Eval("state5") %>'
Visible='<%#Eval("mask5") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state5" runat="server"
Checked='<%#Bind("state5") %>'
Visible='<%#Eval("mask5") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/ins.jpg' alt='Instructions' />" SortExpression="state6">
<ItemTemplate>
<asp:CheckBox ID="state6" runat="server" Enabled="false"
Checked='<%#Eval("state6") %>'
Visible='<%#Eval("mask6") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state6" runat="server"
Checked='<%#Bind("state6") %>'
Visible='<%#Eval("mask6") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<img src='./images/pcb.jpg' alt='PCBs' />" SortExpression="state7">
<ItemTemplate>
<asp:CheckBox ID="state7" runat="server" Enabled="false"
Checked='<%#Eval("state7") %>'
Visible='<%#Eval("mask7") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="state7" runat="server"
Checked='<%#Bind("state7") %>'
Visible='<%#Eval("mask7") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Note" SortExpression="Note">
<ItemTemplate>
<asp:Label ID="notes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="notes" runat="server" Text='<%# Bind("notes") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:worksOrdersConnectionString %>"
SelectCommand = "SELECT [AllocID], [tblCustOrders].[CustORID], [tblStockItems].[ItemID], [ReqdDate], [AllocWONo], [ShortNm], [tblAlloc].[AllocQty], [tblCustOrders].[CustPONo], [MasterPNo], [ItemDescription],
ISNULL([mask0], 0) as [mask0],
ISNULL([mask1], 0) as [mask1],
ISNULL([mask2], 0) as [mask2],
ISNULL([mask3], 0) as [mask3],
ISNULL([mask4], 0) as [mask4],
ISNULL([mask5], 0) as [mask5],
ISNULL([mask6], 0) as [mask6],
ISNULL([mask7], 0) as [mask7],
ISNULL([state0], 0) as [state0],
ISNULL([state1], 0) as [state1],
ISNULL([state2], 0) as [state2],
ISNULL([state3], 0) as [state3],
ISNULL([state4], 0) as [state4],
ISNULL([state5], 0) as [state5],
ISNULL([state6], 0) as [state6],
ISNULL([state7], 0) as [state7],
ISNULL([notes], '') as [notes]
FROM (((((([miniMrpDB].[dbo].[tblAlloc]
INNER JOIN [miniMrpDB].[dbo].[tblSalesOrderDetail] ON [tblSalesOrderDetail].[RowID] = [tblAlloc].[SalesRowID])
INNER JOIN [miniMrpDB].[dbo].[tblStockItems] ON [tblStockItems].[ItemID] = [tblSalesOrderDetail].[StockID])
INNER JOIN [miniMrpDB].[dbo].[tblCustOrders] ON [tblCustOrders].[CustORID] = [tblAlloc].[CustORID])
INNER JOIN [miniMrpDB].[dbo].[tblCusAddresses] ON [tblCustOrders].[CustID] = [tblCusAddresses].[AddID])
LEFT JOIN [worksOrders].[dbo].[tblItemMaskBits] ON [tblStockItems].[ItemID] = [tblItemMaskBits].[ItemID])
LEFT JOIN [worksOrders].[dbo].[tblProdStatus] ON [AllocID] = [tblAllocID])
WHERE (Status IS NULL OR Status < 3) AND ([tblAlloc].[CustORID] > -1) AND ([AllocID] < 999999999) ORDER BY [ReqdDate], [ShortNm], [CustPONo], [AllocWONo]"
UpdateCommand = "BEGIN TRAN
IF EXISTS (SELECT * from [worksOrders].[dbo].[tblProdStatus] WITH (updlock,serializable) WHERE [tblAllocID] = (#AllocID))
BEGIN
UPDATE [worksOrders].[dbo].[tblProdStatus] SET
[state0] = (#State0),
[state1] = (#State1),
[state2] = (#State2),
[state3] = (#State3),
[state4] = (#State4),
[state5] = (#State5),
[state6] = (#State6),
[state7] = (#State7),
[notes] = (#Notes)
WHERE [tblAllocID] = (#AllocID)
END
ELSE
BEGIN
INSERT [worksOrders].[dbo].[tblProdStatus] ([tblAllocID], [state0], [state1], [state2], [state3], [state4], [state5], [state6], [state7], [notes])
VALUES ((#AllocID), (#State0), (#State1), (#State2), (#State3), (#State4), (#State5), (#State6), (#State7), (#Notes))
END
COMMIT TRAN" >
<UpdateParameters>
<asp:Parameter Type="Int32" Name="AllocID"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State0"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State1"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State2"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State3"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State4"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State5"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State6"></asp:Parameter>
<asp:Parameter Type="Boolean" Name="State7"></asp:Parameter>
<asp:Parameter Type="String" Name="Notes"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
</div>
<br />
<div>
<asp:Button ID="btnReload" runat="server" Text="Reload"
onclick="btnReload_Click" />
<a id="countdown"></p>
</div>
<script>
<!--
(function countdown(remaining) {
if (remaining === 0)
location.reload(true);
document.getElementById('countdown').innerHTML = remaining;
setTimeout(function () { countdown(remaining - 1); }, 1000);
})(600);
//-->
</script>
</form>
</body>
</html>

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

Unable to get the Datakey value of a selected row in GridView for use in FormView

I have a gridview like this :
<asp:MultiView ID="MvCustomer" runat="server" ActiveViewIndex="0" >
<%--View 1 to List the customers--%>
<asp:View ID="VwCustomersList" runat="server" >
<asp:GridView ID="GvListCustomer" runat="server" AutoGenerateColumns="False"
HorizontalAlign="Center" DataSourceID="OdsGvCustomers" DataKeyNames="CUSNUM"
EnableModelValidation="True" onrowcommand="GvListCustomer_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LblCUSNUM" runat="server" Text='<%#Eval("CUSNUM") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LblCO_NAM" runat="server" Text='<%#Eval("CO_NAM") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LblCUSCTY" runat="server" Text='<%#Eval("CUSCTY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:CommandField ButtonType="Button" SelectText="Edit" ShowSelectButton="true" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnSelect" runat="server" Text="Edit" CommandArgument='<%#Eval("CUSNUM")%>' CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server" Text="Delete" CommandArgument='<%#Eval("CUSNUM")%>' CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="OdsGvCustomers" runat="server"
SelectMethod="GetAllCustomers" TypeName="MultiView_EF.BLL.Customers_BLL">
</asp:ObjectDataSource>
</asp:View>
<%--View 2 to show customer details--%>
<asp:View ID="VwCustomerDetail" runat="server" >
<asp:FormView ID="FvCustomerDetails" runat="server" HorizontalAlign="Center"
DataSourceID="OdsFvCustomerDetails" EnableModelValidation="True" DefaultMode="Edit" >
<EditItemTemplate>
CUSNUM:
<asp:TextBox ID="CUSNUMTextBox" runat="server" Text='<%# Bind("CUSNUM") %>' />
<br />
CO_NAM:
<asp:TextBox ID="CO_NAMTextBox" runat="server" Text='<%# Bind("CO_NAM") %>' />
<br />
CUSCTY:
<asp:TextBox ID="CUSCTYTextBox" runat="server" Text='<%# Bind("CUSCTY") %>' />
<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>
<InsertItemTemplate>
CUSNUM:
<asp:TextBox ID="CUSNUMTextBox" runat="server" Text='<%# Bind("CUSNUM") %>' />
<br />
CO_NAM:
<asp:TextBox ID="CO_NAMTextBox" runat="server" Text='<%# Bind("CO_NAM") %>' />
<br />
CUSCTY:
<asp:TextBox ID="CUSCTYTextBox" runat="server" Text='<%# Bind("CUSCTY") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<%--<ItemTemplate>
CUSNUM:
<asp:Label ID="CUSNUMLabel" runat="server" Text='<%# Bind("CUSNUM") %>' />
<br />
CO_NAM:
<asp:Label ID="CO_NAMLabel" runat="server" Text='<%# Bind("CO_NAM") %>' />
<br />
CUSCTY:
<asp:Label ID="CUSCTYLabel" runat="server" Text='<%# Bind("CUSCTY") %>' />
<br />
</ItemTemplate>--%>
</asp:FormView>
<asp:ObjectDataSource ID="OdsFvCustomerDetails" runat="server"
SelectMethod="GetCustomerByCusnum" TypeName="MultiView_EF.BLL.Customers_BLL">
<SelectParameters>
<asp:ControlParameter ControlID="GvListCustomer" Name="cusnum"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:View>
</asp:MultiView>
My idea is that when the user clicks "BtnSelect" I will change the View to the view containing the FormView which has a select method configured to take the SelectedValue of the GridView as an input parameter - it will show the details of the selected customer.
I have done this before "n" number of times but I am not able to get it working this time. The trouble is that when the call for the Select Method of the formview goes to the relevant function - "GetCustomerByCusnum" I have a null value in its parameter "cusnum".
I know that I can write a selecting event and using CommandArgument, parse the value of the selected row and pass it into the Select method as a value but I dont want that solution. I know it works without the "Selecting" method but I cant recall how.
Please help.
By looking at your code, all seems ok, just a single hint, if you are not setting the active view, do it by SetActiveView method of multiview, please do that in button click.
Another thing you can do is: add OdsFvCustomerDetails_Selecting and OdsFvCustomerDetails_Selected events. in Selecting you can see the params and values passed and in Selected you can see e.Exception if there is any error in the query. so it will give you more idea about what exactly is going wrong.
Additional note
Another thing that should be noted is, multivew control works in a way that it binds all the views regardless of whichever is active so it degrades the performance. You may probably find better idea
Yikes. A relic of a question - IIRC I did away with the MV approach for this as we moved list and details section to different aspx pages and used query string parameters. Never got to fix what must have been, in hindsight, some missing parameter assignment.

Resources