help doing a loop to save details

Last post 07-05-2009 4:16 AM by vn_nilesh@hotmail.com. 1 replies.

Sort Posts:

  • help doing a loop to save details

    06-29-2009, 4:53 PM
    • Member
      134 point Member
    • gbeford
    • Member since 08-28-2006, 6:43 PM
    • Massachusetts
    • Posts 222

     I am not sure how do the loop to save my details see below bolded... Can someone help ? PulseSurveyQuestions BuildClass() is where i loop through to get all the datat to save and then it passes to the save method...

      protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {           
                PulseSurveyQuestions PSQ = BuildClass();
               PSQ.saveSurveyAnswers(surveyType, sentID );
                clear();
            }
        }
       
       
         public PulseSurveyQuestions BuildClass()
            {
                PulseSurveyQuestions psq = new PulseSurveyQuestions();
                psq.User = _user;
                psq.Login = _login;
                psq.Location = _location;
                psq.Roll = _roll;
                psq.Sup = _sup;
                psq.SurveyType = surveyType;
                psq.State = ddlState.SelectedValue;
       
                if (ddlCity.SelectedValue != "Other")
                {
                    psq.City = ddlCity.SelectedValue;
                }
                else
                {
                    psq.City = txtOther.Text;
                }
       
                //pulls the controls from the place holder
                foreach (Control ctl in ph1.Controls)
                {
                    //looks to see if the control is a label           
                    if (ctl is Label)
                    {
                        // create a label to hold the label in the control
                        //in order to save the info.
                        Label lbl = (Label)ctl;
                        psq.Question = lbl.Text;
                        psq.QuestID = lbl.ID;
                    }
       
                    if (ctl is TextBox)
                    {
                        TextBox txt = (TextBox)ctl;
                        if (txt.ID == "txtRblOther")
                        {
                            psq.OtherAnswer = txt.Text;
                        }
                        else
                        {
                            psq.Answer = txt.Text ;
                        }
                    }
       
                    if (ctl is RadioButtonList)
                    {
                        RadioButtonList rbl = (RadioButtonList)ctl;
                        if (rbl.SelectedItem == null)
                        {
                            psq.Answer = null;
                        }              
                        else{
                             psq.Answer = rbl.SelectedItem.Text;
                        }            
                    }
                    if (ctl is RadComboBox)
                    {
                        RadComboBox ddl = (RadComboBox)ctl;
                        psq.Answer = ddl.SelectedItem.Text;
                    }
                }
       
                return psq;
            }

     public void saveSurveyAnswers(string surveyType, int sentID)
             {
            Charter.ReportingBase.Database db = new Charter.ReportingBase.Database("EDCCConnectString");
            //StringBuilder sb = new StringBuilder();

            try
            {
                db.AddParameter("@QuestID", -1);
                db.AddParameter("@AgentName", _user);
                db.AddParameter("@AgentLogin", _login);
                db.AddParameter("@AgentLoc", _location);
                db.AddParameter("@AgentRoll", _roll);
                db.AddParameter("@AgentSup", _sup);
                db.AddParameter("@DateAnswered", _questionAnswerDate);
                db.AddParameter("@SentID", sentID);
                db.AddParameter("@CustState", _state);
                db.AddParameter("@CustCity", _city);
                db.AddParameter("@SurveyType", _surveyType);
                int _pulseSurveyQuesID = int.Parse(db.ExecuteScalar("PLSS_SavePLSSquestionsMaster", CommandType.StoredProcedure, Charter.ReportingBase.Database.DbConnectionState.CloseOnExit).ToString());

                // loop through to save details
                db.AddParameter("@ID", -1);
                db.AddParameter("@pulseSurveyMasterID", _pulseSurveyQuesID);
                string[] QID = _questID.Split('~');
                db.AddParameter("@QuestID", QID);
                db.AddParameter("@Answer", _answer);
                db.AddParameter("@OtherAnswer", _otherAnswer);

               int _pulseSurveyDetailID = int.Parse(db.ExecuteScalar("PLSS_SavePLSSquestionsDetails", CommandType.StoredProcedure, Charter.ReportingBase.Database.DbConnectionState.CloseOnExit).ToString());

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    Thank you so much for all the help :)

    Gina
  • Re: help doing a loop to save details

    07-05-2009, 4:16 AM
    Answer

     Instead of looping through all the controls within the placeholder, you could use the FindControl method and typecast the control to appropriate type like textbox, radio button etc.

    Please refer to following MSDN article for more details about FindControl method

    http://msdn.microsoft.com/en-us/library/486wc64h.aspx

     

    Regards

    Nilesh
    http://nileshgule.blogspot.com


    Please mark this as "ANSWER" if it helps you
Page 1 of 1 (2 items)