开发者

sql snytax error, and string issue related to sqlsyntax

开发者 https://www.devze.com 2023-02-21 10:50 出处:网络
Hey guys im getting an sqlsyntax error when I ad开发者_开发知识库d in idWallPosting to my select statement in my code below:

Hey guys im getting an sqlsyntax error when I ad开发者_开发知识库d in idWallPosting to my select statement in my code below:

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

            }
        }
    }

My db looks like this:

sql snytax error, and string issue related to sqlsyntax

In my code im trying to set the div.ID to the current idWallPosting in my WallPosting table so im also not sure I have that correct either.

EDIT:

Error:

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

Related to this line I think:

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


You need a comma. This:

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))

Should be this:

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))

Comma: ----------------------------------------------------------------------------------------------------^

EDIT:

You may try GetValue instead of GetString. The description for GetValue:

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


wallPosting is an int and you are using .GetString. You should be using .GetInt32 probably.

0

精彩评论

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