How to create a comment page in C #

In this article, we will discuss how to create a comment page in C # language or by using visual studio.net. It’s not a difficult task to do just you have some expertise in visual studio. net tool. We will use visual studio.net and SQL server to create this comment box or a guest page for visitors through which they can share their ideas and suggestions.

There are three main parts of this project and these are, User interface, coding, and database. In this project we will use visual studio 2010, you may also do all these tasks in visual studio.net 2008 and 2005.

  • User interface/ Designing Phase

The user interface is the most important part of the project you can also say it designing phase. Always try to design a simple and easy form or page, that will be easily accessible for the users and they can easily interact. We have designed a simple comment form for this project by using visual studio.net as shown below: We have used very few fields in this form, you can add more fields and can use it according to your needs by applying these methods.

  • Database

The database is another very important phase, all the data will be stored in the database. we will create a simple table for the above form as shown in the above image. One thing keeps in mind is just add those fields that are given in the form as you have added in the above form. If your database is not created then first of all you have to create a database then you will be able to add tables on it.  For creating a database just follow these steps:

  1. First of all, right-click on the App_Data folder in Solution Explorer and go to Add New Item, then go to SQL Server Database.
  2. You can use the default name as Database.MDF or can change it according to your needs.
  3. Once your database is created, go over to Server Explorer and expand the Database, then right-click the Tables folder and choose Add New Table.
  4. Here we will design our table for the comments by adding the required fields. For this example Name, Email Address, Contact No and comment, etc.

Here is a screenshot below: We have successfully created a table in the database. Now go to the designing page that should be built-in visual studio.net and use  ASP.NET Server Controls, these controls are FormView, DataGrid, and SqlDataSource. This will make it easier for us to allow comments added to the database by using the controls built-in. We want to display our visitor comments on the same page just below the post button, so just click on the Grid view and go to grid view tasks as shown below, then Choose Data Source just clicking on the drop-down arrow, select SQL database and then click OK button, all these procedures are shown below.

Now the next step is, you have to select String, just select ConnectionString and click on the NEXT button. Now the next step is very important, there will be some options you have to select specify columns from a table or view and follow below drop-down menu, here you have to select your desired table, for example, we have selected here, asp_comment this is our table name. So you will select those fields that you want to display in the comment box, for example here we have selected just name any comment we want to share these two fields. you can use more fields, so select your desired fields and go to the next button. There will be one more step, go to next and then finish. here is a screenshot is also given below: We have completed successfully data grid view process also, Once you have completed data bounding then just follow one more step that is to enable paging, this process will automatically generate different pages in your comment page if your comments will be exceeded. for that process, just go to the GridView tasks and click on Enable Paging, after clicking you will see page numbers on the bottom of Grid View as shown below:

We have successfully completed all the steps now open your designing phase as shown on the top of the page, double click on the button “Post Comment” and paste the below code, change SQL connection and use your database path as highlighted below.

SqlCommand cmd;

SqlDataReader reader;
SqlConnection conn = new SqlConnection(@”Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\asghar\documents\visual studio 2010\Projects\test\test\App_Data\Database1.mdf;Integrated Security=True;User Instance=True“);
cmd = new SqlCommand(” Select * from asp_comment Where email_address='” + txtname.Text + “‘”, conn);
conn.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
reader.Close();
conn.Close();
//txtEmail.Text = “”;

}
else
{
//conn.Open();
reader.Close();
cmd = new SqlCommand(“Insert into asp_comment(name,email_address,contact_no,comment) Values(‘” + txtname.Text + “‘,'” + txtemail.Text + “‘,'” + txtcontact.Text + “‘,'” + txtcmnt.Text + “‘)”, conn);
cmd.ExecuteNonQuery();
conn.Close();
lblsgnup.Text = “Your comment has been successfully posted”;

}
}

Now see what will be the output when any visitor post a comment, lets have a look

we are hoping you will create and develop your website comment page with no trouble. It’s very difficult to cover all the things in just one article but we have tried to share some of the main steps here hopefully will be helpful for you. Good luck

Leave a Reply

Your email address will not be published. Required fields are marked *