views:

333

answers:

0

Hi,

I followed the following link

http://weblogs.asp.net/justinsaraceno/archive/2008/02/22/reorderlist-with-objectdatasource.aspx

and manged to make the orderlist work fine except one thing and probably the most importent thing

which is saving the reorder back to the database

what I changed

First I'm using it as a c# web form inside a visual basic.net website. Is this OK?

Second I changed the connectionString to point to my database wich is in SQLEXPRESS ( NOT ATTACHED in App_Data inside the website as the example shows)

Third I changed the select and update statements and parameters to my needs

after all that it works but without saving the reorder back to database

any help is really appreciated

thanks in advance

ReorderClass.cs

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Class object that binds to our ObjectDataSource.
/// Downloaded from Justin Saraceno's blog at http://weblogs.asp.net/justinsaraceno
/// </summary>
public class ReorderClass
{
    public ReorderClass()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /*  Note: in the 'real world' you may have a data access layer and perhaps even other objects
     *      running behind this.  But this shows you the bare essentials of what you'll need
     *      to get the ReorderList bound and re-ordering to an ObjectDataSource.
     */

    /// <summary>
    /// Returns DataSet of items to bind to the ReorderList.
    /// </summary>
    /// <returns></returns>
    public DataSet WishList()
    {
        // create dataset
        DataSet wishList = new DataSet();
        string query = "SELECT wish_id, wish_order FROM wish_line ORDER BY wish_order";

        SqlConnection conn = new SqlConnection(@"Data Source=FAHAD-PC\SQLEXPRESS;Initial Catalog=db;
            Integrated Security=True;");


        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        // fill dataset
        da.Fill(wishList);
        // return dataset
        return wishList;
    }

    /// <summary>
    /// Updates the order of the items in the ReorderList.
    /// </summary>
    /// <param name="wishID"></param>
    /// <param name="wishDescription"></param>
    /// <param name="wishOrder"></param>
    public void ReorderWishList(Guid wish_id, int wish_order)
    {
        //IMPORTANT NOTE: This method will not work unless all of the same fields that were pulled back in the SELECT method are also
        //  used as method parameters in this UPDATE method.  So here you see 'wishDescription' as a method parameter; even though
        //  it isn't used below, it must still be included in the UPDATE method signature.

        // query to update wish order
        string query = "UPDATE wish_line SET wish_order = @wish_order WHERE wish_id = @wish_id";
        // sql connection object
        SqlConnection conn = new SqlConnection(@"Data Source=FAHAD-PC\SQLEXPRESS;Initial Catalog=db;
            Integrated Security=True;");
        // sql command object
        SqlCommand cmd = new SqlCommand(query, conn);
        // sql parameters - you'll at least need the record identifier (WishID) and the order (WishOrder)
        cmd.Parameters.Add("@wish_id", SqlDbType.UniqueIdentifier).Value = new Guid(wish_id.ToString());
        cmd.Parameters.Add("@wish_order", SqlDbType.Int).Value = wish_order;

        // update record in database
        using (conn)
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

OrderWishes.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OrderWishes.aspx.cs" Inherits="OrderWishes" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
    <link href="ReorderStyles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        ReorderList bound to an ObjectDataSource Sample.<br />
        Downloaded from Justin Saraceno's blog at <a href="http://weblogs.asp.net/justinsaraceno" target="_blank">http://weblogs.asp.net/justinsaraceno&lt;/a&gt;
        <br /><br />
        My Wish List ReorderList Sample:<br />
        Drag and re-order these items and the new order is saved in the ReorderSampleDatabase SQLExpress database that accompanied this sample.


        <cc1:ReorderList ID="ReorderList1" runat="server" PostBackOnReorder="True"
            SortOrderField="wish_order" DataKeyField="wish_id" DataSourceID="ObjectDataSource1"
            AllowReorder="True" >
            <ItemTemplate>
                <%--This is a template for the items to be reordered.--%>
                <div class="itemArea">
                    <table style="border-bottom: #C0C0C0 thin solid">
                        <tr id="rowReorderItem">
                            <td>
                                <asp:Label ID="lblWishOrder" runat="server" Width="15px" SkinID="Small" 
                                    Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("wish_order"))) %>'></asp:Label>
                            </td>
                            <td>
                                <asp:Label ID="lblWishDescription" runat="server" Width="275px" SkinID="Small" 
                                    Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("wish_id"))) %>'></asp:Label>
                            </td>
                        </tr>
                    </table>
                </div>
            </ItemTemplate>
            <EmptyListTemplate>
                <%--This template will show up if there were no items in the database to reorder.--%>
                <div>
                    No items exist.
                </div>
            </EmptyListTemplate>
            <ReorderTemplate>
                <asp:Panel ID="pnlReorder" runat="server" CssClass="reorderCue" />
            </ReorderTemplate>
            <DragHandleTemplate>
                <div class="dragHandle">
                    <asp:Image ID="imgReorder" runat="server" AlternateText="move" ToolTip="Click and drag to reorder"
                        ImageUrl="~/images/reorder.gif" />
                </div>
            </DragHandleTemplate>
        </cc1:ReorderList>


        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="WishList"
            TypeName="ReorderClass" UpdateMethod="ReorderWishList">
            <UpdateParameters>
                <asp:Parameter Name="wish_id" DbType="Guid" />
                <asp:Parameter Name="wish_order" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>


    </div>
    </form>
</body>
</html>