2016-01-20 15 views
0

字符串查詢checkuser未連接產品ID以獲取選擇項, 可能是其解析問題。在asp.net中從ID獲取產品時出錯

結果顯示查詢字符串是一個整體而不是項目。

Checkout按鈕代碼:

public partial class WebSite1_checkout : System.Web.UI.Page 
{ 
    List<item> li = new List<item>(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     for(int i=1; i<=Convert.ToInt32(Session["count"]) ;i++) 
     { 
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString); 
      conn.Open(); 
      item items = new item(); 
      items.Id = Session["id" + i].ToString(); 
      string checkuser = "select prod_name from productt where prod_id='" + Convert.ToInt32(Session["id"]) + "'"; 


      SqlCommand com = new SqlCommand(checkuser, conn); 
      //string temp = Convert.ToInt32(com.ExecuteScalar().ToString()); 
      items.Name = checkuser; 

      li.Add(items); 
     } 
     GridView1.DataSource = li; 
     GridView1.DataBind(); 

    } 
+0

有一兩件事要嘗試的是什麼是調試時items.Id的價值? –

+0

它只顯示0,並顯示整個字符串的查詢,而不是取回項目 –

+1

好吧,我明白我認爲你需要執行sql命令。目前我看不到你在執行命令的地方,所以你的項目列表只是持有一個字符串。此鏈接可能有所幫助https://msdn.microsoft.com/en-us/library/tyy0sz6b(v=vs.110).aspx –

回答

0

我看不到任何錯誤代碼。你確定Session [「id」]不是null嗎?

+0

是會議不是空的,完美的工作,但沒有通過價值查詢 –

+1

「結果顯示查詢字符串作爲一個整體,但不是項目。」按結果你是指items.Name? – Boldan

0
for(int i=1; i<=Convert.ToInt32(Session["count"]) ;i++) 
     { 
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString); 
      conn.Open(); 
      item items = new item(); 
      items.Id = Session["id" + i].ToString(); 
      string checkuser = "select prod_name from productt where prod_id=" + Convert.ToInt32(Session["id" + i]) ; 
      SqlCommand com = new SqlCommand(checkuser, conn); 
      string temp= (com.ExecuteScalar()).ToString(); 
      items.Name = temp; 






      li.Add(items); 

     } 

,這是工作的代碼....感謝所有

相關問題