Solved

Add values to database from repeater textbox's

Posted on 2010-09-14
4
623 Views
Last Modified: 2012-05-10
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
Comment
Question by:GrantBailey
  • 2
  • 2
4 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33674502
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
 

Author Comment

by:GrantBailey
ID: 33674711
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33675076
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
 

Author Closing Comment

by:GrantBailey
ID: 33683237
Once again, you are excellent at assisting us wanna be coders.
Thanks so much for you help guru_sami
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now