Asp.net获取数据库第一条内容

问题遇到的现象和发生背景

写入SelectCommand="SELECT * FROM [news] WHERE ([id] = @id)"后,原来的显示内容没了,想要他默认显示第一条数据

遇到的现象和发生背景,请写出第一个错误信息

原来的显示内容没了

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%

这个是newsdetail界面

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="newsdetail.aspx.cs" Inherits="WebApplication1.newsdetail" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style>
        #dtop {
            text-align: center;
            width: 800px;
            margin-top: 20px;
            border-bottom: 1px solid grey;
        }

        div#dtime {
            width: 800px;
            margin-top: 10px;
            text-align: center;
        }
    style>


asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>

            <div id="dtop">
                <asp:Label ID="titleLabel" runat="server"  Text='<%# Eval("title")  %>' />
            div>
            <div id="dtime">
                <asp:Label ID="createTimeLabel" runat="server" Text='<%# Eval("createTime") %>' />

            div>
            <div id="dcontent">
                <asp:Label ID="contentsLabel" runat="server" Text='<%# Eval("contents") %>' />

            div>

        ItemTemplate>
    asp:DataList>



    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WeCat52ConnectionString %>" SelectCommand="SELECT  * FROM [news] WHERE ([id] = @id)">
        <SelectParameters>
            <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
        SelectParameters>
    asp:SqlDataSource>



asp:Content>



这个是首页index

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style>
        #nLeft {
            float: left;
            width: 200px;
        }

        #nRight {
            float: left;
            width: 400px;
        }
        #nLeft a{
            text-decoration:none;
            display:inline-block;
            width:200px;
        }
        #nLeft a:hover{
            background:grey;
            

        }
    style>

asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <div id="nLeft">
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "newsdetail.aspx?id="+Eval("id") %>' Text='<%# Eval("title") %>'>asp:HyperLink>
            div>
            <div id="nRight">
                <asp:Label ID="createTimeLabel" runat="server" Text='<%# Eval("createTime") %>' />
            div>
        ItemTemplate>
    asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WeCat52ConnectionString %>" SelectCommand="SELECT top 10 * FROM [news] order by id desc">asp:SqlDataSource>

asp:Content>



运行结果及详细报错内容

无报错

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

不知道怎么调

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

newsdetail界面直接打开获取第一条数据,从index首页打开选择对应的新闻能跳转到newsdetail界面对应的id(已经实现)

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "newsdetail.aspx?id="+Eval("id") %>' Text='<%# Eval("title") %>'>

既然你是通过querystring 来实现你的需求的。那你可以在newsdetail页面的后台代码中定义一个静态变量,设置其初始值为1来接收ID.如果你从querystring里找不到你传递的参数,那么就使用默认值1.否则就替换id值。

SelectParameters里怎么只有id没有value,那你给@id传的值到底是什么呀