开发者

Pass NavigateUrl Id to next page Sql statement

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
        {
       开发者_Python百科     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.


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"]);


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
    }
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜