将 NavigateUrl Id 传递给下一页 Sql 语句

发布于 2024-12-09 09:58:07 字数 1242 浏览 0 评论 0原文

我正在 asp.net 中编码“列出所有产品”页面。我做了从数据库到 ViewList 的连接。现在我必须使产品可点击。到目前为止,我在 asp 部分中编码的内容如下:

<div class="image">
      <asp:HyperLink ID="HyperLinkSaleDesign" runat="server" NavigateUrl='<%# Eval("ID" ,  "~/EN/ViewTemplate.aspx?id={0}") %>'>
          <asp:Image ID="ImageSaleDesign" runat="server" Width="247" Height="150" ImageUrl='<%# Eval("thumb") %>' />
            </asp:HyperLink>
</div>

导航 URL 有效,我可以看到所选的“?id={0}”。 但是我无法正确传递数据,因此下一页的 SQL 查询不起作用。

我不确定如何将此值传递给 Select 语句。这是我到目前为止所做的:

    String IDquery = ("QueryStringParameter[ID]"); // doesn't work

        try
        {
            string ConnectionString = WebConfigurationManager.ConnectionStrings["Twebconfig"].ConnectionString;
            SqlConnection viewTemplate = new SqlConnection(ConnectionString);

            SqlDataAdapter viewTemplateSet = new SqlDataAdapter("SELECT " +
            " * FROM saleDesigns WHERE ID = @IDquery", viewTemplate); // doesn't seem to see the variable

            Data Binding - etc. etc. etc
        }
        catch (Exception err)
        {
            mylabel.Text = "Invalid " + err.Message;
        }

我愿意接受任何建议。 谢谢。

I am coding "List all Product" page in asp.net. I did the connection from the DB to ViewList. Now I have to make the products clickable. What I have coded so far in the asp part is , as it follows:

<div class="image">
      <asp:HyperLink ID="HyperLinkSaleDesign" runat="server" NavigateUrl='<%# Eval("ID" ,  "~/EN/ViewTemplate.aspx?id={0}") %>'>
          <asp:Image ID="ImageSaleDesign" runat="server" Width="247" Height="150" ImageUrl='<%# Eval("thumb") %>' />
            </asp:HyperLink>
</div>

The navigation URL works and I can see the selected "?id={0}".
However I cannot pass the data correctly , so the SQL query on the next page does not work.

I am not sure how to pass this value to the Select statement. Here is what I have done so far:

    String IDquery = ("QueryStringParameter[ID]"); // doesn't work

        try
        {
            string ConnectionString = WebConfigurationManager.ConnectionStrings["Twebconfig"].ConnectionString;
            SqlConnection viewTemplate = new SqlConnection(ConnectionString);

            SqlDataAdapter viewTemplateSet = new SqlDataAdapter("SELECT " +
            " * FROM saleDesigns WHERE ID = @IDquery", viewTemplate); // doesn't seem to see the variable

            Data Binding - etc. etc. etc
        }
        catch (Exception err)
        {
            mylabel.Text = "Invalid " + err.Message;
        }

I am open to any suggestions.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-12-16 09:58:07

我对你的代码有点困惑,但看起来你只需要从 QueryString 检索 id 并用它构建 SQL 查询,对吧?

这是您想要做的吗?:

int id = int.Parse(Request.QueryString["id"]);

您可以使用 QueryString 中的 ID 将查询放在一起,您可以执行如下操作:

//just an example - should be parameterized to avoid injection
string query = String.Format("SELECT ID, Col1, Col2 FROM Table1 WHERE ID={0}", Request.QueryString["ID"]);

I'm a bit confused by your code, but it looks like you just need to retrieve the id from QueryString and build a SQL query with it, right?

Is this what you're trying to do?:

int id = int.Parse(Request.QueryString["id"]);

You can put the query together using the ID from QueryString, you can do something like this:

//just an example - should be parameterized to avoid injection
string query = String.Format("SELECT ID, Col1, Col2 FROM Table1 WHERE ID={0}", Request.QueryString["ID"]);
巡山小妖精 2024-12-16 09:58:07

这就是它对我有用的方式:

    int v = 0;
try
{
   int v = int.Parse(Request.QueryString["id"]);
}
catch (Exception e)
{}
        if (v > 0)
        {

            try
            {
                string ConnectionString = WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
                SqlConnection conn = new SqlConnection(ConnectionString);//explisionremoteEntities

                string query = String.Format("SELECT * FROM table WHERE ID={0}", Request.QueryString["ID"]);
                SqlDataAdapter viewTemplateSet = new SqlDataAdapter(query, conn);
    }
    catch
    {
        //code in here
    }
     }

This is the way it works for me:

    int v = 0;
try
{
   int v = int.Parse(Request.QueryString["id"]);
}
catch (Exception e)
{}
        if (v > 0)
        {

            try
            {
                string ConnectionString = WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
                SqlConnection conn = new SqlConnection(ConnectionString);//explisionremoteEntities

                string query = String.Format("SELECT * FROM table WHERE ID={0}", Request.QueryString["ID"]);
                SqlDataAdapter viewTemplateSet = new SqlDataAdapter(query, conn);
    }
    catch
    {
        //code in here
    }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文