Im trying to create a webform that is a representation of a paper questionnaire that has 14 questions with yes/no answers. If an answer is no, then you must provide information as to why in 3 separate textboxes and store the information only when a user clicks no.

I also have 4 drop down lists that are chosen before the user answers the first question that also need to be stored with the textboxes, but the ddl's value does not change after the user starts. Right now I have 3 textboxes and a submit button for each of the 14 questions that are hidden until a user hits a "no" button, in which I change the visible property of all of them and the user then types in their reasons and submits the answer. I am currently doing this for each question and i hide/disable the textboxes and button after submitted.

My problem is that for each event on the 14 submit button's is that I'm forced to open & close an sql connection and insert the values. I know there has to be a way to simply this some so that I dont have 14 different groups of the same code. I would also like a way to eliminate having 40 textboxes on a page lol, because the idea was to have a pop up box or something open that would let the user submit it and then return to the question instead of hiding textboxes, but the user has to see all 14 questions at page load so if you got an answer to that one then that might solve my original question. After finishing a mock up of this I was utterly ashamed to look at my code and knew this couldnt be the only way but I just cant figure it out, any help would be appreciated!

Here is an example of something similar if you have a hard time understanding all that garbage.
Two separate questions, 2 separate answers. Please ignore errors, I am just trying to give a quick example.

Private Sub [B]btnQuestion_One[/B](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuestion_One.Click
    
Command.CommandText = "INSERT into Customers Values @reason1, @reason2, @reason3, @cust_id" 

Command.Parameters.Add("@reason1", [B]textbox1.text[/B]) 
Command.Parameters.Add("@reason2", [B]textbox2.text[/B]) 
Command.Parameters.Add("@reason3", [B]textbox3.text[/B]) 
Command.Parameters.Add("@cust_id", ddlCustomer.SelectedItem)

Conn.Open
Command.ExecuteNonQuery
Conn.Close

textbox1.Visible = False
textbox2.Visible =False
textbox3.Visible =False
'etc.....

End Sub

Private Sub [B]btnQuestion_Two[/B](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuestion_Two.Click

Command.CommandText = "INSERT into Customers Values @reason1, @reason2, @reason3, @cust_id" 

Command.Parameters.Add("@reason1", [B]textbox4.text[/B]) 
Command.Parameters.Add("@reason2", [B]textbox5.text[/B]) 
Command.Parameters.Add("@reason3", [B]textbox6.text[/B]) 
Command.Parameters.Add("@cust_id", ddlCustomer.SelectedItem)

CN.Open
Command.ExecuteNonQuery
CN.Close

textbox4.Visible = False
textbox5.Visible =False
textbox6.Visible =False
'etc.....

End Sub

Dani AI

Generated

Short version: replace 14 repeated button handlers and 40 visible textboxes with a single reusable input UI and one server-side save routine. ’s current approach duplicates connection/command logic in every Click handler; ’s InputBox idea is for desktop apps (WinForms/VB), not ASP.NET WebForms — a client-side modal or a single server-side handler is the right fit for the web.

Suggested pattern (high level):

  • Render the 14 questions from a Repeater/ListView so each item carries its QuestionId (CommandArgument / data-key).
  • When a question gets a "No", open one modal dialog (client-side) populated with question text and a hidden QuestionId and the currently selected customer id.
  • Submit the modal either with AJAX to a single WebMethod/handler, or with a standard postback to a single ItemCommand/handler. That handler calls one shared Save routine that does the DB work.

Example server-side helper (VB.NET):

Private Sub SaveQuestionResponse(questionId As Integer, r1 As String, r2 As String, r3 As String, customerId As Integer)
    Dim sql As String = "INSERT INTO QuestionResponses (QuestionId, Reason1, Reason2, Reason3, CustomerId) VALUES (@q,@r1,@r2,@r3,@c)"
    Using cn As New SqlConnection(myConnectionString)
        Using cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@q", questionId)
            cmd.Parameters.AddWithValue("@r1", r1)
            cmd.Parameters.AddWithValue("@r2", r2)
            cmd.Parameters.AddWithValue("@r3", r3)
            cmd.Parameters.AddWithValue("@c", customerId)
            cn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Troubleshooting and cautions: always use parameterized queries (avoid string concatenation), dispose connections/commands (Using), validate and sanitize client input server-side, and prefer typed Parameters over AddWithValue for production. If JavaScript is acceptable, use a modal + AJAX to avoid full page reloads; otherwise an UpdatePanel or a single ItemCommand handler will keep the server-side flow simple. This removes duplicated UI and SQL code while preserving the ddl selection per session.

I would also like a way to eliminate having 40 textboxes on a page lol, because the idea was to have a pop up box or something open that would let the user submit it and then return to the question instead of hiding textboxes

Hi,

You can try the inputbox for that.
To work with the InputBox, drag a Command Button and a TextBox from the Toolbar. Opon clicking the Command Button the InputBox prompts asks for a name. Once entered, press OK. That text will now be displayed in the Textbox.

The sample code for the click event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles Button1.Click
Dim Show As String
Show = InputBox("Enter your name")
TextBox1.Text = Show
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.