由于代码显示多个记录 [英] Multiple records displayed due to code
本文介绍了由于代码显示多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
文本被添加到数据库并显示,但它显示了多个记录,并且它们没有按顺序降序。我在页面上显示了两组记录,插入的文本被下推到第一个多记录的底部。
我怎样才能纠正这个问题,所以我
$ b
代码:
pre>
protected void Page_Load(object sender,EventArgs e)
{
string theUserId = Session [UserID]。ToString();
PopulateWallPosts(theUserId);
}
private void PopulateWallPosts(string userId)
{
using(OdbcConnection cn = new OdbcConnection(Driver = {MySQL ODBC 3.51 Driver}; Server = localhost ; Database = gymwebsite2; User = x; Password = x;))
{
cn.Open();
using(OdbcCommand cmd = new OdbcCommand(SELECT Wallpostings FROM WallPosting WHERE UserID =+ userId +ORDER BY idWallPosting DESC,cn))
{
using(OdbcDataReader reader = cmd。 ExecuteReader())
{
while(reader.Read())
{
System.Web.UI.HtmlControls。 HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl(div);
div.Style [float] =left;
Image img = new Image();
img.ImageUrl =〜/ userdata / 2 / uploadedimage / batman-for-facebook.jpg;
img.AlternateText =测试图片;
div.Controls.Add(img);
test1.Controls.Add(div);
System.Web.UI.HtmlControls.HtmlGenericControl div1 = new System.Web.UI.HtmlControls.HtmlGenericControl(div);
div1.InnerText = String.Format({0},reader.GetString(0));
div1.Style [float] =left;
test1.Controls.Add(div1);
System.Web.UI.HtmlControls.HtmlGenericControl div2 = new System.Web.UI.HtmlControls.HtmlGenericControl(div);
div2.Style [clear] =both;
test1.Controls.Add(div2);
}
}
}
}
}
protected void Button1_Click(object sender,EventArgs e)
{
string theUserId = Session [UserID]。ToString();
using(OdbcConnection cn = new OdbcConnection(Driver = {MySQL ODBC 3.51 Driver}; Server = localhost; Database = gymwebsite2; User = x; Password = x;))
{
cn.Open();
using(OdbcCommand cmd = new OdbcCommand(INSERT INTO WallPosting(UserID,Wallpostings)VALUES(+ theUserId +,'+ TextBox1.Text +'),cn))
{
cmd.ExecuteNonQuery();
}
}
PopulateWallPosts(theUserId);
$ / code>
解决方案清除循环前的占位符控件:
test1.Controls.Clear();
while(reader.Read())
{
...
}
Hello Im getting multiple records displayed and they arent descending in order correctly when I insert on button click.
The text is added to the database and it is displayed but I get two sets of records displayed on the page and the text from the insert is pushed down to the bottom of the first multiple record.
How can I correct this so I only get one return on the page and the comment(text from db) is set to desc.
Code:
protected void Page_Load(object sender, EventArgs e)
{
string theUserId = Session["UserID"].ToString();
PopulateWallPosts(theUserId);
}
private void PopulateWallPosts(string userId)
{
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT Wallpostings FROM WallPosting WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Style["float"] = "left";
Image img = new Image();
img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
img.AlternateText = "Test image";
div.Controls.Add(img);
test1.Controls.Add(div);
System.Web.UI.HtmlControls.HtmlGenericControl div1 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div1.InnerText = String.Format("{0}", reader.GetString(0));
div1.Style["float"] = "left";
test1.Controls.Add(div1);
System.Web.UI.HtmlControls.HtmlGenericControl div2 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div2.Style["clear"] = "both";
test1.Controls.Add(div2);
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string theUserId = Session["UserID"].ToString();
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("INSERT INTO WallPosting (UserID, Wallpostings) VALUES (" + theUserId + ", '" + TextBox1.Text + "')", cn))
{
cmd.ExecuteNonQuery();
}
}
PopulateWallPosts(theUserId);
}
}
解决方案 Simply clear the placeholder control before the loop:
test1.Controls.Clear();
while (reader.Read())
{
...
}
这篇关于由于代码显示多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文