Hello there,
I have been trying to implement a formView to update recrords in an Oracle database, but I seem to be getting a concurrency violation error. I am currently the only user and the record is only access through one web browser. I also setup the FormView DataKeyNames to the primary key of the tables. Here some details about the application. I have posted all the necessary from the files.
Any help or comments will be very much appreciated.
Thanks a lot for your assistance.
Application Details:
- I am using xsd file as a DAL to define all the queries required. Optemistic Concurrency is implemented as the website will be used by multiple users, so it is a requirement.
- I have defined object for each table in the database and their corresponding functionalities (BLL).
- I am using a ObjectDataSources in ASP.Net pages to access all the data from the database.
FormView Details:
- The FormView is created especially to edit records in the datbase. It will not perform any insert, remove, or "view" functinality. Therefore, the default mode is set to "Edit". The table has many foreign keys, that's why i am not using the form to view record details.
- The PK of the record will passed from a GridView through a URL QueryString.
- The FormView also has a DropDownList (Subcategory) that is depending on another DDL(Category) selection.
FormView Code:
1 <asp:FormView ID="FormView1" runat="server" DataKeyNames="ITEM_NUMBER" DataSourceID="GetTaskDetailsDataSource" DefaultMode="Edit" OnItemInserting="FormView1_ItemInserting" OnItemUpdating="FormView1_ItemUpdating">
2 <EditItemTemplate>
3 <!------------------------Declaration of ObjectDataSources------------------------------
4 These ObjectDataSources are specific to EditItemTemplate and cannot be used outside
5 these boudaries ----------------------------------------------------------------------->
6 <asp:ObjectDataSource ID="StreetDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
7 SelectMethod="GetStreets" TypeName="StreetsBLL"></asp:ObjectDataSource>
8 <asp:ObjectDataSource ID="SuburbDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
9 SelectMethod="GetSuburbs" TypeName="SuburbsBLL"></asp:ObjectDataSource>
10 <asp:ObjectDataSource ID="ElectorateDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
11 SelectMethod="GetElectorates" TypeName="ElectorateBLL"></asp:ObjectDataSource>
12 <asp:ObjectDataSource ID="CategoriesDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
13 SelectMethod="GetCategories" TypeName="CategoriesBLL"></asp:ObjectDataSource>
14 <asp:ObjectDataSource ID="OfficerDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
15 SelectMethod="GetEmployees" TypeName="EmployeesBLL"></asp:ObjectDataSource>
16 <asp:ObjectDataSource ID="TaskTypeDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
17 SelectMethod="GetTaskTypes" TypeName="TaskTypesBLL"></asp:ObjectDataSource>
18 <asp:ObjectDataSource ID="StatusDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
19 SelectMethod="GetTaskStatus" TypeName="TaskStatusBLL"></asp:ObjectDataSource>
20 <asp:ObjectDataSource ID="SubCategoriesDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
21 SelectMethod="GetSubCategoriesByCategoryID" TypeName="SubCategoriesBLL">
22 <SelectParameters>
23 <asp:ControlParameter ControlID="CategoryDDL" DefaultValue="-1" Name="catID" PropertyName="SelectedValue"
24 Type="Int32" />
25 </SelectParameters>
26 </asp:ObjectDataSource>
27 <br />
28 <!----------------- End of ObjectDataSource definition for the edit template ------->
29 Item Number:
30 <asp:Label ID="ITEM_NUMBERLabel1" runat="server" Text='<%# Eval("ITEM_NUMBER") %>'>
31 </asp:Label><br />
32 <asp:Panel ID="Panel1" runat="server">
33 <table>
34 <tr>
35 <th colspan='4'class="TableHeader" style="height: 18px">Location Information</th>
36 </tr>
37 <tr>
38 <th>Primary Street:</th>
39 <td>
40 <asp:DropDownList ID="PrimaryStreetDDL" runat="server" DataSourceID="StreetDataSource"
41 DataTextField="STREET_ABBREVIATED" DataValueField="STREET_ID" SelectedValue='<%# Bind("STREET_ID_1") %>' AppendDataBoundItems="True">
42 <asp:ListItem Selected="True">(None)</asp:ListItem>
43 </asp:DropDownList>
44 </td>
45 <th>Primary Street Section:</th>
46 <td>
47 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
48 </tr>
49 <tr>
50 <th>Secondary Street:</th>
51 <td>
52 <asp:DropDownList ID="SecondaryStreetDDL" runat="server" DataSourceID="StreetDataSource"
53 DataTextField="STREET_ABBREVIATED" DataValueField="STREET_ID" SelectedValue='<%# Bind("STREET_ID_2") %>' AppendDataBoundItems="True">
54 <asp:ListItem Selected="True">(None)</asp:ListItem>
55 </asp:DropDownList></td>
56 <th>Secondary Street Section</th>
57 <td>
58 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
59 </tr>
60 <tr>
61 <th>Suburb:</th>
62 <td>
63 <asp:DropDownList ID="SuburbDDL" runat="server" DataSourceID="SuburbDataSource"
64 DataTextField="SUBURB" DataValueField="SUBURB_ID" SelectedValue='<%# Bind("SUBURB_ID") %>' AppendDataBoundItems="True">
65 <asp:ListItem Selected="True">(None)</asp:ListItem>
66 </asp:DropDownList></td>
67 <th>Electorate</th>
68 <td>
69 <asp:DropDownList ID="ElectorateDDL" runat="server" DataSourceID="ElectorateDataSource"
70 DataTextField="VALUE" DataValueField="ELECTORATE_ID" SelectedValue='<%# Bind("ELECTORATE_ID") %>' AppendDataBoundItems="True">
71 <asp:ListItem Selected="True">(None)</asp:ListItem>
72 </asp:DropDownList></td>
73 </tr>
74 <tr>
75 <th>Location</th>
76 <td colspan='3'>
77 <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LOCATION", "{0}") %>' Width="494px"></asp:TextBox></td>
78 </tr>
79 </table>
80 <br />
81 <table>
82 <tr>
83 <th colspan='4' class="TableHeader">
84 Details:</th>
85 </tr>
86 <tr>
87 <th style="width: 20%">Category:<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="CategoryDDL"
88 ErrorMessage="You must select a category" Operator="NotEqual" SetFocusOnError="True"
89 ValueToCompare="-1">*</asp:CompareValidator></th>
90 <td>
91 <asp:DropDownList ID="CategoryDDL" runat="server" DataSourceID="CategoriesDataSource"
92 DataTextField="VALUE" DataValueField="ISSUE_CAT_ID" SelectedValue='<%# Bind("ISSUE_CAT_ID") %>' AutoPostBack="True" AppendDataBoundItems="True">
93 </asp:DropDownList>
94 </td>
95 <th>Date Approved:</th>
96 <td>
97 <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("APPROVAL_DATE") %>'></asp:TextBox></td>
98 </tr>
99 <tr>
100 <th style="width: 20%">Subcategory:<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="SubCategoryDDL"
101 ErrorMessage="You must select a Sub Category" Operator="NotEqual" SetFocusOnError="True"
102 ValueToCompare="-1">*</asp:CompareValidator></th>
103 <td>
104 <asp:DropDownList ID="SubCategoryDDL" runat="server" DataSourceID="SubCategoriesDataSource"
105 DataTextField="VALUE" DataValueField="ISSUE_SUB_CAT_ID" OnDataBound="SubCategoryDDL_DataBound" AppendDataBoundItems="True">
106 </asp:DropDownList>
107 </td>
108 <th>Date Installed:</th>
109 <td>
110 <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("INSTALLATION_DATE") %>'></asp:TextBox></td>
111 </tr>
112 <tr>
113 <th style="width: 20%">Task:<asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="TaskTypeDDL"
114 ErrorMessage="You must select a task type" Operator="NotEqual" SetFocusOnError="True"
115 ValueToCompare="-1">*</asp:CompareValidator></th>
116 <td>
117 <asp:DropDownList ID="TaskTypeDDL" runat="server" DataSourceID="TaskTypeDataSource"
118 DataTextField="VALUE" DataValueField="TASK_ID" SelectedValue='<%# Bind("TASK_ID") %>' AppendDataBoundItems="True">
119 <asp:ListItem Selected="True" Value="-1">Select a Task Type</asp:ListItem>
120 </asp:DropDownList>
121 </td>
122 <th>Date Completed:</th>
123 <td>
124 <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("COMPLETED_DATE") %>'></asp:TextBox></td>
125 </tr>
126 <tr>
127 <th style="width: 20%">Responsible:</th>
128 <td >
129 <asp:DropDownList ID="OfficerDDL" runat="server" DataSourceID="OfficerDataSource"
130 DataTextField="FULL_NAME" DataValueField="PAY_NUMBER" SelectedValue='<%# Bind("OFFICER_RESPONSIBLE_ID") %>' AppendDataBoundItems="True">
131 <asp:ListItem Selected="True">(None)</asp:ListItem>
132 </asp:DropDownList></td>
133 <th>Status:</th>
134 <td>
135 <asp:DropDownList ID="StatusDDL" runat="server" DataSourceID="StatusDataSource" DataTextField="VALUE"
136 DataValueField="STATUS_ID" SelectedValue='<%# Bind("STATUS_ID") %>' AppendDataBoundItems="True">
137 <asp:ListItem Selected="True">(None)</asp:ListItem>
138 </asp:DropDownList></td>
139 </tr>
140 </table>
141 <br />
142 <table>
143 <tr>
144 <th colspan='4' class="TableHeader"> References:</th>
145 </tr>
146 <tr>
147 <th>CRM Reference:</th>
148 <td>
149 <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("CRM_REFERENCE_NO") %>'></asp:TextBox></td>
150 <th>Reference:</th>
151 <td>
152 <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("REFERENCE_NO") %>'></asp:TextBox></td>
153 </tr>
154 <tr>
155 <th>TRIM Reference:</th>
156 <td>
157 <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("TRIM_REFERENCE_NO") %>'></asp:TextBox><td style="width: 144px; height: 21px;">
158 </td>
159 <td style="height: 21px"></td>
160 </tr>
161 <tr>
162 <th>Comments:</th>
163 <td colspan='3' >
164 <asp:TextBox ID="TextBox10" runat="server" Rows="5" Text='<%# Bind("COMMENTS") %>'
165 TextMode="MultiLine" Width="493px"></asp:TextBox></td>
166 </tr>
167 </table>
168 </asp:Panel>
169 <br />
170 <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
171 Text="Update">
172 </asp:LinkButton>
173 <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
174 Text="Cancel">
175 </asp:LinkButton>
176 </EditItemTemplate>
177 </asp:FormView>
178 <asp:ObjectDataSource ID="GetTaskDetailsDataSource" runat="server" DeleteMethod="RemoveTaskItem"
179 InsertMethod="AddTaskItem" OldValuesParameterFormatString="original_{0}" SelectMethod="GetTasksByID"
180 TypeName="TasksBLL" UpdateMethod="UpdateTaskItem">
181 <DeleteParameters>
182 <asp:Parameter Name="original_ITEM_NUMBER" Type="String" />
183 <asp:Parameter Name="original_ISSUE_CAT_ID" Type="Int32" />
184 <asp:Parameter Name="original_ISSUE_SUB_CAT_ID" Type="Int32" />
185 <asp:Parameter Name="original_TASK_ID" Type="Int32" />
186 <asp:Parameter Name="original_LOCATION" Type="String" />
187 <asp:Parameter Name="original_STREET_ID_1" Type="Int32" />
188 <asp:Parameter Name="original_STREET_ID_2" Type="Int32" />
189 <asp:Parameter Name="original_SUBURB_ID" Type="Int32" />
190 <asp:Parameter Name="original_ELECTORATE_ID" Type="Int32" />
191 <asp:Parameter Name="original_OFFICER_RESPONSIBLE_ID" Type="Int32" />
192 <asp:Parameter Name="original_APPROVAL_DATE" Type="DateTime" />
193 <asp:Parameter Name="original_INSTALLATION_DATE" Type="DateTime" />
194 <asp:Parameter Name="original_COMPLETED_DATE" Type="DateTime" />
195 <asp:Parameter Name="original_CRM_REFERENCE_NO" Type="String" />
196 <asp:Parameter Name="original_TRIM_REFERENCE_NO" Type="String" />
197 <asp:Parameter Name="original_REFERENCE_NO" Type="String" />
198 <asp:Parameter Name="original_COMMENTS" Type="String" />
199 <asp:Parameter Name="original_STATUS_ID" Type="String" />
200 </DeleteParameters>
201 <UpdateParameters>
202 <asp:Parameter Name="ISSUE_CAT_ID" Type="Int32" />
203 <asp:Parameter Name="ISSUE_SUB_CAT_ID" Type="Int32" />
204 <asp:Parameter Name="TASK_ID" Type="Int32" />
205 <asp:Parameter Name="LOCATION" Type="String" />
206 <asp:Parameter Name="STREET_ID_1" Type="Int32" />
207 <asp:Parameter Name="STREET_ID_2" Type="Int32" />
208 <asp:Parameter Name="SUBURB_ID" Type="Int32" />
209 <asp:Parameter Name="ELECTORATE_ID" Type="Int32" />
210 <asp:Parameter Name="OFFICER_RESPONSIBLE_ID" Type="Int32" />
211 <asp:Parameter Name="APPROVAL_DATE" Type="DateTime" />
212 <asp:Parameter Name="INSTALLATION_DATE" Type="DateTime" />
213 <asp:Parameter Name="COMPLETED_DATE" Type="DateTime" />
214 <asp:Parameter Name="CRM_REFERENCE_NO" Type="String" />
215 <asp:Parameter Name="TRIM_REFERENCE_NO" Type="String" />
216 <asp:Parameter Name="REFERENCE_NO" Type="String" />
217 <asp:Parameter Name="COMMENTS" Type="String" />
218 <asp:Parameter Name="STATUS_ID" Type="String" />
219 <asp:Parameter Name="original_ITEM_NUMBER" Type="String" />
220 <asp:Parameter Name="original_ISSUE_CAT_ID" Type="Int32" />
221 <asp:Parameter Name="original_ISSUE_SUB_CAT_ID" Type="Int32" />
222 <asp:Parameter Name="original_TASK_ID" Type="Int32" />
223 <asp:Parameter Name="original_LOCATION" Type="String" />
224 <asp:Parameter Name="original_STREET_ID_1" Type="Int32" />
225 <asp:Parameter Name="original_STREET_ID_2" Type="Int32" />
226 <asp:Parameter Name="original_SUBURB_ID" Type="Int32" />
227 <asp:Parameter Name="original_ELECTORATE_ID" Type="Int32" />
228 <asp:Parameter Name="original_OFFICER_RESPONSIBLE_ID" Type="Int32" />
229 <asp:Parameter Name="original_APPROVAL_DATE" Type="DateTime" />
230 <asp:Parameter Name="original_INSTALLATION_DATE" Type="DateTime" />
231 <asp:Parameter Name="original_COMPLETED_DATE" Type="DateTime" />
232 <asp:Parameter Name="original_CRM_REFERENCE_NO" Type="String" />
233 <asp:Parameter Name="original_TRIM_REFERENCE_NO" Type="String" />
234 <asp:Parameter Name="original_REFERENCE_NO" Type="String" />
235 <asp:Parameter Name="original_COMMENTS" Type="String" />
236 <asp:Parameter Name="original_STATUS_ID" Type="String" />
237 </UpdateParameters>
238 <SelectParameters>
239 <asp:QueryStringParameter Name="itemID" QueryStringField="itemNumber" Type="String" />
240 </SelectParameters>
241 <InsertParameters>
242 <asp:Parameter Name="ITEM_NUMBER" Type="String" />
243 <asp:Parameter Name="ISSUE_CAT_ID" Type="Int32" />
244 <asp:Parameter Name="ISSUE_SUB_CAT_ID" Type="Int32" />
245 <asp:Parameter Name="TASK_ID" Type="Int32" />
246 <asp:Parameter Name="LOCATION" Type="String" />
247 <asp:Parameter Name="STREET_ID_1" Type="Int32" />
248 <asp:Parameter Name="STREET_ID_2" Type="Int32" />
249 <asp:Parameter Name="SUBURB_ID" Type="Int32" />
250 <asp:Parameter Name="ELECTORATE_ID" Type="Int32" />
251 <asp:Parameter Name="OFFICER_RESPONSIBLE_ID" Type="Int32" />
252 <asp:Parameter Name="APPROVAL_DATE" Type="DateTime" />
253 <asp:Parameter Name="INSTALLATION_DATE" Type="DateTime" />
254 <asp:Parameter Name="COMPLETED_DATE" Type="DateTime" />
255 <asp:Parameter Name="CRM_REFERENCE_NO" Type="String" />
256 <asp:Parameter Name="TRIM_REFERENCE_NO" Type="String" />
257 <asp:Parameter Name="REFERENCE_NO" Type="String" />
258 <asp:Parameter Name="COMMENTS" Type="String" />
259 <asp:Parameter Name="STATUS_ID" Type="String" />
260 </InsertParameters>
261 </asp:ObjectDataSource>
FormView Code Behind:
protected void SubCategoryDDL_DataBound(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
FormView frmV = (FormView)ddl.NamingContainer;
if (frmV.DataItem != null)
{
string strSubCategory = ((DataRowView)frmV.DataItem)["ISSUE_SUB_CAT_ID"].ToString();
ddl.ClearSelection();
ListItem lm = ddl.Items.FindByValue(strSubCategory);
if (lm != null)
{
lm.Selected = true;
}
}
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
/*e.NewValues["ISSUE_CAT_ID"] = ((DropDownList)((FormView)sender).FindControl("CategoryDDL")).SelectedValue;
e.NewValues["ISSUE_SUB_CAT_ID"] = ((DropDownList)((FormView)sender).FindControl("SubCategoryDDL")).SelectedValue;
*/
IDictionaryEnumerator restoreOldValues = e.Keys.GetEnumerator();
while (restoreOldValues.MoveNext())
{
e.OldValues.Add(restoreOldValues.Key.ToString(), restoreOldValues.Value.ToString());
e.NewValues.Add(restoreOldValues.Key.ToString(), restoreOldValues.Value.ToString());
}
}
Update Function in BLL:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateTaskItem(
//New parameter values
int ISSUE_CAT_ID, int ISSUE_SUB_CAT_ID, int TASK_ID, string LOCATION,
int? STREET_ID_1, int? STREET_ID_2, int? SUBURB_ID, int? ELECTORATE_ID, int? OFFICER_RESPONSIBLE_ID,
DateTime? APPROVAL_DATE, DateTime? INSTALLATION_DATE, DateTime? COMPLETED_DATE, string CRM_REFERENCE_NO,
string TRIM_REFERENCE_NO, string REFERENCE_NO, string COMMENTS, string STATUS_ID,
//Original paramters values
string original_ITEM_NUMBER, int original_ISSUE_CAT_ID, int original_ISSUE_SUB_CAT_ID,
int original_TASK_ID, string original_LOCATION, int? original_STREET_ID_1, int? original_STREET_ID_2,
int? original_SUBURB_ID, int? original_ELECTORATE_ID, int? original_OFFICER_RESPONSIBLE_ID,
DateTime? original_APPROVAL_DATE, DateTime? original_INSTALLATION_DATE, DateTime? original_COMPLETED_DATE,
string original_CRM_REFERENCE_NO, string original_TRIM_REFERENCE_NO, string original_REFERENCE_NO,
string original_COMMENTS, string original_STATUS_ID,
string ITEM_NUMBER
)
{
// STEP 1: Read in the current database task information
Traffic.TASKSDataTable tasks = Adapter.GetTasksByItemNumber(original_ITEM_NUMBER);
if (tasks.Count == 0)
// no matching record found, return false
return false;
Traffic.TASKSRow taskItem = tasks[0];
// STEP 2: Assign the original values
UpdateTaskValues(taskItem,
original_ISSUE_CAT_ID,
original_ISSUE_SUB_CAT_ID,
original_TASK_ID, original_LOCATION,
original_STREET_ID_1, original_STREET_ID_2,
original_SUBURB_ID, original_ELECTORATE_ID,
original_OFFICER_RESPONSIBLE_ID,
original_APPROVAL_DATE, original_INSTALLATION_DATE, original_COMPLETED_DATE,
original_CRM_REFERENCE_NO, original_TRIM_REFERENCE_NO, original_REFERENCE_NO,
original_COMMENTS, original_STATUS_ID);
// STEP 3: Accept the changes
taskItem.AcceptChanges();
// STEP 4: Assign the new values
UpdateTaskValues(taskItem,
ISSUE_CAT_ID,
ISSUE_SUB_CAT_ID,
TASK_ID, LOCATION,
STREET_ID_1, STREET_ID_2,
SUBURB_ID, ELECTORATE_ID,
OFFICER_RESPONSIBLE_ID,
APPROVAL_DATE, INSTALLATION_DATE, COMPLETED_DATE,
CRM_REFERENCE_NO, TRIM_REFERENCE_NO, REFERENCE_NO,
COMMENTS, STATUS_ID);
// STEP 5: Update the record
int rowsAffected = Adapter.Update(taskItem);
return rowsAffected == 1;
}