I am developing a simple online suggestion application where the user can fill a form with his suggestion and submit it directly to the system. The system will send this suggestion to the admin via email. I developed it like the form of the Contact Us page. Now, I want, in addition to sending the suggestion by email, to store the suggestion in the database in order to list all the suggestions in one page.
I created the following table in the database:
SuggestionLog Table: ID, Title, Description, Username
(ID is the primary key and Username is a foreign key to the Users table)
My ASP.NET Code is:
<div ID="contactform">
<ol>
<li>
<label for="subject">
Subject</label>
<asp:TextBox ID="txtSubject" runat="server" CssClass="text"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtSubject"
ErrorMessage="Please enter a subject/title for your suggestion"></asp:RequiredFieldValidator>
</li>
<li>
<label for="message">
Your Suggestion</label>
<asp:TextBox ID="txtSuggestion" runat="server" cols="50" CssClass="textarea"
rows="6" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtSuggestion" ErrorMessage="Please enter your suggestion"></asp:RequiredFieldValidator>
</li>
<li class="buttons">
<asp:ImageButton ID="imageField" runat="server" imageurl="images/Send.gif"
OnClick="btnSubmit_Click" />
</li>
</ol>
</div>
and my code-behind is:
protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
SmtpClient sc = new SmtpClient("MAIL ADDRESS");
StringBuilder sb = new StringBuilder();
MailMessage msg = null;
sb.Append("Message : " + txtSuggestion.Text + "\n");
try
{
msg = new MailMessage("",
"xxxxxxxxxx@gmail.com", "Message from PSSP System",
sb.ToString());
sc.Send(msg);
MultiView1.SetActiveView(ViewConfirm);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
}
Now, how can I store the suggestion in the database at the same time that it will be sent to the Admin when the user clicks on the Send button?
First of all, your code-behind should be dedicated to building your front-end (view). It shouldn’t be troubled with writing/sending emails or database INSERTs. You should really create another tier (controller) to handle that.
Anyway, to update the database (assuming SQL Server here), you’ll need to pass your title, desc, and username to a function where you’ll create a connection object. You’ll then open it, create a command object with your INSERT SQL, add your parameters, and EXECUTE it. It’ll look something like this:
ID is omitted because I’m assuming that your ID field would be an autonumber, so the DB would generate a value for you. If not, you’ll need to pass that along as well. Also, this code assumes a custom error log function referenced by “writeToLog”.
I should also mention that your sql connection info should be retrieved from your web.config, not hard-coded into the function.