Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# sqldatasource和数据绑定问题_C#_.net_Asp.net - Fatal编程技术网

C# sqldatasource和数据绑定问题

C# sqldatasource和数据绑定问题,c#,.net,asp.net,C#,.net,Asp.net,我试图根据从URL传递的id从数据库的表中提取数据。但是我总是从id=1获取数据?为什么?仅供参考,我直接从ClubWebsite初学者工具包中获取了这段代码,并将其复制粘贴到我的项目中进行了一些更改,ClubWebsite one运行良好。。但是这一个没有,也找不到任何原因,因为它们看起来完全一样 <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Code

我试图根据从URL传递的id从数据库的表中提取数据。但是我总是从id=1获取数据?为什么?仅供参考,我直接从ClubWebsite初学者工具包中获取了这段代码,并将其复制粘贴到我的项目中进行了一些更改,ClubWebsite one运行良好。。但是这一个没有,也找不到任何原因,因为它们看起来完全一样

<%@ 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");
        }
    }

}


“; } 其他的 { 返回“”; } } 受保护的字符串显示持续时间(对象开始时间、对象结束时间) { DateTime starttimet=(DateTime)starttime; if(endtime!=null&&endtime!=DBNull.Value) { DateTime endtimeDT=(DateTime)endtime; 如果(starttimeDT.Date==endtimeDT.Date) { if(starttimeDT==endtimeDT) { 返回starttimeDT.ToString(“h:mm tt”); } 其他的 { 返回starttimeDT.ToString(“h:mm tt”)+“-”+endtimeDT.ToString(“h:mm tt”); } } 其他的 { 返回“至”+endtimeDT.ToString(“M/d/yy”); } } 其他的 { 返回starttimeDT.ToString(“h:mm tt”); } } }
请检查SelectCommand。它应该是:

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

请参见数据源的选择参数(而不是选择命令)?它指定仅获取id=1的行

删除该部分


更新:哎哟,实际上,将其更改为querystring参数并引用url中的id。然后将参数引用添加到Select命令中。

调试如何?我相信您只需调试就可以解决问题。如果没有,至少您可以与我们共享调试结果,我们可能会更好地帮助您。我认为实际上,他希望看到的不仅仅是“id=1”行,因此他需要删除这些参数并使用querystring参数。我不明白为什么给我-1,因为:-(。Where子句缺失,这是正确的答案。