How to create a Feedback Web User Control form in C# (Part 1)

While creating this technical blog website, I decided to implement the functionality that allows readers to give feedback. This is a common function. What is not common is finding examples of how to create a feedback form. As well, a feedback form that can be resued inside of each blog using a web control. So here it is, an example of creating a Feedback form as a web user control (.ascx file) in C#.

Let’s start by designing our database table. I tend to think about the data I want to capture now like NAME, DATETIME, FEEDBACK and also plan for some growth in functionality at some point in the future. I will include EMAIL, URL, etc in my database, just in case I want to enhace my form in the future.

image

Let’s create a Web.config file, ConnectionManager class and a DataAccessComponent (DAC) class to begin with. Seperatining your code into different layers promotes reuse and thereby increases the speed of new development and maintenance. Example: I write my ConnectionManager only once, each time I need a connection to the database I call this function. If the database changes, I only need to change my connection in a single place. Had I created a connection locally, each time I execute a query, it would result in much more time and effort to make that change.




The web.config file is where I will store my connection.

<?xml version="1.0"?>
 <configuration>
  <connectionStrings>
   <add name="tbcspitw-pro" 
        connectionString="Data Source=SERVER;Initial Catalog=DB;Persist Security Info=True;User ID=*;Password=*"/>
 </connectionStrings>
</configuration>

From within the ConnectionManager class I will use the ConfigurationManager to get the datasource out of the web.config. Then I will make the connection to the database.

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace TBCSPITW
{
     internal sealed class ConnectionManager
     {
          public static SqlConnection GetConnection()
          {
               // Build the connection string from Web.Config file
               string connectionString = ConfigurationManager.ConnectionStrings["tbcspitw-pro"].ConnectionString;

               SqlConnection connection = new SqlConnection(connectionString);

               connection.Open();
               return connection;
          }
     }
} 

image

I will then create 2 methods within my DAC:

  1. InsertFeedback – used when someone posted a response to my blog
  2. DisplayFeedback – used when the blog is loaded to show responses

Note the use of the USING clause. The objects within this clause will be deallocated once the execution leaves the brackets. Making them available for Garbage collection. When you use the USING clause, you do not have to formally close your objects, it is done for you.

using System;
using System.Data;
using System.Data.SqlClient;

namespace TBCSPITW
{

     public sealed class FeedbackDAC
     {
          public static bool InsertFeedbackComment(string BLOGID, string COMMENTID, string SUBJECT, string COMMENT,
               string EMAIL, string NAME, string PASS, string URL)
          {
               bool success = false;

               string sql = "INSERT INTO FEEDBACK (BLOGID, COMMENTID, SUBJECT, COMMENT, EMAIL, NAME, PASS, URL) " +
                    "VALUES (@prmBLOGID ,@prmCOMMENTID, @prmSUBJECT, @prmCOMMENT, @prmEMAIL, @prmNAME, @prmPASS, @prmURL) ";

               using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
               {
                    command.Parameters.Add("@prmBLOGID", SqlDbType.NVarChar).Value = BLOGID;
                    command.Parameters.Add("@prmCOMMENTID", SqlDbType.NVarChar).Value = COMMENTID;
                    command.Parameters.Add("@prmSUBJECT", SqlDbType.NVarChar).Value = SUBJECT;
                    command.Parameters.Add("@prmCOMMENT", SqlDbType.NVarChar).Value = COMMENT;
                    command.Parameters.Add("@prmEMAIL", SqlDbType.NVarChar).Value = EMAIL;
                    command.Parameters.Add("@prmNAME", SqlDbType.NVarChar).Value = NAME;
                    command.Parameters.Add("@prmPASS", SqlDbType.NText).Value = PASS;
                    command.Parameters.Add("@prmURL", SqlDbType.NVarChar).Value = URL;

                    int rowsAffected = command.ExecuteNonQuery();
                    success = (rowsAffected == 1);
               }
               return success;
          }

          public static SqlDataReader GetFeedback(string BLOGID)
         {
               SqlDataReader reader;

               string sql = "SELECT COMMENT, SUBJECT, NAME, TIMESTAMP FROM FEEDBACK " +
               "WHERE BLOGID = @prmBLOGID";

               using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
               {
                     command.CommandType = CommandType.Text;
                     command.Parameters.Add("@prmBLOGID", SqlDbType.NVarChar).Value = BLOGID;
                     reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
                }
               return reader;
          }
     }
} 

I then create the web user control (.ascx) and code it almost exactly like I would any other windows form (.aspx).




1 Comment

 Add your comment
  1. Excеllent post. I useԁ to be checking continuously this weblog and
    I am impressed! Extremely helpful іnfo partіcularly
    the final ρhase 🙂 I handle such information a lot.
    I was lookіng for thiѕ certain іnfo for a very lengthy time.
    Thanks and good luck.

Leave a Comment

Your email address will not be published.