SqlDataSource insert error - too many arguments - asp.net

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.

Related

asp.net fail to access value when set read-only to true

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!

UpdateCommand with function

I have an UpdateCommand for a grid as follows:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KruSQL %>"
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID">
<UpdateParameters>
<asp:Parameter Name="RoomNum" Type="String" />
<asp:Parameter Name="WaterFixure" Type="String" />
<asp:Parameter Name="CollDate" Type="DateTime" />
</UpdateParameters>
When the user enters a date I need to convert that to UTC. Is there a way to do this from the UpdateCommand - add a function. It didn't work when I tried it.
you could write a stored procedure and use that as your UpdateCommand ... inside the stored procedure you could convert the date to utc

Gridview delete/edit not working when using select parameter

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

Why isn't my SqlDataSource's UpdateCommand working?

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

Update Gridview Error - "oldValues is empty"

So I'm trying to update my GridView, and I get this error:
You have specified that your update command compares all values on
SqlDataSource 'SqlDataSource1', but the dictionary passed in for
oldValues is empty. Pass in a valid dictionary for update or change
your mode to OverwriteChanges.
Here is the SQL Data Source in question:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT field1, field2, field3, field4, field5, field6
FROM table
ORDER BY field4"
UpdateCommand="UPDATE table2
SET field2 = #field2 , field3 = #field3, field4 = #field4
WHERE (field1 = #original_field1) AND (field6 = #original_field6)"
FilterExpression="field1 LIKE '{0}'">
<FilterParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</FilterParameters>
<SelectParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="field2" Type="String" />
<asp:Parameter Name="field3" Type="String" />
<asp:Parameter Name="field4" Type="Int32" />
<asp:Parameter Name="original_field1" Type="String" />
<asp:Parameter Name="original_field5" Type="Int32" />
<asp:Parameter Name="original_field2" Type="String" />
<asp:Parameter Name="original_field3" Type="String" />
<asp:Parameter Name="original_field4" Type="Int32" />
<asp:Parameter Name="original_field6" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Now I understand one way to get around this is to change 'CompareAllValues' to 'OverwriteChanges', however I have never needed to do this before (I have other GridViews on the page that update just fine) and it never seems to update the data when I DO try anyway.
Is it the nature of the data that would cause this Data Source to act differently to the rest? Or have I done something stupid?
Thanks! :)
I discovered the answer! The data field 'field6' was not included in my Gridview as a field.
Once I added it as a column in the GridView I was able to make it work :)
(Can also add it as a DataKeyName)

Resources