Solved

Add values to database from repeater textbox's

Posted on 2010-09-14
4
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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