Click here to Skip to main content
15,909,503 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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..

C#
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");
}
        }


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
Posted
Comments
Subrata.In 11-Jan-13 2:01am    
If you got your answer it's ok. However your question is not clear. If it's possible give an output example.
On your loop Qid will replace current rows value, so try to use Qid+=Convert.ToInt32(dt2.Rows[0]["id"].ToString());

C#
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.
}


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. :)
 
Share this answer
 
Try this,It will work

Qid = Convert.ToInt32(d
C#
r["id"].ToString());
 
Share this answer
 
v2
change your forach loop like Below:
C#
foreach (DataRow dr in dt2.Rows)
{
    Qid = Convert.ToInt32(dr["id"].ToString());
}
 
Share this answer
 
what are you trying to do with this loop
C#
foreach (DataRow dr in dt2.Rows)
{
   Qid = Convert.ToInt32(dt2.Rows[0]["id"].ToString());
}


when it finishes the looping 'Qid' hold the last row data and you are using the last Qid in the following lines
C#
en.submitanswer(id, id1, s, Qid);


If you want to insert each QiD move the above code inside dt2.Rows looping.
C#
foreach (DataRow dr in dt2.Rows)
{
  Qid = Convert.ToInt32(dr["id"].ToString());
  en.submitanswer(id, id1, s, Qid);
}
 
Share this answer
 
v2
However, Your Qid is holding only one data at a time. In this case, possibly the last "id" in dt2.
Two Solutions

1. Use an Array of Qid and while passing Qid as parameter in submitanswer(), use foreach for inserting
like
C#
foreach id in Qid[]
    en.submitanswer(id, id1, s, Qid[id])

2. Move
C#
en.submitanswer(id, id1, s, Qid);
to the foreach loop above
like
C#
foreach (DataRow dr in dt2.Rows)
{
  Qid = Convert.ToInt32(dr["id"].ToString());
  en.submitanswer(id, id1, s, Qid);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900