正在从QueryString ASP.NET检索表

本文关键字:NET 检索 ASP QueryString | 更新日期: 2024-09-20 03:34:28

我正在创建一个简单的查询,根据文本框中的ID获取一行数据。然而,它既没有检索信息,也没有出错。

我有一个文本框,其中填充了一个在URL中传递的querystring参数。这正在工作,并在页面上显示确切的ID。

我正在利用这一点将其其余信息获取到相关字段中。

C#

   protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)'v11.0; AttachDbFilename=C:'Users'Donald'Documents'Visual Studio 2013'Projects'DesktopApplication'DesktopApplication'Student_CB.mdf ;Integrated Security=True");
            con.Open();
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
                sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count > 0)
                    nameTxt.Text = dt.Rows[0][0].ToString();
                descriptionTxt.Text = dt.Rows[0][1].ToString();
                instructionsTxt.Text = dt.Rows[0][2].ToString();
                dt.Clear();
            }
            catch (Exception ex)
            {
            }
            con.Close();
        }

ASP.NET

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
        <h1><asp:Label ID="RecipeID" runat="server" ><%=Request.QueryString["id"] %></asp:Label></h1>
        <asp:Label ID="nameTxt" runat="server" Text="Name"></asp:Label>
    </hgroup>
            <table style="width:926px">
              <tr>
                <td class="auto-style2" > IMAGE </td>
                <td >
                    <asp:Panel ID="descriptionPnl" runat="server" BackColor="White" Height="160px" Width="472px">
                        <asp:Label ID="descriptionTxt" runat="server" Text="Label"></asp:Label>
                    </asp:Panel>
                  </td> 
              </tr>    
            </table>
    <h6> Step by Step Guide</h6>
            <table style="width:900px">
              <tr>
                <td >  
                    <asp:Panel ID="guidePnl" runat="server" BackColor="White" Height="200px" Width="900px">
                        <asp:Label ID="instructionsTxt" runat="server" Text="Label"></asp:Label>
                    </asp:Panel>
                  </td> 
              </tr>   
            </table>           
    </asp:Content>

有人能在这件事上帮我吗?我哪里出了问题,我需要添加或更改什么。非常感谢。

正在从QueryString ASP.NET检索表

它不会出错,因为您可以捕获所有异常,而不对其执行任何操作。

此外,使用该代码很容易受到sql注入的攻击(正如注释中正确指出的那样)。

您应该使用相对路径来定位数据库文件(部署时会中断),并将类似的配置信息放在Web.config文件中。

protected void Page_Load(object sender, EventArgs e)
        {
            string ID = Request.QueryString["id"];
            RecipeID.Text = ID;
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)'v11.0; AttachDbFilename=C:'Users'Donald'Documents'Visual Studio 2013'Projects'DesktopApplication'DesktopApplication'Student_CB.mdf ;Integrated Security=True");
            con.Open();
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
                sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count > 0)
                    nameTxt.Text = dt.Rows[0][0].ToString();
                descriptionTxt.Text = dt.Rows[0][1].ToString();
                instructionsTxt.Text = dt.Rows[0][2].ToString();
                dt.Clear();
            }
            catch (Exception ex)
            {
            }
            con.Close();
        }

sda.SelectCommand.Parameters.Add("@recipeid",SqlDbType.Int).Value=Request.QueryString["id"];