插入嵌套循环中的问题 [英] problem in Insertion on nested loops
问题描述
您好我有一个场景,我在插入值时使用多个for循环。找到我的下面的代码,我需要插入一个Qid作为其中一个paraemeter ..
Hi i have an scenario where i am using multiple for loops while inserting an values..Find my below code where i need to insert an Qid as one of the paraemeter among the others..
protected void Button1_Click(object sender, EventArgs e)
{
string s = "";
string str = string.Empty;
int id = Convert.ToInt32(Session["id"]);
int id1 = Convert.ToInt32(Request.QueryString["id"]);
foreach (RepeaterItem item in DataList1.Items)
{
TextBox txt = (TextBox)item.FindControl("TextBox1");
string ss = txt.Text;
if (String.IsNullOrEmpty(ss))
{
s = "empty field";
}
else
{
s = txt.Text;
jaya j = new jaya();
DataTable dt2 = j.fillquestionuser(id1);
if (dt2.Rows.Count > 0)
{
foreach (DataRow dr in dt2.Rows)
{
Qid = Convert.ToInt32(dt2.Rows[0]["id"].ToString());
}
}
}
einstein en = new einstein();
en.submitanswer(id, id1, s, Qid);
//Qid = Convert.ToInt32(str);
MessageBox msg = new MessageBox();
msg.Show("Assignment answer submitted successfully");
}
}
如果我的dt2由列id组成27和28两行,同时插入它在我的表中只插入27次,而不是单独插入27和28 ...请找到任何解析
if my dt2 consists of column id as 27 and 28 two rows,while inserting it inserts only 27 2 times in my table instead off 27 and 28 individually...pls find any soolution
推荐答案
foreach (DataRow dr in dt2.Rows)
{
//Qid = Convert.ToInt32(dt2.Rows[0]["id"].ToString()); //this should be
Qid = Convert.ToInt32(dr["id"].ToString(); // this will work.
}
你正在做什么总是从dt2.Rows [0]开始,所以它总会返回你的第一个值。当你循环通过表通过foreach你不必写dt2.Rows [0]而不是你可以使用dr。:)
What you are doing is always taking from dt2.Rows[0] so it will always return you the first value. As you are looping through table via foreach u dont have to write dt2.Rows[0] instead you can make use of dr. :)
试试这个,它会工作
Qid = Convert.ToInt32(d
Try this,It will work
Qid = Convert.ToInt32(d
r["id"].ToString());
更改你的forach循环如下:
change your forach loop like Below:
foreach (DataRow dr in dt2.Rows)
{
Qid = Convert.ToInt32(dr["id"].ToString());
}
这篇关于插入嵌套循环中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!