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. Its 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

User interface is 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:leave-a-commentWe 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

Data base is another very important phase, all the data will be stored in the data base. we will create a simple table for the above form as shown in the above image. One thing keep in mind 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 database then you will be able to add tables on it.  For creating 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 required fields. For this example Name, Email Address, Contact No and comment etc.

Here is a screen shot below:adding-table-in-databaseWe 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, thesecontrols 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 displays our visitor comments on the same page just below 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 drop down arrow, select SQL database and then click OK button, all these procedures are shown below.databoundNow the next step is, you have to select String, just select ConnectionString and click on 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 and comment we want to share these two fields. you can use more fields, so select your desired fields and go the next button. There will be one more step, go to next and then finish. here is a screen shot also given below:databound2We have completed successfully data grid view process also, Once you have completed data bounding then just follow one more step that is enable paging, this process will automatically generate different pages in your comment page if your comments will be exceed. for that process, just go the GridView tasks and click on Enable Paging, after clicking you will see page numbers on the bottom of Grid View as shown below:databound4We 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 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 out put when any visitor post a comment, lets have a look

outputcommentspage

we are hoping you will create and develop your website comment page with no any trouble. Its very difficult to cover all the things in just one article but we have try 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.