UpdateCommand with function - asp.net

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

Related

Stop asp:SqlDataSource column from displaying '00:00:00' next to my date

I have an asp.net web application and on one page I have an asp:GridView which is populated with the following code:
<asp:SqlDataSource ID="SqlAdminTasks" runat="server" ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" SelectCommand="GetAdminTasks" SelectCommandType="StoredProcedure" InsertCommand="INSERT INTO [Admin_TaskList] ([Description], [Checkbox_Radio_Value], [Action_For], [Location], [Additional_Info], [Date_Submitted], [Submitted_By], [Status]) VALUES (#Description, #Checkbox_Radio_Value, #Action_For, #Location, #Additional_Info, #Date_Submitted, #Submitted_By, #Status)">
<InsertParameters>
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="Action_For" Type="String" />
<asp:Parameter Name="Checkbox_Radio_Value" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="Additional_Info" Type="String" />
<asp:Parameter Name="Date_Submitted" Type="DateTime" />
<asp:Parameter Name="Submitted_By" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
When I populate my SQL db, the date is displayed as '2016-01-04' so does not display a time, but when I hit the page my grid displays as '04-01-2016 00:00:00'. I don't want the timestamp to be displayed but I cant figure out how to stop it as all the grid is all rendered in my .aspx file.
My SQL db is working fine, the issue is the code above.
The reason I do not want the timestamp is because the page has limited space so I want to keep column widths, small as possible and the timestamp has no relevance to the user anyway.
In your grid view datetime column you need to set the DataFormatString to {0:d}
This will format the datetime to only show the date

how to validate input in list views?

this is my list view
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Year"
DataSourceID="AccountValueFundsDataSource" InsertItemPosition="LastItem">
........
</asp:ListView>
This is my data source
<asp:SqlDataSource ID="AccountValueFundsDataSource" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:Life_InsuranceConnectionString %>"
DeleteCommand="DELETE FROM [AccountValueFunds] WHERE [Year] = #original_Year AND [avfcharge] = #original_avfcharge"
InsertCommand="INSERT INTO [AccountValueFunds] ([Year], [avfcharge]) VALUES (#Year, #avfcharge)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [AccountValueFunds]"
UpdateCommand="UPDATE [AccountValueFunds] SET [avfcharge] = #avfcharge WHERE [Year] = #original_Year AND [avfcharge] = #original_avfcharge">
<DeleteParameters>
<asp:Parameter Name="original_Year" Type="Int32" />
<asp:Parameter Name="original_avfcharge" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Year" Type="Int32" />
<asp:Parameter Name="avfcharge" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="avfcharge" Type="Decimal" />
<asp:Parameter Name="original_Year" Type="Int32" />
<asp:Parameter Name="original_avfcharge" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
my question is how to validate input? example if a users enters a string where the type of parameter is decimal or integer??
You can handle that validation with RegularExpressionValidator - MSDN Article, RegularExpressionValidator - W3School.
You need the ValidationExpression to allow decimals and integers but not allow strings.
Check these
Accept only (0-9) integer and one decimal point(decimal point is also optional).
Input for Numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
Regular Expression Validator - Numbers only no letters no spaces

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)

Adding ControlParameter to SqlDataSource prevents query and databinding?

I have a SqlDataSource that calls a stored procedure and it works fine. If I add a <ControlParameter> tag to add an additional argument, then the query never fires and the databinding never occurs. Suggestions?
This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
When I add the ControlParameter, the databinding no longer occurs:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
<asp:ControlParameter Name="SprocArgName" ControlID="ddlFilter" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
The ControlParameter refers to a valid object on the page. Any other suggestions?
Most likely one of the parameter is empty or null. Add CancelSelectOnNullParameter="false" to the asp:SqlDataSource and ConvertEmptyStringToNull="true" to both parameters. Once it works, tweak the parameters so that SP gets what it expects.

ASP VB .NET SqlDataSource Update Error Fails

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?

Resources