new to ASP.NET.
I created a sqldatasource and set up basic select query (SELECT * FROM Accounts) using the wizard. I then had the sqldatasource wizard create the INSERT, EDIT and DELETE queries. Connected this datasource to a gridview with EDITING and DELETING enabled. Everything works fine. The SELECT query returns all records and I can edit/delete them.
Now I need to send a parameter to the SELECT command to filter the records to those with the user's id (pulled from Membership.GetUser). When I add this parameter, the SELECT command works fine, but the EDIT/DELETE buttons in the gridview no longer work.
No error is generated. The page refreshes but the records were not updated in the database. I don't understand what is wrong.
CODE:
<%
Dim u As MembershipUser
Dim userid As String
u = Membership.GetUser(User.Identity.Name)
userid = u.ProviderUserKey.ToString
SqlDataSource1.SelectParameters("UserId").DefaultValue = userid
%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
<asp:BoundField DataField="AccountName" HeaderText="AccountName"
SortExpression="AccountName" />
<asp:BoundField DataField="DateAdded" HeaderText="DateAdded"
SortExpression="DateAdded" />
<asp:BoundField DataField="LastModified" HeaderText="LastModified"
SortExpression="LastModified" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CheckingConnectionString %>"
DeleteCommand="DELETE FROM [Accounts] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [Accounts] ([UserId], [AccountName], [DateAdded], [LastModified]) VALUES (#UserId, #AccountName, #DateAdded, #LastModified)"
SelectCommand="SELECT * FROM [Accounts] WHERE [UserId] = #UserId"
UpdateCommand="UPDATE [Accounts] SET [UserId] = #UserId, [AccountName] = #AccountName, [DateAdded] = #DateAdded, [LastModified] = #LastModified WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AccountName" Type="String" />
<asp:Parameter Name="DateAdded" Type="DateTime" />
<asp:Parameter Name="LastModified" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AccountName" Type="String" />
<asp:Parameter Name="DateAdded" Type="DateTime" />
<asp:Parameter Name="LastModified" Type="DateTime" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter Name="UserId"/>
</SelectParameters>
</asp:SqlDataSource>
Have you confirmed that the Records are still in the DB? And that this is not just that the Grid hasn't done a DataBind after the Edit, Update or Delete? That has gotten me more than once before!
Also Look at your Code:
SqlDataSource1.SelectParameters("UserId").DefaultValue = userid
You are setting the SelectParameters... but you need to set the Update, Delete, etc Params too.
Why did you choose to that method .getuser? a simple join using statement in the SQL command line would do the job. You don't need a wizard for that.
Found the answer. I moved the code pulling the membership provider key to a datasource_selecting event.
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
' Get a reference to the currently logged on user
Dim currentUser As MembershipUser = Membership.GetUser()
' Determine the currently logged on user's UserId value
Dim currentUserId As String = currentUser.ProviderUserKey.ToString()
' Assign the currently logged on user's UserId to the #UserId parameter
e.Command.Parameters("#UserId").Value = currentUserId
End Sub
Related
I want to insert a row into an Infragistics grid. It has 2 columns, ID and Name. Both are bound. But when I insert I only want to use the Name column as a parameter. But when I use both, the insertion works. I have the ID as a parameter but I don't use it because it is an identity column. When i only use 'Name', I get the error, too many arguments for the stored procedure.
This is the markup:
<ig:WebHierarchicalDataGrid runat="server" height="600px" width="875px"
ClientIDMode="Static" AutoGenerateBands="False"
AutoGenerateColumns="False" DataKeyFields="ID"
DataMember="SqlDataSource9_DefaultView" StyleSetName="Windows7"
ID="wdgPrivileges"
DataSourceID="WebHierarchicalDataSource7" Key="SqlDataSource9_DefaultView">
<Columns>
<ig:BoundDataField DataFieldName="ID" Key="ID" Hidden="true">
<Header Text="ID" />
<header text="ID" />
</ig:BoundDataField>
<ig:BoundDataField DataFieldName="Name" Key="Name" Width="95%">
<Header Text="Name" />
<header text="Name" />
</ig:BoundDataField>
</Columns>
</ig:WebHierarchicalDataGrid>
<asp:SqlDataSource ID="SqlDataSource13" runat="server"
ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="GetPrivilege" SelectCommandType="StoredProcedure"
UpdateCommand="SetPrivilege" UpdateCommandType="StoredProcedure"
InsertCommand="InsPrivilege" InsertCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
My stored procedure:
PROCEDURE [dbo].[InsPrivilege]
#Name varchar(50)
AS
Begin
INSERT INTO Privilege ([Name], LastUpdate)
VALUES (#Name, GetDate())
end
I need the ID to be bound for the update SQL command.
Do I have to use it as a parameter in the insert also and just not use it?
UPDATE
<ig:WebHierarchicalDataSource ID="WebHierarchicalDataSource7" runat="server">
<DataViews>
<ig:DataView ID="SqlDataSource9_DefaultView" DataMember="DefaultView"
DataSourceID="SqlDataSource13" />
</DataViews>
</ig:WebHierarchicalDataSource>
UPDATE
This is the Update and Select stored procedure info...
<asp:SqlDataSource ID="SqlDataSource13" runat="server"
ConnectionString="<%$ ConnectionStrings %>"
SelectCommand="GetPrivilege" SelectCommandType="StoredProcedure"
UpdateCommand="SetPrivilege" UpdateCommandType="StoredProcedure"
InsertCommand="dmInsPrivilege" InsertCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="Name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Select Stored Procedure:
PROCEDURE [dbo].[GetPrivilege]
AS
Begin
SELECT ID, [Name]
FROM Privilege with (nolock)
end
Update Stored Procedure:
PROCEDURE [dbo].[SetPrivilege]
#ID int,
#Name varchar(50)
AS
Begin
UPDATE Privilege
SET [Name] = #Name, LastUpdate = GetDate()
WHERE ID = #ID
end
Error Message:
[SqlException]: Procedure or function InsPrivilege has too many arguments specified.
I found out the problem...it is not SqlDataSource but the Infragistics Control that requires all bound fields in queries.
So, because my Id field is bound, I have to use it as a parameter in my insert command even though I don't need it; which is kind of weird.
But for anyone else with is problem...I solved it by adding the ID parameter in the insert statement and setting the default value to zero.
My update sql procedure is
ALTER PROCEDURE [dbo].[usp_update]
#ID uniqueidentifier,
#Period nvarchar(6),
#News nvarchar(max),
#Editor nvarchar(30)
as
begin
update table1
set News= #News, Editor=#Editor, LastRevNT=getdate()
where ID=#ID and Period=#Period
end
my asp.net grid view is
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
<Columns>
asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Period" HeaderText="Period" SortExpression="Period" ReadOnly="True"/>
<asp:BoundField DataField="News" HeaderText="News" SortExpression="News" />
<asp:BoundField DataField="Editor" HeaderText="Editor" SortExpression="Editor" ReadOnly="True" />
</Columns>
datasource is
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="usp_select" SelectCommandType="StoredProcedure" UpdateCommand="usp_update" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="ID" SessionField="SelectedID" dbType="guid" />
</SelectParameters>
<UpdateParameters>
<asp:SessionParameter DefaultValue="" Name="ID" SessionField="SelectedID" dbType="guid" />
<asp:SessionParameter DefaultValue="" Name="Editor" SessionField="SelectedEditor" dbType="string"/>
<asp:Parameter Name="Period" Type="String" />
<asp:Parameter Name="News" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Problem is that when I update the table, I need both ID and [Period] to get the unique record.
And in gridview, I don't want to [period] to be modified. When I set the bound for period to be read-only, then I fail to access the value of period, thus fail to update the right record.
I wonder how I can handle the problem?
Thanks for advice!
I added DataKeyNames = "ID,Period" in my aspgridview, and it works ok now. Thanks for advice!
<asp:SqlDataSource ID="HopefulDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand= "SELECT id, regGroupID, amountReceived, other FROM table"
UpdateCommand="UPDATE table
SET [amountReceived] = #amountReceived
WHERE [regGroupID] = #regGroupID">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCourses" Name="ddlSelectedCourse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
other parameters
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
The above works when I change "WHERE [regGroupID] = #regGroupID" to either
WHERE [id] = #id
or
WHERE [regGroupID] = 2
You need to add "regGroupID" to the DataKeyNames collection in your GridView declaration. Something like this:
<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
...
</asp:GridView>
See the DataKeyNames documentation:
You must set the DataKeyNames property in order for the automatic
update and delete features of the GridView control to work. The values
of these key fields are passed to the data source control in order to
specify the row to update or delete.
Note: it's been a while since I used this, so you might need to include both the primary key, and your other key field. Like this:
DataKeyNames="id,regGroupID"
I am trying to update values in my access database, however it does not work in some cases.
The code :
<form id="form1" runat="server">
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Database1.mdb"
DeleteCommand="UPDATE DT_Person SET Deleted = NOW() WHERE (Person_Ref = ?)"
InsertCommand="INSERT INTO [DT_Person] ([Person_Ref], [Created], [Updated], [Deleted], [Person_Name], [Person_No]) VALUES (?, ?, ?, ?, ?, ?)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [DT_Person]"
UpdateCommand="UPDATE DT_Person SET Updated = NOW(), Person_Name = ?, Person_No = ? WHERE (Person_Ref = ?)">
<DeleteParameters>
<asp:Parameter Name="original_Person_Ref" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Created" Type="DateTime" />
<asp:Parameter Name="Updated" Type="DateTime" />
<asp:Parameter Name="Deleted" Type="DateTime" />
<asp:Parameter Name="Person_Name" Type="String" />
<asp:Parameter Name="Person_No" Type="String" />
<asp:Parameter Name="original_Person_Ref" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Person_Ref" Type="Int32" />
<asp:Parameter Name="Created" Type="DateTime" />
<asp:Parameter Name="Updated" Type="DateTime" />
<asp:Parameter Name="Deleted" Type="DateTime" />
<asp:Parameter Name="Person_Name" Type="String" />
<asp:Parameter Name="Person_No" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="Person_Ref" DataSourceID="AccessDataSource1">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="Person_Ref" HeaderText="Person_Ref"
InsertVisible="False" ReadOnly="True" SortExpression="Person_Ref" />
<asp:BoundField DataField="Created" HeaderText="Created"
SortExpression="Created" />
<asp:BoundField DataField="Updated" HeaderText="Updated"
SortExpression="Updated" />
<asp:BoundField DataField="Deleted" HeaderText="Deleted"
SortExpression="Deleted" />
<asp:BoundField DataField="Person_Name" HeaderText="Person_Name"
SortExpression="Person_Name" />
<asp:BoundField DataField="Person_No" HeaderText="Person_No"
SortExpression="Person_No" />
</Columns>
As you can see, the delete command
(DeleteCommand="UPDATE DT_Person SET Deleted = NOW() WHERE (Person_Ref = ?)" )
is really an update statement and this works fine . However the actual update statement does not work on the web page. The weird thing is, if you test the query in the 'query builder' it works fine.
Really need some help here.
TIA
P.S. Im using Visual Studio 2008 and using the data source builder each time
Windows 7 (if that's what you're using) has been known to make a copy of Access databases down in the Debug files and use the copy for all data changes. You might want to check that the database you are actually accessing is the copy you think it is.
Do a search for files with the same name across your whole development machine. You might be surprised what turns up.
i am trying to bind an sqldatasource from the code by passing variable from another page, but when i run the report i am getting 0 records,
, my code as fllow
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WorkflowConnStr %>"
SelectCommand="SELECT * FROM tbl_ServiceTracking WHERE (ActorUID = #strUserId) AND (RequestedDate >= #DateFrom) AND (RequestedDate <= #DateTo)">
<SelectParameters>
<asp:Parameter Name="strUserId" />
<asp:Parameter Name="DateFrom" />
<asp:Parameter Name="DateTo" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" />
c# code
string strUserId = Request.QueryString["userid"];
string DateFrom = Request.QueryString["dtfrom"];
string DateTo = Request.QueryString["dtto"];
SqlDataSource1.SelectParameters.Add("strUserId", strUserId);
SqlDataSource1.SelectParameters.Add("DateFrom", DateFrom);
SqlDataSource1.SelectParameters.Add("DateTo", DateTo);
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
did i miss something?
I solved this problem by changing my code
SqlDataSource1.SelectParameters["strUserId"].DefaultValue = strUserId;
SqlDataSource1.SelectParameters["DateFrom"].DefaultValue = DateFrom;
SqlDataSource1.SelectParameters["DateTo"].DefaultValue = DateTo;
You can also set select parameter in design mode:
<SelectParameters>
<asp:QueryStringParameter Name="UserID" QueryStringField="userid"
Type="String" DefaultValue="userid" />
<asp:QueryStringParameter DefaultValue="dtfrom" Name="dtfrom"
QueryStringField="dtfrom" Type="String" />
<asp:QueryStringParameter DefaultValue="dtto" Name="dtto"
QueryStringField="dtto" Type="String" />
</SelectParameters>