2011-03-30 63 views
0

嗨相關的字符串的問題,當我在idWallPosting在我下面的代碼添加到我的select語句即時得到一個sqlsyntax錯誤:SQL snytax錯誤,並sqlsyntax

using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn)) 
    { 
     using (OdbcDataReader reader = cmd.ExecuteReader()) 
     { 
      test1.Controls.Clear(); 

      while (reader.Read()) 
      { 

       System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div"); 
       div.Attributes["class"] = "test"; 


       div.ID = String.Format("{0}", reader.GetString(2)); 
       // this line is responsible, problem here and my sqlsntax, im trying to set the SELECT idWallPosting for the div ID 
       Image img = new Image(); 
       img.ImageUrl = String.Format("{0}", reader.GetString(1)); 

       img.AlternateText = "Test image"; 

       div.Controls.Add(img); 
       div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp " + "{0}", reader.GetString(0)))); 
       div.Attributes.Add("onclick", "return confirm_delete();"); 

       div.Style["clear"] = "both"; 
       test1.Controls.Add(div); 

      } 
     } 
    } 

我的數據庫看起來是這樣的:

enter image description here

在我的代碼即時嘗試將div.ID設置爲我的WallPosting表中的當前idWallPosting,所以即時通訊也不知道我有沒有更正。

編輯:

錯誤:

Unable to cast object of type 'System.Int32' to type 'System.String'.

相關的這條線,我認爲:

div.ID = String.Format("{0}", reader.GetString(2)); 
+2

讓我們開始吧,在idwallposting之後是否缺少逗號? – nycdan 2011-03-30 17:14:10

回答

1

你需要一個逗號。這:

using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn)) 

應該是這樣的:

using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn)) 

逗號:---------------------------- -------------------------------------------------- ----------------------^

編輯:

您可以嘗試GetValue,而不是GetString。 GetValue的說明:

Gets the value of the column at the specified ordinal in its native format.

+1

大聲笑,這是得到代表這是錯誤的。 – nycdan 2011-03-30 17:16:06

+0

哈哈真的,但我有一個新的錯誤壽,所以如果你也可以幫助我編輯我的文章,並會給出正確的答案遺憾的是,我失明像蝙蝠一樣盲目。 – 2011-03-30 17:17:23

+1

是嗎?有5分鐘的時間和[Google](http://www.google.com)可以找到本網站的一半問題。人們仍然一直得到那些代表。 – FreeAsInBeer 2011-03-30 17:17:35

0

wallPosting是一個int,並且您正在使用.GetString。可能你應該使用.GetInt32。

+0

嘗試了同樣的事情 – 2011-03-30 17:20:39