It's rather simple in ASP.Net to create a generic FeedBack form. You can make
it as simple or as complicated as you need. The simplest form would be merely
a Form-To-Email version, which would ONLY send an email of the feedback to a
specific destination. In many cases, this would for many users. However, most
often, the reality is that sometimes emails get lost. Therefore, it would be
more preferable to enter the data from the form, and then, also send the
results by email. This way, if the email gets lost, there is still a record of
the FeedBack.
There are a couple of things to consider when starting the design process.
They are:
- Which database are you going to use?
- How many pieces of Information are you needing to capture?
- How do you want to interact with the data, once it's in the database?
You can use any you want in this scenario, of course, but we're only
covering SQL Server and MS Access. It really doesn't matter, in the long run.
For most ANSII sql compliant databases, everything is going to be the same,
except for the methods used to talk back and forth to the databases.
We also will only cover the basic fields needed in the form, but you can
make the form as simple or complex as needed for your own purposes, as stated
before. Just remember - for every field you have in the form, you should have
a corresponding field in the database table which will be capturing the
end-user's input.
The fields we'll be covering here are:
- Title
- Email Address (the address of the person entering the feedback,for
follow-up) - Feedback (multi-line textbox, sized as needed)
- Time Entered (automatic)
- Follow-up
You will need to create a database table with these fields - start with an
ID field (autonumbering in MS Access or Identity in SQL Server - set as
Primary Key also). Then, create the other fields however you'd like,
size-wise, making the Time Entered field a Date/Time field and in our case,
we'll make the Follow-up field very small so that a 'Yes' can be entered when
follow-up is finished concerning the feedback. If you'd like, you can add an
extra field for follow-up notes.
Here is how the Form will look on the page:
Title: <asp:TextBox id="txtTitle" Runat="server" /><br>
Email Address: <asp:TextBox id="txtEmail" Runat="server" /><br>
Feedback:<br>
<asp:TextBox id="txtFeedback" Rows="4" Width="500" TextMode="MultiLine" Runat="server" /><p>
<asp:Button id="btnSubmit" Text="Submit" onclick="doInsert" Runat="server" />
As you probably noticed, the last two items in the list (Time Entered and
Follow-Up) above are not included in the visual code for the feedback form.
That's because we will automatically get the system date and time and enter it
into the database when the data is inserted, and the Follow-Up field is not
for input, but obviously, as the name implies, for 'follow-up' - AFTER the
data has been captured from the form.
Now, next, we'll need an input sub that takes care of gathering the data
entered into the form. Here is the code for doing just that:
Sub doInsert(Source as Object, E as EventArgs)
Dim strConn as String = "server=YourServer;uid=sa;pwd=YourPWD;database=YourDB;"
Dim MySQL as string = "Insert into feedback (Title, Email, Feedback, dtEntered, Followup) " & _
"Values (@Title, @Email, @Feedback, @dtEntered)"
Dim MyConn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(MySQL, MyConn)
With Cmd.Parameters
.Add(New SQLParameter("@Title", frmTitle.text))
.Add(New SQLParameter("@Title", frmEmail.text))
.Add(New SQLParameter("@Feedback", frmFeedback.text))
.Add(New SQLParameter("@dtEntered", DateTime.Now()))
End With
MyConn.Open()
cmd.ExecuteNonQuery()
'Put your Success statement Here
'Create a label on the page and assign the success statement to the label's text value, like:
'Label1.Text="Your inormation has been successfully received. " & _
"We will get back to you as soon as possible"
'Remember to Import the SQLClient Namespace also
MyConn.Close
End Sub
For MS Access, your code would be:
Sub doInsert(Source as Object, E as EventArgs)
Dim strConn as String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
"server.mappath("\pathAndNameToYourDB.mdb") & ";"
Dim MySQL as string = "Insert into feedback (Title, Email, Feedback, dtEntered, Followup) " & _
"Values (@Title, @Email, @Feedback, @dtEntered)"
Dim MyConn as New OleDbConnection(strConn)
Dim Cmd as New OleDbCommand(MySQL, MyConn)
With Cmd.Parameters
.Add(New OleDbParameter("@Title", frmTitle.text))
.Add(New OleDbParameter("@Title", frmEmail.text))
.Add(New OleDbParameter("@Feedback", frmFeedback.text))
.Add(New OleDbParameter("@dtEntered", DateTime.Now()))
End With
MyConn.Open()
cmd.ExecuteNonQuery()
'Put your Success statement Here
'Create a label on the page and assign the success statement to the label's text value, like:
'Label1.Text="Your inormation has been successfully received." & _
"We will get back to you as soon as possible"
'Remember to Import the OleDb Namespace also
MyConn.Close
End Sub
The final piece of this will be to setup a SubRoutine for emailing the form
results to yourself (or a designated email address). Here, we'll just use a
text based (instead of HTML) email, for simplicity's sake. First we'll create
a string variable and assign the results of the feedback form to it:
Dim sMsg as String
sMsg= "Feedback has been sent to you." & vbcrlf & "The results are as follows:" & vbcrlf
sMsg+="Title : " & frmTitle.text & vbcrlf
sMsg+="Email : " & frmEmail.text & vbcrlf
sMsg+="Feedback : " & frmFeedback.text & vbcrlf
sMsg+="Time Entered : " & DateTime.Now()
The rest is fairly simple, as outlined in a previous Tutorial entitled
Emailing Form Results. Here's the whole subroutine:
Sub doEmail()
Dim sMsg as String
Dim sBody as String
sMsg= "Feedback has been sent to you." & vbcrlf & "The results are as follows:" & vbcrlf
sMsg+="Title : " & frmTitle.text & vbcrlf
sMsg+="Email : " & frmEmail.text & vbcrlf
sMsg+="Feedback : " & frmFeedback.text & vbcrlf
sMsg+="Time Entered : " & DateTime.Now()
Dim objEmail as New MailMessage
objEmail.To=YourEmail@wherever.com
objEmail.FROM=frmEmail.text
objEmail.SUBJECT="You have Feedback"
objEmail.BODY=sMsg
objEmail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer ="mail.Wherever.com"
SmtpMail.Send(objEmail)
End Sub
Lastly, just put a line inside the 'doInsert' sub, at the very end, in
order to send the email, once it's entered into the database:
doEmail()
As you can see, it's fairly straightforward to create a Feedback form in
ASP.Net. Your last step is only to customize it anyway in which you'd like for
your own purposes.