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
Related
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 am updating the table in database by entering some fields in same page containing INPUT fields and Grid. but grid which is connected to that table is not refreshing after submit the value. I have to open that page in new tab to view the changes. Also i want those input fields empty after submit.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:con %>"
ProviderName="<%$ ConnectionStrings:con.ProviderName %>"
SelectCommand="SELECT * FROM [View_Intakesheet]"
InsertCommand="INSERT INTO [View_Intakesheet] [date_withdrawn], [date_delivered], [qc_by], [qc_date], [additional_notes], [Case_Status_Text], [Case_Type_Text]) VALUES (#date_withdrawn, #date_delivered, #qc_by, #qc_date, #additional_notes, #Case_Status_Text, #Case_Type_Text)"
UpdateCommand="UPDATE intakesheet SET date_withdrawn= #date_withdrawn, date_delivered= #date_delivered, qc_by = #qc_by, qc_date=#qc_date, additional_notes = #additional_notes WHERE (case_number = #case_number)">
<InsertParameters>
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="Case_Status_Text" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="assigned_to" SessionField="Name" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="case_status" />
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="case_number" />
<asp:Parameter Name="case_type" />
</UpdateParameters>
</asp:SqlDataSource>
You can do it by rebinding the gridview like this just after where your update is done.
GridViewIs.DataBind()
You will have to re-bind the grid once you are done with changes. Please let us know if you are doing that.
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)
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.
I have used Visual Studio 2008 ASP.NET AccessDataSource Wizard to generate the update command. The gridview edit button works. But when I click on Update Link in Gridview, I get this message:
System.Data.OleDb.OleDbException: No value given for one or more required parameters
Is the wizard bugged ?
This is the source code it generated:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="Id"
DataSourceID="AccessDataSource1" PageSize="1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="TaskName" HeaderText="TaskName"
SortExpression="TaskName" />
<asp:BoundField DataField="TaskDescription" HeaderText="TaskDescription"
SortExpression="TaskDescription" />
<asp:BoundField DataField="TaskPriority" HeaderText="TaskPriority"
SortExpression="TaskPriority" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
ConflictDetection="CompareAllValues" DataFile="~/App_Data/tasks.mdb"
DeleteCommand="DELETE FROM [Tasks] WHERE [Id] = ? AND (([TaskName] = ?) OR ([TaskName] IS NULL AND ? IS NULL)) AND (([TaskDescription] = ?) OR ([TaskDescription] IS NULL AND ? IS NULL)) AND (([TaskPriority] = ?) OR ([TaskPriority] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [Tasks] ([Id], [TaskName], [TaskDescription], [TaskPriority]) VALUES (?, ?, ?, ?)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [Tasks]"
UpdateCommand="UPDATE [Tasks] SET [TaskName] = ?, [TaskDescription] = ?, [TaskPriority] = ? WHERE [Id] = ? AND (([TaskName] = ?) OR ([TaskName] IS NULL AND ? IS NULL)) AND (([TaskDescription] = ?) OR ([TaskDescription] IS NULL AND ? IS NULL)) AND (([TaskPriority] = ?) OR ([TaskPriority] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_TaskName" Type="String" />
<asp:Parameter Name="original_TaskDescription" Type="String" />
<asp:Parameter Name="original_TaskPriority" Type="Single" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="TaskName" Type="String" />
<asp:Parameter Name="TaskDescription" Type="String" />
<asp:Parameter Name="TaskPriority" Type="Single" />
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_TaskName" Type="String" />
<asp:Parameter Name="original_TaskDescription" Type="String" />
<asp:Parameter Name="original_TaskPriority" Type="Single" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="TaskName" Type="String" />
<asp:Parameter Name="TaskDescription" Type="String" />
<asp:Parameter Name="TaskPriority" Type="Single" />
</InsertParameters>
</asp:AccessDataSource>
</form>
</body>
</html>
In my experience, that error message usually signals an incorrectly typed column name.
I suggest you step-debug your code and try to examine the SQL that is generated for each of the commands just before they are executed, once parameter substitution has occurred.
Also could be because of wrong column name.
Example:
cmd.CommandText = ""
+ "UPDATE Marriage "
+ " SET labelprint = ? "
+ " WHERE model = ? AND plant = ? AND seq = ? "
+ "";
Will always report the "System.Data.OleDb.OleDbException: No value given for one or more required parameters" error because of wrong column name. But as discovered the correct column name was "LabelPrinted" (but not "LabelPrint")!
So, it looks like MS Access may interpret wrong column name as a parameter, as result - reporting that error...
Change your UpdateParamters section like this:
<UpdateParameters>
<asp:Parameter Name="TaskName" Type="String" />
<asp:Parameter Name="TaskDescription" Type="String" />
<asp:Parameter Name="TaskPriority" Type="Single" />
<asp:Parameter Name="original_Id" Type="Int32" />
</UpdateParameters>
And your UpdateCommand to this (just remove line breaks):
UPDATE [Tasks]
SET [TaskName] = ?, [TaskDescription] = ?, [TaskPriority] = ?
WHERE [Id] = ?
This assumes your Id column is the primary key, I'm not sure why the wizard created it like it did, but no need to compare those other columns.