Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

Add values to database from repeater textbox's

Hello All,

I have a repeater which displays a textbox for each record. This is a survey and each question is yes or no. If no is selected  I display a textbox for further explanation. So what I'm stumped on is getting the values from the textbox and assigning it to a variable so I can update the database. The question are pulled from the database and bound to the repeater. There may be 1 to 10 questions. Here is a snip it of some my code. I may be taking the wrong approach?


'// I know this will print out the values from each textbox 

For Each item As RepeaterItem In myRepeater.Items
            Dim noResonpse As TextBox = TryCast(item.FindControl("questionExplained"), TextBox)
Response.Write(noResonpse.Text + "<br>")
'// I could use a sp to update each record at a time but I would rather do just one update for the answers
Next

Here is the db update

        strConn = ConfigurationManager.ConnectionStrings("SiteConnectionString").ConnectionString
        Dim conn As New SqlConnection(strConn)
        Dim insertSurvey As New SqlCommand("spSurvey", conn)
        insertSurvey.CommandType = CommandType.StoredProcedure
        insertSurvey.Parameters.AddWithValue("@Name", name.Text)
        insertSurvey.Parameters.AddWithValue("@date", date.Text)
        '// I would like to add each textbox 
        insertSurvey.Parameters.AddWithValue("@question1", explained1Txt.Text)
        insertSurvey.Parameters.AddWithValue("@question2", explained2Txt.Text)
 
        conn.Open()

Open in new window

0
GrantBailey
Asked:
GrantBailey
  • 2
  • 2
1 Solution
 
guru_samiCommented:
I am getting what you are asking, but not completely.
So how is your table structure that is going to store the response? Will be there 10 fields one for each textbox?
Upon insert, you are going to store all responses in one record or a record per question?

A wild guess would be get the values and store it in a dictionary<key,value> object. Then loop through it and add it to your insertSurvery Paramters
0
 
GrantBaileyAuthor Commented:
Well hello  again guru_sami

The table structure is way simplified.

id
name
answer1 'yes/no field
answer1explained
answer2
answer2explained
answer3
answer3explained

and so on....

Here is the repeater code. I assumed once I learned to populate the fields for the textboxes it would be easy to understand how to do the same with the radio buttons.

I was expecting to need to send blank value to the fields not used. So if there are only 9 questions then answer10explained = ""




<asp:Repeater ID="myRepeater" runat="server" >
                         <ItemTemplate>
        <div style="padding-top:20px;">
        <div style="float:left; width:20px;"><%#DataBinder.Eval(Container, "ItemIndex", "") + 1%>.</div>
        <div style="float:left; width:362px;"><%#DataBinder.Eval(Container, "DataItem.question")%></div>
        <div style="float:right;">
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="question1" Text="Yes" /><asp:RadioButton ID="RadioButton2"
            runat="server" GroupName="question1"  Text="No"  />

            
        </div>
        <div id="no_<%#DataBinder.Eval(Container, "ItemIndex", "")+1%>" class="noExplained" style="margin-top:10px; margin-left:20px; float:left;">
                    <asp:Label ID="Label2" runat="server" Text="Please Explain"></asp:Label>
                        <br />
<asp:TextBox ID="question1Explained" runat="server" Height="72px" TextMode="MultiLine" 
                            Width="453px"></asp:TextBox>
                    </div>
                   
        </div>
        <div style="clear:both;" ></div>
        </ItemTemplate>
                    </asp:Repeater>

Open in new window

0
 
guru_samiCommented:
Take a look at this sample code and see if it makes sense.
Note:
1: I am assuming your parameters are name @question1,@question2, so on for storing the textbox values.
2: The textbox id is "questionExplained"

'This dictionary object will store values from all textboxes inside the repeater
Dim answers As New Dictionary(Of String, String)()
Dim counter As Integer = 0
For i As Integer = 0 To myRepeater.Items.Count - 1
      'Start counter with 1 assuming your paramters are @question1,@question2
      counter = i + 1
      Dim tb As TextBox = DirectCast(myRepeater.Items(i).FindControl("questionExplained"), TextBox)
      If tb IsNot Nothing Then
            answers.Add("@question" & counter.ToString(), tb.Text)
      Else
            'This is to add empty value in case textbox is not found
            answers.Add("@question" & counter.ToString(), [String].Empty)
      End If
Next

'to add those values to insert paramters in your update code

For Each answer As KeyValuePair(Of String, String) In answers
      insertSurvey.Parameters.AddWithValue(answer.Key, answer.Value)
Next
0
 
GrantBaileyAuthor Commented:
Once again, you are excellent at assisting us wanna be coders.
Thanks so much for you help guru_sami
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now