locked
For loop RRS feed

  • Question

  • User1754860325 posted

     I have 6 TextBoxes id as txt0 txt1 txt2 ... txt5 and 6 labels also id: lbl0 ...lbl5.

    I am trying to loop these 6 txt and lbl's according to a maximun of 6 rows in a sql database but in the code below where I put txt[0]  it throws an error.

     

     MembershipUser currentUser = Membership.GetUser();
            Guid customerId = (Guid)currentUser.ProviderUserKey;
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection(@"Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=****; password=****");
            SqlCommand sqlCmd = new SqlCommand("SELECT Staff.FirstName + ' ' + Staff.Surname AS Name, LkupVehicleAssignment.VehicleAssignment FROM StaffVehicleAssignments INNER JOIN LkupVehicleAssignment ON StaffVehicleAssignments.VehicleAssignmentID = LkupVehicleAssignment.VehicleAssignmentID INNER JOIN Staff ON StaffVehicleAssignments.Userid = Staff.Userid WHERE (ESVID = @rv) ORDER BY StaffVehicleAssignments.VehicleAssignmentID", conn);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
     
            sqlCmd.Parameters.AddWithValue("@username", customerId);
            sqlCmd.Parameters.AddWithValue("@rv", ddlRespondingVehicles.SelectedValue);
            sqlCmd.Parameters.AddWithValue("@id", "1");
            sqlDa.Fill(dt);
     
            if (dt.Rows.Count > 0)
            {
                for (var i = 0; i <= dt.Rows.Count; i++)
                {
                    lbl[i].Text = dt.Rows[i]["VehicleAssignment"].ToString();
                    lbl[i].Visible = true;
                    txt[i].Text = dt.Rows[i]["Name"].ToString();
                    txt[i].Visible = true;
                }
            }

     What is the best method on doing this.

     

     

     

    Sunday, August 25, 2013 11:17 AM

Answers

  • User-183374066 posted

    gmboat

    if (dt.Rows.Count > 0)
            {
                for (var i = 0; i <= dt.Rows.Count; i++)
                {
                    lbl[i].Text = dt.Rows[i]["VehicleAssignment"].ToString();
                    lbl[i].Visible = true;
                    txt[i].Text = dt.Rows[i]["Name"].ToString();
                    txt[i].Visible = true;
                }
            }

    Change your for loop to

    if (dt.Rows.Count > 0)
    {
        for (var i = 0; i <= dt.Rows.Count; i++)
        {
            var txt = Page.FindControl("txt" + i.ToString()) as TextBox;
            var lbl = Page.FindControl("lbl" + i.ToString()) as Label;
            if (txt != null && lbl != null)
            {
                lbl.Text = Convert.ToString(dt.Rows[i]["VehicleAssignment"]);
                lbl.Visible = true;
                txt.Text = Convert.ToString(dt.Rows[i]["Name"]);
                txt.Visible = true;    
            }
        }
    }


    Regards.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 25, 2013 12:33 PM

All replies

  • User-1360095595 posted

    What's lbl and txt supposed to be? You're using them as if they are arrays or lists. What are they? 

    Sunday, August 25, 2013 12:30 PM
  • User1754860325 posted

    They are textboxes and labels and I am trying to populate them with names that is on the database.

    Sunday, August 25, 2013 12:32 PM
  • User-183374066 posted

    gmboat

    if (dt.Rows.Count > 0)
            {
                for (var i = 0; i <= dt.Rows.Count; i++)
                {
                    lbl[i].Text = dt.Rows[i]["VehicleAssignment"].ToString();
                    lbl[i].Visible = true;
                    txt[i].Text = dt.Rows[i]["Name"].ToString();
                    txt[i].Visible = true;
                }
            }

    Change your for loop to

    if (dt.Rows.Count > 0)
    {
        for (var i = 0; i <= dt.Rows.Count; i++)
        {
            var txt = Page.FindControl("txt" + i.ToString()) as TextBox;
            var lbl = Page.FindControl("lbl" + i.ToString()) as Label;
            if (txt != null && lbl != null)
            {
                lbl.Text = Convert.ToString(dt.Rows[i]["VehicleAssignment"]);
                lbl.Visible = true;
                txt.Text = Convert.ToString(dt.Rows[i]["Name"]);
                txt.Visible = true;    
            }
        }
    }


    Regards.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 25, 2013 12:33 PM