p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   DetailView Update not working. (http://p2p.wrox.com/showthread.php?t=88837)

scott100 November 20th, 2012 09:30 AM

DetailView Update not working.
 
Hi all, new member stuck so need some help please [:)]

I'm following page 410 - Beginning ASP.NET 3.5


DB Name
  • dbo.A_TEST_SCOTT

Columns:
  • id (PK, int, not null)
  • name (varchar(50), null)
  • address (varchar(50), null)


I can insert OK into the database
I can't update, when i try to do this and check the results, nothing has changed :(

My DetailsView page is displaying the record ok based on the query string parameter so ?id=2 and ?id=3 pull through different records fine.


Here is my code, all drap and drop:

Code:

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames="id" DataSourceID="SqlDataSource1" DefaultMode="Insert"
        Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
            <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:Commercial_db %>"
        DeleteCommand="DELETE FROM [A_TEST_SCOTT] WHERE [id] = @id"
        InsertCommand="INSERT INTO [A_TEST_SCOTT] ([id], [name], [address]) VALUES (@id, @name, @address)"
        SelectCommand="SELECT [id], [name], [address] FROM [A_TEST_SCOTT] WHERE ([id] = @id)"
        UpdateCommand="UPDATE [A_TEST_SCOTT] SET [name] = @name, [address] = @address WHERE [id] = @id">
        <SelectParameters>
            <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>


Code Behind

Code:

       
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString.Get("id") != null)
    {
      DetailsView1.DefaultMode = DetailsViewMode.Edit;
    }
}



any advise appreciated

scott100 November 20th, 2012 11:40 AM

Ok i think i may have figured out what wrong.

My code was in a Web User Control .ascx

When i tried again using a Web Form .aspx the update worked fine.

The problem is i'm using a CMS so can only use Web User Conrols... what can i do?

Imar November 20th, 2012 02:45 PM

Hi there,

This should work in User Controls as well. Are you sure you're passing the correct query string parameter ("id") from the lost page to the edit page that contains the UC?

Imar

scott100 November 21st, 2012 07:35 AM

Hi Imar, thanks for taking the time to post back, and so quickly [:)]

I tried a little experiment

i have saved my DetailsView which does the displaying/editing/inserting in a control: dv.ascx

i have then created a webform.aspx and a webusercontrol.ascx - both of these files include the dv.ascx control

as before, inserting from both webform.aspx and webusercontrol.ascx works fine

when it comes to editing, only webform.aspx works, webusercontrol.ascx does nothing. This is strange given that they are both using the exact same code contained in dv.ascx [:confused:]

I take it there is probably some underlying conflict with the CMS i'm using.

Imar November 21st, 2012 02:23 PM

Hi there,

Try debugging the site and set a breakpoint in the code that looks at the Query string:

if (Request.QueryString.Get("id") != null)

Then put a watch on Request.QueryString.Get("id"). Maybe that's null? Or maybe the code in Page_Load doesn't fire at all?

Imar

adamsmtih321 March 30th, 2013 03:54 AM

I try to update the detailview control. got this error messsage. ObjectDataSource 'odsDetail' could not find a non-generic method 'Update

Imar March 30th, 2013 09:47 AM

Hi there,

How is this related to the original post? If I were you, I'd start a new thread, and supply lots more information and the current code you're working with.

Cheers,

Imar


All times are GMT -4. The time now is 12:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.