2013-11-21 2 views
0

В настоящее время я работаю над проектом, который использует ObjectDataSources вместе с элементом управления GridView для запуска доступа и обновления базы данных.Использование ObjectDataSource для обновления GridView в ASP.NET

Текущая проблема, с которой я столкнулся, заключается в том, что при нажатии кнопки «Обновить» в gridview исключается ошибка исключения. Из того, что я понимаю, эта ошибка, похоже, говорит о том, что grid-view передает множество параметров в инструкцию update.

Я бы хотел, чтобы он отправил 2 объекта «orignal_Incident» и «инцидент», которые должны содержать все поля, которые мне нужны. Но, похоже, он также проходит по полям «DateClosed» и «Description».

Возможно ли отредактировать параметры, которые передает сетка? Или есть что-то еще, что нужно сделать, чтобы сделать эту работу?

Ниже приведен код и ошибки, относящиеся к этой части программы:

Ошибка:

ObjectDataSource 'obsIncidents' could not find a non-generic method 'UpdateIncident' that has parameters: original_Incident, incident, DateClosed, Description.

код ASP.NET для ObjectDataSource

<asp:ObjectDataSource ID="obsIncidents" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCustomerIncidents" TypeName="IncidentDB" UpdateMethod="UpdateIncident"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="ddlCustomer" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="original_Incident" Type="Object" /> 
     <asp:Parameter Name="incident" Type="Object" /> 
    </UpdateParameters> 
</asp:ObjectDataSource> 

кода ASP.NET для GridView

<asp:GridView ID="gvIncidents" runat="server" AutoGenerateColumns="False" DataSourceID="obsIncidents" CellPadding="4" ForeColor="#333333" GridLines="None"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
     <asp:BoundField DataField="IncidentID" HeaderText="ID" ReadOnly="True" ControlStyle-Width="30" /> 
     <asp:BoundField DataField="ProductCode" HeaderText="Product Code" ReadOnly="True" ControlStyle-Width="70" /> 
     <asp:BoundField DataField="DateOpened" DataFormatString="{0:d}" HeaderText="Date Opened" ReadOnly="True" ControlStyle-Width="70" /> 
     <asp:BoundField DataField="DateClosed" HeaderText="Date Closed" DataFormatString="{0:d}" ControlStyle-Width="70" ApplyFormatInEditMode="True" /> 
     <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" ControlStyle-Width="150" /> 

     <asp:TemplateField HeaderText="Description" ControlStyle-Width="300" > 
     <ItemTemplate> 
      <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' Width="300"></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Width="300" Rows="4" TextMode="MultiLine"></asp:TextBox> 
     </EditItemTemplate> 
     </asp:TemplateField> 

     <asp:CommandField ButtonType="Button" ShowEditButton="True" /> 
    </Columns> 
    <EditRowStyle BackColor="#2461BF" /> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#EFF3FB" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
    <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
    <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
    <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
</asp:GridView> 

метод Visual Basic обновление в классе-файле

<DataObjectMethod(DataObjectMethodType.Update)> 
Public Shared Function UpdateIncident(
     ByVal original_Incident As Incident, 
     ByVal incident As Incident) As Integer 
    Dim con As New SqlConnection(TechSupportDB.GetConnectionString) 
    Dim up As String = "UPDATE Incidents " & 
         "SET DateClosed = @DateClosed, " & 
         "Description = @Description " & 
         "WHERE IncidentID = @original_IncidentID " & 
         "AND ProductCode = @original_ProductCode " & 
         "AND DateOpened = @original_DateOpened " & 
         "AND (DateClosed = @original_DateClosed " & 
         "OR DateClosed IS NULL " & 
         "AND @original_DateClosed IS NULL) " & 
         "AND Title = @original_Title " & 
         "AND Description = @original_Description" 
    Dim cmd As New SqlCommand(up, con) 
    If incident.DateClosed = #12:00:00 AM# Then 
     cmd.Parameters.AddWithValue("DateClosed", DBNull.Value) 
    Else 
     cmd.Parameters.AddWithValue("DateClosed", incident.DateClosed) 
    End If 
    cmd.Parameters.AddWithValue("Description", incident.description) 
    cmd.Parameters.AddWithValue("original_IncidentID", original_Incident.IncidentID) 
    cmd.Parameters.AddWithValue("original_ProductCode", original_Incident.ProductCode) 
    cmd.Parameters.AddWithValue("original_DateOpened", original_Incident.DateOpened) 
    If original_Incident.DateClosed = #12:00:00 AM# Then 
     cmd.Parameters.AddWithValue("original_DateClosed", DBNull.Value) 
    Else 
     cmd.Parameters.AddWithValue("original_DateClosed", original_Incident.DateClosed) 
    End If 
    cmd.Parameters.AddWithValue("original_Title", original_Incident.title) 
    cmd.Parameters.AddWithValue("original_Description", original_Incident.description) 
    con.Open() 
    Dim i As Integer = cmd.ExecuteNonQuery() 
    con.Close() 
    Return i 
End Function 

ответ

0

AzureShadow,

Просто измените OldValuesParameterFormatString = "original_ {0}" в OldValuesParameterFormatString = "{0}". Он должен работать отлично! Я думаю, что ваш случай и this case очень похожи. Взгляните на этот пост. Это будет совершенно ясно.

Смежные вопросы