Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add values to database from repeater textbox's

Posted on 2010-09-14
4
Medium Priority
?
638 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

609 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