views:

2134

answers:

2

I have an ObjectDataSource that I'm binding to a DetailsView control. I have the insert method written in a business layer (which calls down into a data layer) and everything works fine.. until i want to do something else before the insert method fires. Before going to my business layer I need access to a fileupload control. So I wired up an ItemCommand event on the DetailsView - it picks up the event and I can do what i need with the FileUpload control just fine. In that event I call the insert method in the business layer - the same method specified in the ObjectDataSource control. But the Insert method fires twice! After thinking on this for a minute i realize this is the expected behavior - it's fired once when called from the ItemCommand event, and the second time from ObjectDataSource InsertMethod.

I thought I could simply remove the InsertMethod attribute from the ObjectDataSource to eliminate the double fire on that method, but when I do that I get this error:

Inserting is not supported by ObjectDataSource 'objStudentDetails' unless the InsertMethod is specified.

So is there any way I can tell the ObjectDataSource not to fire the method? See code simplified code below:

<asp:DetailsView ID="dtvStudentDetails" 
  runat="server" 
  AutoGenerateRows="False" 
  DataSourceID="objStudentDetails"
  OnItemCommand="dtvStudentDetails_ItemCommand">
   :
   :
</asp:DetailsView>


<asp:ObjectDataSource ID="objStudentDetails" 
  runat="server" 
  TypeName="AIMLibrary.BLL.Students" 
  SelectMethod="GetStudentDetails" 
  UpdateMethod="UpdateStudent">         
    :
    :
</asp:ObjectDataSource>


public static Int32 InsertStudent(Int32 studentId, String firstName, String lastName, String employer, String phone, String email, String address, String city, String state, String zip, String dob, String cardImagePath)
{
  StudentDetails record = new StudentDetails(firstName, lastName, employer, phone, email, address, city, state, zip, dob, cardImagePath);
  StudentsProvider provider = new StudentsProvider();
  return provider.InsertStudent(record);  //actual insert happens in here..
}
+1  A: 

Is there a reason you can't just handle the Inserting event on the ObjectDataSource? It even has a way to cancel the insert if you want.

Just add the event handler to the ObjectDataSource in markup (or use the designer):

<asp:ObjectDataSource id=CustomerObjectDataSource" runat="server" 
    oninserting="CustomerObjectDataSource_Inserting"
</asp:ObjectDataSource>

This event fires just before the insert, and if you need to stop it from propagating, you can do something like this:

protected void CustomerObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
    InsertMethod(someParams);

    //If you are satisfied with what has already been done..
    e.Cancel = true;    
}
Josh
works great! Not sure how I missed that - thanks!
Tone
Nice bit of insight.
Robert Harvey
A: 

Hi try this example of gridview update with objectdatasource and edititmetemplate

Untitled Page

        <Columns>
        <asp:CommandField ShowEditButton="true" />
        <asp:TemplateField HeaderText="ID" SortExpression="ID">
        <EditItemTemplate>
        <asp:TextBox ID="txtID" runat="server" ReadOnly="true" Text='<%# Eval("ID") %>'></asp:TextBox>
        </EditItemTemplate>       
        <ItemTemplate>
        <asp:Label ID="lblID" runat="server" BackColor="AliceBlue" Text='<%# Eval("ID") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
        <EditItemTemplate>
        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
        </EditItemTemplate>
         <ItemTemplate>
        <asp:Label ID="lblName" runat="server" BackColor="AliceBlue" Text='<%# Eval("Name") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Locaton" SortExpression="Location">
        <EditItemTemplate>
        <asp:TextBox ID="txtLocation" runat="server" Text='<%# Eval("Location") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
        <asp:Label ID="lblLocation" runat="server" BackColor="AliceBlue" Text='<%# Eval("Location") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <ItemTemplate>

        </ItemTemplate>
        </asp:TemplateField>
        </Columns>

        </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="DataSet1TableAdapters.TestTableAdapter" UpdateMethod="UpdateQuery">
        <InsertParameters>
            <asp:Parameter Name="ID" Type="Decimal" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Location" Type="String" />
        </InsertParameters>
        <UpdateParameters>
        <asp:Parameter Name="ID" Type="Decimal" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Location" Type="String" />


        </UpdateParameters>
    </asp:ObjectDataSource>

</div>
</form>

Code behind

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID")).Text; string strName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName")).Text; string strLocation = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLocation")).Text; ObjectDataSource1.UpdateParameters.Clear(); ObjectDataSource1.UpdateParameters.Add("ID", strID); ObjectDataSource1.UpdateParameters.Add("Name", strName); ObjectDataSource1.UpdateParameters.Add("Location", strLocation); }

refer link for more articles and code examples

csharpdotnetfreak.blogspot.com

.