p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 (http://p2p.wrox.com/forumdisplay.php?f=229)
-   -   Data Binding - Editing GridView Row Data (http://p2p.wrox.com/showthread.php?t=47383)

desk_star September 5th, 2006 12:12 AM

Data Binding - Editing GridView Row Data
 
In my book at page 363 the authors start explaining how to set things up to enable data update - editing a row in other words.

We get the content for the SqlDataSource's UpdateCommand which contains "UPDATE [Customers] SET [CompanyName] = @CompanyName ... WHERE [CustomerID] = @original_CustomerID".

Then in the explanation they mention these "@something" explaining what they are:parameters or placeholders for the corresponding information from the selected row in the GridView. This is pretty good so far but this time, however they talk about a @CustomerID parameter, completely ignoring that a few lines above they wrote @original_CustomerID.

Of course the code is not working this way so I changed @original_CustomerID to @CustomerID which did nothing to fix my problem. I was not getting the error anymore but no update whatsoever.

I'm pretty pissed off with them ignoring to explain this and many other things.Not to mention that the book could've been half the number of pages - a lot of bla, bla, bla on not so important matters like what you can do without coding at all. There are hundreds of documents and webcasts on how to do that already. They should've made the book lighter and fill it with substance.

I found something: a SqlDataSource parameter called OldValuesParameterFormatString which has to do with this but I don't want to detail as I'm still doing research ... Of course, instead understanding it in 20 minutes, from the book I paid for, I have to waste a lot of time.

Thanks, Professional ASP 2.0 authors.

Oh, if anybody bothers to read these messages and has a clue about my problem, please, don't be shy. I just needed to express my disappointment.


shysusan October 17th, 2006 08:27 AM

I would be interested to know if you got a resolution to this as I am having the same problem (I am not a happy Susan with this book at the moment!):(


Asp.Net January 11th, 2007 10:34 AM

Here's the answer :))
The working example is here:http://aspspider.biz/chadfunk/SqlDat...ataUpdate.aspx

(Be aware)Due to the often server crashes, you may receive Errors.
If you get error, come back later.
(Server Crashes cause Connection string failure)
================================================== ===================
Page 363
Listing 11-22 to 11-27

//-------------------------------------------------
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT * FROM [Customers]"
             UpdateCommand ="UPDATE [Customers] SET
                                     [CompanyName] = @CompanyName,
                                     [ContactName] = @ContactName,
                                     [ContactTitle] = @ContactTitle,
                                     [Address] = @Address,
                                     [City] = @City,
                                     [Region] = @Region,
                                     [PostalCode] = @PostalCode,
                                     [Country] = @Country,
                                     [Phone] = @Phone,
                                     [Fax] = @Fax
                                    WHERE [CustomerID] = @CustomerID
                            ">
                            
                             <UpdateParameters>
                                    <asp:Parameter Type ="string" Name ="CompanyName" Direction ="Input"/>
                                    <asp:Parameter Type ="string" Name ="ContactName" />
                                    <asp:Parameter Type ="string" Name ="ContactTitle" />
                                    <asp:Parameter Type ="string" Name ="Address" />
                                    <asp:Parameter Type ="string" Name ="City" />
                                    <asp:Parameter Type ="string" Name ="Region" />
                                    <asp:Parameter Type ="string" Name ="PostalCode" />
                                    <asp:Parameter Type ="string" Name ="Country" />
                                    <asp:Parameter Type ="string" Name ="Phone" />
                                    <asp:Parameter Type ="string" Name ="Fax" />
                                    <asp:Parameter Type ="string" Name ="CustomerID" />
                             </UpdateParameters>
            </asp:SqlDataSource>

------------

Gridview Code

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" Font-Names="Verdana" Font-Size="8.5pt">
            <Columns>
            <asp:CommandField ShowHeader ="True" HeaderText ="Command" ShowEditButton ="True" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly ="True" SortExpression="CustomerID" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
                <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
            </Columns>
        </asp:GridView>


/------------------------------------------------------------------
It works 100% :))

jvanreck December 30th, 2007 09:47 AM

I bought your book in the spanish version and it looks great, but I have had problems with the Update command.
I have copied the code as it is and it still doesn't makes the update.

If you change the parameter "@CustomerID" for 'ALFKI', it always updates
the customer with customerID = 'ALFKI'.

If you remove the WHERE condition, it updates ALL the records.

It seems that the problem is that the program is not getting the parameter CustomerID for the "WHERE [CustomerID] = @CustomerID" in the UPDATE command.

Please assist!!! Thanx

BTW: It seems related to http://p2p.wrox.com/topic.asp?TOPIC_...pdate,gridview

Imar December 30th, 2007 10:10 AM

Did you set the DataKeyNames as suggested in an earlier post?

Can you post the relevant parts of your code?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

jvanreck December 30th, 2007 10:29 AM

I've tested it with Northwind database and an example program and it works.

But when I try to adapt the code to my program it still doesn't work.

Thank you very much for your response...


The full code is here:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Gestión COLENF</title>
  <link rel="StyleSheet" type="text/css" media="screen" />
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <div>
        <asp:GridView ID="GV_Colegiados" runat="server" DataSourceID="SDS_Colegiados"
          DataKeyNames="COLEGIADO"
          AllowSorting="True" AllowPaging="True" PagerSettings-Position="Top"
          EmptyDataText="No se han encontrado coincidencias"
          AutoGenerateColumns="False">
          <Columns>
            <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
            <asp:BoundField DataField="COLEGIADO" HeaderText="COLEGIADO" ReadOnly="True" SortExpression="COLEGIADO" />
            <asp:BoundField DataField="APELLIDOS" HeaderText="APELLIDOS" SortExpression="APELLIDOS" />
            <asp:BoundField DataField="NOMBRE" HeaderText="NOMBRE" SortExpression="NOMBRE" />
            <asp:CheckBoxField DataField="DEVOLUCION" HeaderText="DEVOLUCION" SortExpression="DEVOLUCION" />
            <asp:BoundField DataField="DIRECCION" HeaderText="DIRECCION" SortExpression="DIRECCION" />
            <asp:BoundField DataField="IUBICACION" HeaderText="IUBICACION" SortExpression="IUBICACION" />
            <asp:BoundField DataField="COMARCA" HeaderText="COMARCA" SortExpression="COMARCA" />
            <asp:BoundField DataField="POBLACION" HeaderText="POBLACION" SortExpression="POBLACION" />
            <asp:BoundField DataField="NCODPROV" HeaderText="NCODPROV" SortExpression="NCODPROV" />
            <asp:BoundField DataField="CODPOSTAL" HeaderText="CODPOSTAL" SortExpression="CODPOSTAL" />
            <asp:BoundField DataField="PAIS" HeaderText="PAIS" SortExpression="PAIS" />
            <asp:BoundField DataField="TELEFONO" HeaderText="TELEFONO" SortExpression="TELEFONO" />
            <asp:BoundField DataField="TELEFONO2" HeaderText="TELEFONO2" SortExpression="TELEFONO2" />
            <asp:BoundField DataField="CDNICIF" HeaderText="CDNICIF" SortExpression="CDNICIF" />
            <asp:BoundField DataField="TITULO 1" HeaderText="TITULO 1" SortExpression="TITULO 1" />
            <asp:BoundField DataField="TITULO 2" HeaderText="TITULO 2" SortExpression="TITULO 2" />
            <asp:BoundField DataField="FECHA DE NACIMIENTO" HeaderText="FECHA DE NACIMIENTO" SortExpression="FECHA DE NACIMIENTO" />
            <asp:BoundField DataField="SEXO" HeaderText="SEXO" SortExpression="SEXO" />
            <asp:BoundField DataField="E-MAIL" HeaderText="E-MAIL" SortExpression="E-MAIL" />
            <asp:CheckBoxField DataField="MOROSO" HeaderText="MOROSO" SortExpression="MOROSO" />
            <asp:BoundField DataField="SALDO" HeaderText="SALDO" SortExpression="SALDO" />
          </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SDS_Colegiados" runat="server" ConnectionString="<%$ ConnectionStrings:ACS_Colegiados %>"
          ProviderName="<%$ ConnectionStrings:ACS_Colegiados.ProviderName %>"
          SelectCommand="SELECT * FROM [Colegiados]"
          UpdateCommand="UPDATE [Colegiados] SET [Nombre]=@Nombre, [Apellidos]=@Apellidos, [CDNICIF]=@CDNICIF, [Direccion]=@Direccion WHERE [COLEGIADO]=@COLEGIADO ">
        </asp:SqlDataSource>
      </div>
    </div>
  </form>
</body>
</html>
:)


jvanreck December 30th, 2007 10:51 AM

It finally works...

I had to put EXACTLY the same fields and in the same order inside the <columns> label and in the UPDATE command.

Saludos... :)


Imar December 30th, 2007 11:07 AM

You may want to take a look here: http://msdn2.microsoft.com/en-us/library/ms228051(VS.80).aspx

If you need to customize the columns / fields, you need to create an <UpdateParameters> element....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.


All times are GMT -4. The time now is 07:03 PM.

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