0

im having some wierd issue with my GridView. Basically, im trying to run a delete command on a table. The gridview has 3 fields bound to it, ItemId, ItemSummary, and ItemDate.

Now as i recall, during a mode change (say, between Select and Edit) these fields are automatically passed using the syntax @+FieldName (ie. ItemId is passed as @ItemId).

So keeping this in mind i tried the delete statement, but it gave an error saying the parameter was not passed. So i tried the other route, to extract the ItemId programmatically and insert it as a parameter at runtime.

I tried reading from the DataItem bound to to the control, but it kept returning null.

After some more debugging, i found that when the data was initially bound to the control, it correctly created a DataItem, but as soon as the mode was changed the DataItem is now null.

I have been trying to fix this for ages and it just wont work. Here is my code:

<asp:DetailsView FieldHeaderStyle-CssClass="bold" CssClass="marginLeftRightBottom10px center"
                AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" GridLines="Both"
                ID="dvIndividualUpdate" AutoGenerateInsertButton="true" runat="server" AutoGenerateRows="False"
                DataSourceID="sqldsSingleUpdate" OnDataBound="dvIndividualUpdate_DataBound">
                <Fields>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Update Id:
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label Text='<%# Eval("updateId") %>' ID="lblUpdateId" runat="server"></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <asp:Label Text='Auto Generated' ID="lblUpdateIdInsert" runat="server"></asp:Label>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <asp:Label Text='Auto Generated' ID="lblUpdateIdEdit" runat="server"></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Update Summary:
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label Text='<%# Eval("updateSummary") %>' ID="lblUpdateSummary" runat="server"></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox CssClass="tbUpdateSummaryInsert" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
                                ID="tbUpdateSummary" runat="server"></asp:TextBox>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox CssClass="tbUpdateSummaryEdit" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
                                ID="tbUpdateSummary" runat="server"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Update Date:
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label Text='<%# Eval("dateOfUpdate") %>' ID="lblDateOfUpdateInsert" runat="server"></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <asp:Label Text='' ID="lblEditDateOfUpdate" runat="server"></asp:Label>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <asp:Label Text='<%# Eval("dateOfUpdate") %>' ID="lblDateOfUpdateEdit" runat="server"></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>
            <asp:Label ID="lblUpdateErrors" runat="server" Text="" CssClass="block colorRed marginBottom10px center"></asp:Label>

            <asp:SqlDataSource DataSourceMode="DataSet" ID="sqldsSingleUpdate" runat="server"
                ConnectionString="<%$ ConnectionStrings:myDbConnection%>" SelectCommandType="StoredProcedure"
                SelectCommand="dbo.getUpdate" InsertCommand="dbo.createUpdate" InsertCommandType="StoredProcedure"
                OnInserted="sqldsSingleUpdate_Inserted" DeleteCommand="dbo.deleteUpdate" DeleteCommandType="StoredProcedure"
                OnDeleted="sqldsSingleUpdate_Deleted" OnDeleting="sqldsSingleUpdate_Deleting">
                <DeleteParameters>
                    <asp:Parameter Name="updateID" />
                </DeleteParameters>
                <SelectParameters>
                    <asp:ControlParameter Name="updateId" ControlID="gvUpdates" PropertyName="SelectedDataKey.Value" />
                </SelectParameters>
                <InsertParameters>
                    <asp:ControlParameter DbType="Date" ControlID="dvIndividualUpdate$lblEditDateOfUpdate"
                        Name="dateOfUpdate" PropertyName="Text" />
                </InsertParameters>
            </asp:SqlDataSource>

And my related codebehind:

protected void dvIndividualUpdate_DataBound(object sender, EventArgs e)
{
    if (dvIndividualUpdate.CurrentMode == DetailsViewMode.Insert)
    {
        Label lbl = dvIndividualUpdate.FindControl("lblEditDateOfUpdate") as Label;
        lbl.Text = DateTime.Today.ToString("dd-MM-yyyy");
    }
}

protected void sqldsSingleUpdate_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        Exception ex = e.Exception;
        lblUpdateErrors.Text = "There was a problem creating the update.";
        ErrorSignal.FromCurrentContext().Raise(ex);
        e.ExceptionHandled = true;
        return;
    }
    else
    {
        lblUpdateErrors.Text = "Update created successfully.";
        gvUpdates.DataBind();
    }
}
protected void sqldsSingleUpdate_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        Exception ex = e.Exception;
        lblUpdateErrors.Text = "There was a problem deleting the update.";
        ErrorSignal.FromCurrentContext().Raise(ex);
        e.ExceptionHandled = true;
        return;
    }
    else
    {
        lblUpdateErrors.Text = "Update deleted successfully.";
        gvUpdates.DataBind();
    }
}

Thanks a lot

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

1 Answers1

0

If you add ItemID as a data key to the GridView, the data source control should grab it automatically for the update and delete. Remove the update and delete parameters from the data source, assign the ItemID as a data key, and give that a shot.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • This works, but now i have run into another problem. Youd thing that sqlDataSource would automatically clear the cache after a delete, but it doesnt. I tried to clear it programmatically bu doing: – TheGateKeeper Aug 29 '11 at 19:22
  • @TheGateKeeper: You can turn off caching on the page by adding this to your page load: Response.Cache.SetCacheability(HttpCacheability.NoCache); – James Johnson Aug 29 '11 at 19:27
  • I dont want to disable cache, i just want it to request from the database when a delete happens. Then it can use cache again for paging etc. – TheGateKeeper Aug 29 '11 at 19:35
  • @TheGateKeeper: I see... I don't use data source controls, so I'm not sure about that one. Calling DataBind() after the delete doesn't fetch new data? – James Johnson Aug 29 '11 at 19:45
  • No, i think what it does is rebind it to the cache. Il disable caching until i find the solution, thanks for now :) – TheGateKeeper Aug 29 '11 at 19:47