tags:

views:

21

answers:

2

I am trying to pull out data from the table I had from the database according to the id which is passed from the URL. However I always get data from id= 1? Why? FYI I took this code directly from the ClubWebsite starter kit and copy and paste it to my project to make several changes, the ClubWebsite one worked fine.. but this one doesn't and can't find any reason why because they both looked exactly the same.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Events_View.aspx.cs" Inherits="Events_View" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="splash" Runat="Server">
<div id="splash4">&nbsp;</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <div id="content">
   <div class="post">                  
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubDatabase %>"
                SelectCommand="SELECT dbo.Events.id, dbo.Events.starttime, dbo.events.endtime, dbo.Events.title, dbo.Events.description, dbo.Events.staticURL, dbo.Events.address FROM  dbo.Events">
                <SelectParameters>
                    <asp:Parameter Type="Int32" DefaultValue="1" Name="id"></asp:Parameter>
                </SelectParameters>
            </asp:SqlDataSource>

            <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id" AllowPaging="false" Width="100%">
                    <ItemTemplate>
                        <h2>
                            <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
                        </h2>
                        <div>
                            <br />
                            <p>
                                <asp:Label Text='<%# Eval("address") %>' runat="server" ID="addressLabel" />
                            </p>
                            <p>
                                <asp:Label Text='<%# Eval("starttime","{0:D}") %>' runat="server" ID="itemdateLabel" />
                                <br />
                                <asp:Label Text='<%# ShowDuration(Eval("starttime"),Eval("endtime")) %>' runat="server" ID="Label1" />
                            </p>
                        </div>

                        <p>
                            <asp:Label Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" />
                        </p>

                    </ItemTemplate>
                </asp:FormView>
                <div class="dashedline">
                </div>
   </div>
</div>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Events_View : System.Web.UI.Page
{

    const int INVALIDID = -1;

    protected void Page_Load(object sender, System.EventArgs e)
    {
        SqlDataSource1.SelectParameters["id"].DefaultValue = System.Convert.ToString(EventID);
    }

    public int EventID
    {
        get
        {
            int m_EventID;
            object id = ViewState["EventID"];
            if (id != null)
            {
                m_EventID = (int)id;
            }
            else
            {
                id = Request.QueryString["EventID"];
                if (id != null)
                {
                    m_EventID = System.Convert.ToInt32(id);
                }
                else
                {
                    m_EventID = 1;
                }
                ViewState["EventID"] = m_EventID;
            }
            return m_EventID;
        }
        set
        {
            ViewState["EventID"] = value;
        }
    }


    protected void FormView1_DataBound(object sender, System.EventArgs e)
    {
        DataRowView view = (DataRowView)(FormView1.DataItem);
        object o = view["staticURL"];
        if (o != null && o != DBNull.Value)
        {
            string staticurl = (string)o;
            if (staticurl != "")
            {
                Response.Redirect(staticurl);
            }
        }
    }

    protected string ShowLocationLink(object locationname, object id)
    {
        if (id != null && id != DBNull.Value)
        {
            return "At <a href='Locations_view.aspx?LocationID=" + Convert.ToString(id) + "'>" + (string)locationname + "</a><br/>";
        }
        else
        {
            return "";
        }
    }

    protected string ShowDuration(object starttime, object endtime)
    {
        DateTime starttimeDT = (DateTime)starttime;
        if (endtime != null && endtime != DBNull.Value)
        {
            DateTime endtimeDT = (DateTime)endtime;
            if (starttimeDT.Date == endtimeDT.Date)
            {
                if (starttimeDT == endtimeDT)
                {
                    return starttimeDT.ToString("h:mm tt");
                }
                else
                {
                    return starttimeDT.ToString("h:mm tt") + " - " + endtimeDT.ToString("h:mm tt");
                }
            }
            else
            {
                return "thru " + endtimeDT.ToString("M/d/yy");
            }
        }
        else
        {
            return starttimeDT.ToString("h:mm tt");
        }
    }

}
+2  A: 

Please check your SelectCommand. It should be:

SelectCommand="SELECT id, starttime, endtime, title, description, staticURL, address FROM  dbo.Events WHERE id=@id"

HTH

Raja
I think the OP actually wants to see more than 'id = 1' rows, so he needs to remove those parameters and use a querystring parameter instead.
BigChrisDiD
I dont understand why I am given -1 for this :-(. the Where Clause was missing which is the right answer.
Raja
A: 

See the Select Parameters (not select command) for your data source? It is specifying you only get rows where the id = 1.

Remove that section.

UPDATE: Whoops, actually, change it to a querystring parameter and reference the id in your url there. Then add the parameter reference into your Select command.

BigChrisDiD