Problem building string whilst looping through con

Results 1 to 2 of 2

Thread: Problem building string whilst looping through con

  1. #1
    Join Date
    Dec 1969

    Default Problem building string whilst looping through con

    I&#039;m trying to write a function that loops through all the controls on a web form, and puts the values entered by the user into a string, which will then be passed into the database using a stored procedure.<BR><BR>For some reason that I simply can&#039;t fathom, the stringbuilder class loses the string it contains at certain points during the cycle through the controls on the page. The page contains 5 panel objects, and the stringbuilder loses the values stored in it when the "GetControlValues" function moves from one panel to the next. Why would this be?<BR><BR>Hope I&#039;ve explained this well enough - all code is below - can anyone suggest what I&#039;m doing wrong? Thanks for any help :)<BR><BR><BR><BR>Public Function GetControlValues(s as Object, SqlStr As String) <BR> <BR> Dim ctl As Control<BR> Dim ReturnValue As String<BR> Dim SqlStringProp As String<BR> Dim StringBuild As New Stringbuilder<BR> <BR> StringBuild.Append(SqlStr)<BR> response.write("&#060;b&#062;Stringbuilder BEFORE:&#060;/b&#062; " & StringBuild.ToString()) &#060;&#060;--- Stringbuilder has a value here<BR> <BR> For each ctl in s.Controls<BR> <BR> response.write("&#060;b&#062;Stringbuilder DURING:&#060;/b&#062; " & StringBuild.ToString()) &#060;&#060;-- But is empty here *only* at the begining of a new panel<BR> <BR> ReturnValue=ShowControlDetails(ctl)<BR> <BR> If Len(ReturnValue) &#062; 0 Then<BR> <BR> StringBuild.Append(" &#039;")<BR> StringBuild.Append(ReturnValue)<BR> StringBuild.Append("&#039;") <BR> <BR> Else<BR> <BR> response.write("&#060;b&#062;ReturnValue:&#060;/b&#062; No value returned")<BR> <BR> End If<BR> <BR> response.write("&#060;b&#062;Stringbuilder AFTER:&#060;/b&#062; " & StringBuild.ToString()) <BR> <BR> GetControlValues(ctl, SqlStringProp)<BR> <BR> Next<BR><BR>End Function<BR><BR><BR><BR>Public Function ShowControlDetails(ctlControl As Object)<BR><BR> Dim SqlString As String<BR> <BR> Select Case ctlControl.GetType().ToString()<BR> <BR> Case "System.Web.UI.WebControls.TextBox"<BR> SqlString=ctlControl.Text.ToString()<BR> <BR> Case "System.Web.UI.WebControls.DropDownList"<BR> SqlString=ctlControl.SelectedItem.Text.ToString()< BR> <BR> Case Else<BR> response.write("&#060;b&#062;This control isn&#039;t relevant?&#060;/b&#062;")<BR> End Select<BR> <BR> return SqlString<BR><BR>End Function

  2. #2
    Join Date
    Dec 1969

    Default RE: Problem building string whilst looping through

    Are you not Instantiatimng a new Instance of the Stringbuilder class?<BR><BR>You may want to do some reading on the "Singleton Pattern"<BR><BR>HTH.<BR>D.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts