gpt4 book ai didi

c# - 指数数组的边界之外

转载 作者:行者123 更新时间:2023-11-29 04:57:14 25 4
gpt4 key购买 nike

我收到这个错误:

Index was outside the bounds of the array.

代码:

   private void PopulateWallPosts(string userId)
{

using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT wp.WallPostings, p.PicturePath FROM WallPosting wp INNER JOIN User u ON u.UserID = wp.FriendUserID INNER JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
//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
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(0)); //idwallposting
string id = Convert.ToString(div.ID);
//store the div id as a string
Image img = new Image();
img.ImageUrl = String.Format("{0}", reader.GetString(2)); //p.picturepath
img.AlternateText = "Test image";

div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp " + "{0}", reader.GetString(1)))); //wp.wallpostings
div.Attributes.Add("onclick", "confirm_delete(" + id + ");");
// send the div id to javascript
div.Style["clear"] = "both";
test1.Controls.Add(div);

}
}
}
}
}

enter image description here

最佳答案

GetString(2) 在您只选择 2 列时看起来很可疑:

SELECT wp.WallPostings, p.PicturePath FROM ...

因为它是从 0 开始的,所以应该是 GetString(1)

关于c# - 指数数组的边界之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com