2017-07-21 6 views
-2
public partial class Questions : System.Web.UI.Page 
{ 

    private bool InstanceFieldsInitialized = false; 

    private Questions() 
    { 
     if (!InstanceFieldsInitialized) 
     { 
      InitializeInstanceFields(); 
      InstanceFieldsInitialized = true; 
     } 
    } 

    private void InitializeInstanceFields() 
    { 
     r = User.Identity.Name; 
    } 

    private ArrayList @params = new ArrayList(); 
    string r; 
    private ArrayList pklist = new ArrayList(); 

    //Dynamically Loads the Questions based off of the table. 
    protected void Page_Load(object sender, System.EventArgs e) 
    { 
     //Questions Loaded 

     string proc = "Question_Select"; 
     SqlConnection QuestionsConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Feedback-ConnectionString"].ConnectionString); 
     SqlCommand QuestionsCommand = new SqlCommand(proc, QuestionsConnection); 
     QuestionsCommand.CommandType = CommandType.StoredProcedure; 
     SqlDataAdapter da = new SqlDataAdapter(QuestionsCommand); 
     DataTable vTable = new DataTable(); 
     da.Fill(vTable); 




     Table detailsTable = new Table(); 
     detailsTable.CellSpacing = 10; 
     var i = 0; 

     foreach (DataRow row in vTable.Rows) 
     { 
      var value = vTable.Rows[i][0].ToString(); 
      TableRow tRow = new TableRow(); 
      TableCell tCell1 = new TableCell(); 
      TableCell tCell2 = new TableCell(); 
      var pk = vTable.Rows[i][1].ToString(); 
      pklist.add(pk); 

      Label myLabel = new Label(); 

      myLabel.CssClass = "bold"; 

      TextBox myText = new TextBox(); 

      myText.TextMode = TextBoxMode.MultiLine; 
      myText.Columns = 40; 
      myText.Rows = 4; 
      myText.ID = "Box" + i; 
      @params.add(myText.ID); 

      myLabel.Text = string.Format("{0}. {1}", i + 1, value); 
      tCell1.Controls.Add(myLabel); 
      tCell2.Controls.Add(myText); 
      tRow.Cells.Add(tCell1); 
      tRow.Cells.Add(tCell2); 

      detailsTable.Rows.Add(tRow); 
      i += 1; 
     } 
     Panel1.Controls.Add(detailsTable); 

    } 


    //INSTANT C# WARNING: Strict 'Handles' conversion only applies to fields declared in the same class - the event will be wired in 'SubscribeToEvents': 
    //ORIGINAL LINE: Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    protected void Button1_Click(object sender, EventArgs e) 
    { 

     //When a survey is completed, this method checks to see who completed the survey and insert them into a table. 
     string proc2 = "User_Insert"; 
     SqlConnection ResponsesConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Feedback-ConnectionString"].ConnectionString); 
     SqlCommand ResponsesCommand2 = new SqlCommand(proc2, ResponsesConnection2); 
     ResponsesCommand2.CommandType = CommandType.StoredProcedure; 

     ResponsesCommand2.Parameters.AddWithValue("@UserName", r); 
     ResponsesConnection2.Open(); 
     ResponsesConnection2 = (SqlConnection)ResponsesCommand2.ExecuteScalar(); 

     // Establish connection and set up command to use stored procedure 
     string proc = "Responses_Insert"; 
     SqlConnection ResponsesConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Feedback-ConnectionString"].ConnectionString); 
     SqlCommand ResponsesCommand = new SqlCommand(proc, ResponsesConnection); 
     ResponsesCommand.CommandType = CommandType.StoredProcedure; 

     ResponsesConnection.Open(); 

     <--HERE IS WHERE I THINK THE PROBLEM IS--> 
     ***var i = 0; 
     for (int a = 0; a < @params.ToString().Count(); a++) 
     { 
      ResponsesCommand.Parameters.Clear(); 
      TextBox textbox = Panel1.FindControl(@params.ToString().ElementAt(a).ToString()) as TextBox; 
      ResponsesCommand.Parameters.AddWithValue("@QuestionID", pklist.ToString().ElementAt(i).ToString()); 
      ResponsesCommand.Parameters.AddWithValue("@response", textbox.Text); 
      ResponsesCommand.Parameters.AddWithValue("@UserName", r); 
      ResponsesConnection = (SqlConnection)ResponsesCommand.ExecuteScalar(); 
      i += 1;*** 

     } 


     Microsoft.VisualBasic.Interaction.MsgBox("Thank you for completing the survey.", Microsoft.VisualBasic.MsgBoxStyle.MsgBoxSetForeground, "Survey Complete"); 



     Response.Redirect("../../../Default.aspx"); 

    } 
    //Button to see if a person has chosen to be marked annonymous. 
    //INSTANT C# WARNING: Strict 'Handles' conversion only applies to fields declared in the same class - the event will be wired in 'SubscribeToEvents': 
    //ORIGINAL LINE: Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles Checkbox1.CheckedChanged 
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     if (Checkbox1.Checked == false) 
     { 
      r = User.Identity.Name; 
     } 
     else 
     { 
      r = "Anonymous"; 
     } 
    } 
} 

}C# zeigt doppelte Ausgabe

Ich brauche ein wenig Hilfe. Mein Code zeigt doppelte Ausgabe, wenn er ausgeführt wird. Hatte mehrere Möglichkeiten, um var, object oder dynamic mit Code-Konvertern zu verwenden. Wählen Sie var, aber es gibt immer noch doppelt aus. Wenn der Debugger ausgeführt wird, werden keine Fehler angezeigt.

+0

Was meinen Sie mit Doppelausgabe? – FortyTwo

+2

Debugger zeigen keine Fehler an. Debugger helfen Dir *** *** die Fehler zu finden. Aber du machst das Finden. –

+0

Es gibt mehr als doppelte Ausgabe falsch mit Ihrem Code ... – maccettura

Antwort

1

Ich würde fragen, ob Sie .ToString() in der folgenden (in der for-Schleife) benötigen:

a < @params.ToString().Count() 

Es scheint, wie Sie wahrscheinlich nur die Anzahl der Elemente in der Liste @params wollen, so versuchen Ersetzen Sie die obigen mit folgenden:

Das kann Ihr Problem der Schleife die falsche Anzahl von Malen zu beheben. Wenn ja, dann in ähnlicher Weise innerhalb der Schleife sollten Sie wahrscheinlich nicht in einer dieser beiden mit .ToString() sein:

@params.ToString().ElementAt(a) 
pklist.ToString().ElementAt(i) 

Wenn Ihre Schleife immer noch zu oft zu sein scheint die Ausführung, dann sollten Sie versuchen, durch Schritt es im Debugger oder Hinzufügen von Print-Anweisungen (Console.WriteLine), um die Variablenwerte für jede Iteration der Schleife zu drucken und besser zu verstehen, was es tut.