<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"
Related
How do you set a parameter passed to a SqlDataSource to be null? I have the code below where the parameter "#var1" is set from a TextBox. However, if that value is not defined I need to set "#var1" to be null so that the coalesce returns true.
Is there a way to do this or am I going about it the wrong way?
I've also tried setting the DefaultValue="null" and dsABC.SelectParameters["#var1"]=null which both lead to "Input string was not in a correct format" errors.
<asp:SqlDataSource ID="dsABC" runat="server" ConnectionString="<%$ ConnectionStrings:abc123 %>"
SelectCommand=" SELECT col1, col2, col3
FROM table1 as T1
WHERE COALESCE(#var1, T.ID) = T.ID">
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Add the ConvertEmptyStringToNull and the CancelSelectOnNullParameter attributes. The first sets the parameter to null when empty, the second lets the query run (default behavior is to not run a query with a null parameter).
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16"
ConvertEmptyStringToNull="true"
CancelSelectOnNullParameter="false" />
</SelectParameters>
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.
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
I'm having problems passing a null value to a stored procedure, e.g. if an option isn't selected on a dropdown how can this be passed as null to through the sqdatasource to the database.
I currently have a number of dropdown controls on my page which are holding paramater values to get passed to a stored procedure.
When I select some values from these controls the gridview will display the results that it should, but what I'm having a problem with is when no values are selected is passing a null value to the SP. I've checked the SP and when I execute it and pass in null values it gets me the correct results so I'm happy with the SP. I've tried
ConvertEmptyStringToNull="true" DefaultValue=""
settings in the control paramater with no luck, and the dropdown's "ALL" option has a value of ""
The code for the sqldatasource is:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:scConnString %>"
SelectCommand="spGetOrgTickets" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:SessionParameter Name="org_id" Type="Int32" SessionField="org_id" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="drpPriority" Name="priority_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue="" />
<asp:ControlParameter ControlID="drpStatus" Name="status_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue=""/>
</SelectParameters>
</asp:SqlDataSource>
One of the dropdown's is :
<asp:DropDownList Style="width: 100%" ID="drpStatus" runat="server" class="field select"
AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2_Status"
DataTextField="status" DataValueField="status_id">
<asp:ListItem Value="">All</asp:ListItem>
</asp:DropDownList>
Any ideas?
Thanks
Sorted...just need CancelSelectOnNullParameter="false" within sqldatasource.
In VS 2005, using VB, page has a FormView linked to an SqlDataSource. When data is changed and Update button pressed, changed data is cleared in FormView but database table is not updated. Below is the SqlDataSource code. Any ideas why the Update doesn't work?
<asp:SqlDataSource ID="SqlDataDetails" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ALFSConnectionString %>"
...
...
OldValuesParameterFormatString="original_{0}"
ProviderName="<%$ ConnectionStrings:ALFSConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [Resident] WHERE ([Resident_ID] = ?)"
UpdateCommand="UPDATE [Resident] SET [Resident_Company_ID] = ?, ..., [Resident_Diet] = ?, [Resident_Social_Security] = ? WHERE [Resident_ID] = ?" >
<UpdateParameters>
<asp:SessionParameter Name="Resident_ID" SessionField="Resident_ID" Type="String" />
<asp:Parameter Name="Resident_Company_ID" Type="Int32" /> ...
...
...
<asp:Parameter Name="original_Resident_Diet" Type="String" />
<asp:Parameter Name="original_Resident_Social_Security" Type="Int32" />
</UpdateParameters>
...
...
<SelectParameters>
<asp:SessionParameter Name="Resident_ID" SessionField="Resident_ID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx
What is your DataSourceMode, and/or are you calling .Update() if applicable?