Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Discussion Forum Board - Beta 1

Add Comment
 

 
<div align="center"> <table border="0" width="90%" class="outline"> <tr> <td width="26%" class="outline"><b>Download File</b></td> <td width="14%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="26%" class="outline"><a href="../../file/forum1.zip" class="wbox">forum1.zip</a> (29kb)</td> <td width="14%" class="outline">Beta1</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> I had received a few requests for building an ASP.NET Forum. So I decided to build a ASP.NET forum board which has support of HTML message posting. This is a beta release of the forum and has very few features (more shall come when I have time :) ). <b> Mahesh Chand</b> (of C# Corner) has helped me out with the database design. This example use C# scripting with ADO.NET to build a ASP.NET Web Application. You will get to learn a lot of ADO.NET in this example.</p> <p><span class=wboxheado>Explanation</span><br> This example consists of 3 pages.<br> 1) forum.aspx - The Main Forum page. It lists all the topics in a DataGrid object with <b> Paging</b> enabled. It also contains a form to post new entries.<br> 2) reply.aspx - This page is called from the forum page when you click on any topic. It contains the details of the topic as well as all replies made to the topic. This page contains the form to reply to the topic.<br> 3) postmessage.aspx - This page is called whenever a form is posted. It stores the posted data into the Database.&nbsp;</p> <p><span class=wboxheado>Requirements</span><br> 1) .NET SDK beta 1 (Note: This example may not work with future versions of the SDK).<br> 2) Ms Access 2000 (Optional, only if you want to edit the database).<br> 3) ASP.NET support on your web server.</p> <p align="justify"><span class=wboxheado>Database Structure</span></p> <table width="90%" border="0" class="outline"> <tbody> <tr> <td width="100%" colSpan="3" class="outline"><span class=wboxhead>Table Name - newpost / This table will contain the new topics posted.</span></td> </tr> <tr> <td width="20%" class="outline"><b>Column Name&nbsp;</b></td> <td width="15%" class="outline"><b>Data Type</b></td> <td class="outline"><b>Description</b></td> </tr> <tr> <td width="20%" class="outline">postid&nbsp;</td> <td width="15%" class="outline">Integer</td> <td class="outline">The primary key field. The unique id for each new topic.</td> </tr> <tr> <td width="20%" class="outline">name</td> <td width="15%" class="outline">Text</td> <td class="outline">The name of the author of the message.</td> </tr> <tr> <td width="20%" class="outline">email</td> <td width="15%" class="outline">Text</td> <td class="outline">E-mail address of the author.</td> </tr> <tr> <td width="20%" class="outline">subject</td> <td width="15%" class="outline">Text</td> <td class="outline">Subject of the message.</td> </tr> <tr> <td width="20%" class="outline">ip</td> <td width="15%" class="outline">Text</td> <td class="outline">IP address of the author.</td> </tr> <tr> <td width="20%" class="outline">date</td> <td width="15%" class="outline">Text</td> <td class="outline">Date / Time of the Post</td> </tr> <tr> <td width="20%" class="outline">message</td> <td width="15%" class="outline">Memo</td> <td class="outline">Message posted.</td> </tr> <tr> <td width="20%" class="outline">replies</td> <td width="15%" class="outline">Integer</td> <td class="outline">Number of replies to the post (if any)</td> </tr> <tr> <td width="20%" class="outline">views</td> <td width="15%" class="outline">Integer</td> <td class="outline">Number of times the message has been viewed.&nbsp;</td> </tr> </tbody> </table> <br> <table width="90%" border="0" class="outline"> <tbody> <tr> <td width="99%" colSpan="3" class="outline"><span class=wboxhead>Table Name- reply / This table will contain the replies made to the new topics.</span></td> </tr> <tr> <td width="20%" class="outline"><b>Column Name</b></td> <td width="15%" class="outline"><b>Data Type</b></td> <td class="outline"><b>Description</b></td> </tr> <tr> <td width="20%" class="outline">replyid</td> <td width="15%" class="outline">Integer</td> <td class="outline">The primary key field. The unique id for each reply.</td> </tr> <tr> <td width="20%" class="outline">name</td> <td width="15%" class="outline">Text</td> <td class="outline">Name of the author.</td> </tr> <tr> <td width="20%" class="outline">email</td> <td width="15%" class="outline">Text</td> <td class="outline">E-mail address of the author.</td> </tr> <tr> <td width="20%" class="outline">subject</td> <td width="15%" class="outline">Text</td> <td class="outline">Subject of the post</td> </tr> <tr> <td width="20%" class="outline">ip</td> <td width="15%" class="outline">Text</td> <td class="outline">IP of the author.</td> </tr> <tr> <td width="20%" class="outline">date</td> <td width="15%" class="outline">Text</td> <td class="outline">Date / Time of post.</td> </tr> <tr> <td width="20%" class="outline">message</td> <td width="15%" class="outline">Memo</td> <td class="outline">Message posted.</td> </tr> <tr> <td width="20%" class="outline">postid</td> <td width="15%" class="outline">Integer</td> <td class="outline">postid from the &quot;newpost&quot; table for which this message is a reply.</td> </tr> </tbody> </table><P><span class=wboxheado>Code</span><BR>1) <b> forum.aspx</b> :- The main forum page. </P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;%@ Page Language=&quot;C#&quot; Debug=&quot;true&quot; %&gt; &lt;%@ Assembly Name=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data.ADO&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;html&gt;&lt;head&gt; &lt;title&gt;Welcome to My Forum!&lt;/title&gt; &lt;script language=&quot;C#&quot; runat=&quot;server&quot; &gt; <span class=cmt>//execute this script when the page loads</span> void Page_Load(Object Src, EventArgs E) { <span class=cmt>//Call the Method to DataBind the DataGrid</span> Binding() ; } <span class=cmt>//This Method Connects to the Database, and DataBinds the Database //to the DataGrid </span> public void Binding() { <span class=cmt>//String to connect to the database, //If your Database is in some other directory then change the path //To the Database below </span> string strConn=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ; Data Source=&quot;+Server.MapPath(&quot;.\\db\\board.mdb&quot;) ; <span class=cmt>//Make a Connection to the Database</span> ADOConnection myConn = new ADOConnection(strConn) ; <span class=cmt>//String to select records from the Database. //I have used &quot;ORDER BY postid DESC&quot; since I want to show the //latest post on the top. If you remove this clause then //the oldest message will be shown first</span> string strCom = &quot;SELECT postid ,subject ,name ,replies ,views &quot;; strCom += &quot;,date FROM newpost ORDER BY postid DESC&quot; ; <span class=cmt>//Open the Connection, Always remember to Open the connection //before doing anything else</span> myConn.Open(); DataSet myDataSet = new DataSet(); <span class=cmt>//Create a ADODataSetCommand and a DataSet</span> ADODataSetCommand myCommand =new ADODataSetCommand(strCom,myConn); <span class=cmt>//Fill the DataSet</span> myCommand.FillDataSet(myDataSet,&quot;newpost&quot;) ; <span class=cmt>//Connection is closed</span> myConn.Close(); <span class=cmt>//Set the DataView of the Table &quot;newpost&quot; contained in the //DataSet for the DataGrid</span> DataGrid1.DataSource = myDataSet.Tables[&quot;newpost&quot;].DefaultView ; <span class=cmt> //DataBind the DataGrid</span> DataGrid1.DataBind(); } <span class=cmt> //This method is called when the DataGrid is Paged //(i.e. when you change from Page 1 to Page 2 etc.. )</span> public void DataGrid_Updt(Object sender, DataGridPageChangedEventArgs e) { <span class=cmt>//Call the Method to Databind</span> Binding(); } <span class=cmt>//This Method is called when the form is submitted to make a new Post</span> public void Submit_Click(Object sender, EventArgs e) { <span class=cmt>//proceed only if all the required fields are filled-in</span> if(Page.IsValid&amp;&amp;name.Text!=&quot;&quot;&amp;&amp;subject.Text!=&quot;&quot;&amp;&amp;email.Text!=&quot;&quot;){ <span class=cmt> //Get the Current Date and Time</span> DateTime now = DateTime.Now ; errmess.Text=&quot;&quot; ; <span class=cmt>//building a custom query, used to call postmessage.aspx //Encode the query into UTF8 format. //Get all the fields from the form and encode them</span> string req=&quot;name=&quot;+System.Web.HttpUtility.UrlEncodeToString(name.Text, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;email=&quot;+System.Web.HttpUtility.UrlEncodeToString(email.Text, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;subject=&quot;+System.Web.HttpUtility.UrlEncodeToString(subject.Text, System.Text.Encoding.UTF8); <span class=cmt>//Get the HostAddress of the Author</span> req+=&quot;&amp;&amp;ip=&quot;+System.Web.HttpUtility.UrlEncodeToString( Request.UserHostAddress.ToString(),System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;date=&quot;+System.Web.HttpUtility.UrlEncodeToString(now.ToString(), System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;message=&quot;+System.Web.HttpUtility.UrlEncodeToString(message.Text, System.Text.Encoding.UTF8); <span class=cmt>//A 'yes' is used below to tell the postmessage page //that this is a new topic post</span> req+=&quot;&amp;&amp;newpost=&quot;+System.Web.HttpUtility.UrlEncodeToString(&quot;yes&quot;, System.Text.Encoding.UTF8); <span class=cmt>//call the postmessage.aspx page and append the query to it.</span> Page.Navigate(&quot;postmessage.aspx?&quot; + req); } else { errmess.Text=&quot;Fill in all the Required Fields before Posting!&quot;; } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt;&lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot;&gt; &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;center&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;COLOR:#ff0000&quot; runat=&quot;server&quot; /&gt; &lt;asp:Label class=fodark text=&quot;&lt;font color=#00000 &gt; Welcome to My Discussion Forum&lt;/font&gt;&quot; runat=server /&gt; &lt;br&gt; &lt;br&gt; &lt;form method=&quot;post&quot; runat=&quot;server&quot; ID=Form1&gt; <span class=cmt>&lt;%-- The DataGrid settings. Its very interesting how much you can play with it --%&gt;</span> &lt;asp:DataGrid id=DataGrid1 runat=&quot;server&quot; ForeColor=&quot;Black&quot; PagerStyle-Mode=&quot;NumericPages&quot; OnPageIndexChanged=&quot;DataGrid_Updt&quot; PageSize=&quot;20&quot; AllowPaging=&quot;True&quot; width=&quot;80%&quot; autogeneratecolumns=&quot;False&quot;&gt; &lt;property name=&quot;PagerStyle&quot;&gt; &lt;asp:DataGridPagerStyle BackColor=&quot;Coral&quot; Mode=&quot;NumericPages&quot;&gt; &lt;/asp:DataGridPagerStyle&gt; &lt;/property&gt; &lt;property name=&quot;AlternatingItemStyle&quot;&gt; &lt;asp:TableItemStyle BorderColor=&quot;#FFC080&quot; BackColor=&quot;#FF9966&quot;&gt; &lt;/asp:TableItemStyle&gt; &lt;/property&gt; &lt;property name=&quot;FooterStyle&quot;&gt; &lt;asp:TableItemStyle ForeColor=&quot;White&quot; BackColor=&quot;DarkOrange&quot;&gt; &lt;/asp:TableItemStyle&gt; &lt;/property&gt; &lt;property name=&quot;ItemStyle&quot;&gt; &lt;asp:TableItemStyle BackColor=&quot;Moccasin&quot;&gt; &lt;/asp:TableItemStyle&gt; &lt;/property&gt; &lt;property name=&quot;HeaderStyle&quot;&gt; &lt;asp:TableItemStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BackColor=&quot;Coral&quot;&gt; &lt;/asp:TableItemStyle&gt; &lt;/property&gt; <span class=cmt>&lt;%-- I am setting up the Individual columns using Templates --%&gt;</span> &lt;property name=&quot;Columns&quot;&gt; <span class=cmt>&lt;%-- Manipulate the subject entry so that it contains a link to the reply page --%&gt;</span> &lt;asp:TemplateColumn HeaderText=&quot;Subject&quot; itemstyle-width=50%&gt; &lt;template name=&quot;ItemTemplate&quot; &gt; &lt;asp:Label runat=&quot;server&quot; Text='&lt;%#&quot;&lt;a href=reply.aspx?postid=&quot; +DataBinder.Eval(Container, &quot;DataItem.postid&quot;)+&quot;&gt;&quot; +DataBinder.Eval(Container, &quot;DataItem.subject&quot;)+&quot;&lt;/a&gt;&quot; %&gt;' ID=Label2&gt; &lt;/asp:Label&gt; &lt;/template&gt; &lt;/asp:TemplateColumn&gt; &lt;asp:TemplateColumn HeaderText=&quot;Author Name&quot; itemstyle-width=20%&gt; &lt;template name=&quot;ItemTemplate&quot;&gt; &lt;asp:Label runat=&quot;server&quot; Text='&lt;%# DataBinder.Eval(Container, &quot;DataItem.name&quot;) %&gt;' ID=Label3&gt; &lt;/asp:Label&gt; &lt;/template&gt; &lt;/asp:TemplateColumn&gt; &lt;asp:TemplateColumn HeaderText=&quot;Replies&quot; itemstyle-width=10%&gt; &lt;template name=&quot;ItemTemplate&quot;&gt; &lt;asp:Label runat=&quot;server&quot; width=10% Text='&lt;%# DataBinder.Eval(Container, &quot;DataItem.replies&quot;) %&gt;' ID=Label4&gt; &lt;/asp:Label&gt; &lt;/template&gt; &lt;/asp:TemplateColumn&gt; &lt;asp:TemplateColumn HeaderText=&quot;Views&quot; itemstyle-width=10%&gt; &lt;template name=&quot;ItemTemplate&quot;&gt; &lt;asp:Label runat=&quot;server&quot; width=10% Text='&lt;%# DataBinder.Eval(Container, &quot;DataItem.views&quot;) %&gt;' ID=Label5&gt; &lt;/asp:Label&gt; &lt;/template&gt; &lt;/asp:TemplateColumn&gt; &lt;asp:TemplateColumn HeaderText=&quot;Date of Post&quot; itemstyle-width=10%&gt; &lt;template name=&quot;ItemTemplate&quot;&gt; &lt;asp:Label runat=&quot;server&quot; width=10% Text=' &lt;%# DataBinder.Eval(Container, &quot;DataItem.date&quot;).ToString(). ToDateTime().ToShortDateString()%&gt;' ID=Label6&gt;&lt;/asp:Label&gt; &lt;/template&gt; &lt;/asp:TemplateColumn&gt; &lt;/property&gt; &lt;/asp:DataGrid&gt; &lt;br&gt; &lt;br&gt; &lt;asp:Label class=fodark text=&quot;&lt;font color=#00000 &gt;Post New Topic&lt;/font&gt;&quot; runat=server /&gt; &lt;br&gt; &lt;table border=&quot;0&quot; width=&quot;80%&quot; align=&quot;center&quot;&gt; &lt;tr &gt; &lt;td class=&quot;fohead&quot; colspan=2&gt;&lt;b&gt;Post New Topic&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot; &gt; &lt;td&gt;Name :&lt;/td&gt; &lt;td &gt;&lt;asp:textbox text=&quot;&quot; id=&quot;name&quot; runat=&quot;server&quot; /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#ff0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;E-Mail :&lt;/td&gt; &lt;td&gt;&lt;asp:textbox text=&quot;&quot; id=&quot;email&quot; runat=&quot;server&quot;/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#ff0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt; Subject:&lt;/td&gt; &lt;td&gt;&lt;asp:textbox test=&quot;&quot; id=&quot;subject&quot; width=200 runat=&quot;server&quot;/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#ff0000&gt;*&lt;/font&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;Message&amp;nbsp;:&lt;/td&gt; &lt;td&gt; &lt;asp:TextBox id=message runat=&quot;server&quot; Columns=&quot;30&quot; Rows=&quot;15&quot; TextMode=&quot;MultiLine&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=folight&gt; &lt;td colspan=2&gt; &lt;asp:Button class=fodark id=write onClick=Submit_Click runat=&quot;server&quot; Text=&quot;Submit&quot;&gt; &lt;/asp:Button&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/center&gt; &lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt;&lt;/html&gt;</pre> </td> </tr> </table><P>&nbsp;</P> <P>2) <b> reply.aspx</b> : The topic viewing and replying page.</P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;%@ Page Language=&quot;C#&quot; EnableSessionState=&quot;False&quot; Debug=&quot;True&quot; %&gt; &lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;%@ Assembly Name=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data.ADO&quot; %&gt; &lt;html&gt;&lt;head&gt; &lt;title&gt;Post New Topic.&lt;/title&gt; <span class=cmt>&lt;%-- These are the imported assemblies and namespaces needed --%&gt;</span> &lt;script Language=&quot;C#&quot; runat=&quot;server&quot;&gt; DataSet ds ,rs; DataRow dr ; string postid ; public void Page_Load(object sender , EventArgs e) { <span class=cmt>//Check if the page is Post Back </span> if(!Page.IsPostBack) { <span class=cmt>//Get the postid from the Query string</span> postid = Request.Params[&quot;postid&quot;] ; if(postid!=null) { <span class=cmt>//Database connection string. Change the path to //the database file if you have some other path where //you are saving your Database file</span> string strConn=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ; Data Source=&quot;+Server.MapPath(&quot;.\\db\\board.mdb&quot;) ; <span class=cmt>//Make a connection to the Database</span> ADOConnection myConn = new ADOConnection(strConn) ; <span class=cmt>//string to select the records from the newpost table</span> string strCon =&quot;SELECT subject, name, email, message ,date &quot;; strCon+=&quot;FROM newpost WHERE postid=&quot;+postid ; <span class=cmt>//set a ADODataSetCommand</span> ADODataSetCommand myCommand =new ADODataSetCommand(strCon,myConn); ds = new DataSet(); <span class=cmt>//Don't ever forget to open the Connection </span> myConn.Open(); <span class=cmt> //Fill the DataSet</span> myCommand.FillDataSet(ds,&quot;newpost&quot;) ; <span class=cmt>//Get the Row at position '0' and store it in a DataRow object //Why row at position '0'? //Since there can only be one record with the given postid remember! //Why put into a DataRow ? Its easy to access data from a DataRow</span> dr = ds.Tables[&quot;newpost&quot;].Rows[0] ; <span class=cmt>//Get the &quot;subject&quot; from the DataRow and set it up in the post //reply form's subject field </span> subject.Text=&quot;Re:&quot;+dr[&quot;subject&quot;].ToString() ; <span class=cmt>//Select the replies to the post from the reply table</span> strCon =&quot;SELECT name , email, subject, message ,date&quot;; strCon+= &quot;FROM reply WHERE postid=&quot;+postid ; <span class=cmt> //Make a new ADODataSetCommand and DataSet for the reply table</span> ADODataSetCommand myCommand2 =new ADODataSetCommand(strCon,myConn); rs = new DataSet() ; <span class=cmt>//fill the DataSet</span> myCommand2.FillDataSet(rs, &quot;reply&quot;) ; <span class=cmt>//Code to update the &quot;views&quot; field for the newpost Table //Select the views field from the table for a given postid</span> strCon =&quot;SELECT views FROM newpost WHERE postid = &quot;+postid ; <span class=cmt>//Make a ADOCommand here since we want a ADODataReader later</span> ADOCommand vicomm = new ADOCommand(strCon, myConn) ; ADODataReader reader ; <span class=cmt>//execute the statement and create a ADODataReader</span> vicomm.Execute(out reader) ; <span class=cmt>//Read the First record (there can only be one record remember )</span> reader.Read() ; <span class=cmt>//Get a &quot;Int32&quot; value from the first Column (we have one column //in the reader, check the select statement above)</span> int i = reader.GetInt32(0) ; <span class=cmt>//Increase the views count</span> i++ ; reader.Close() ; <span class=cmt> //Update the newpost table with the new views value</span> strCon =&quot;UPDATE newpost SET views = &quot;+i+&quot; WHERE (postid= &quot;+postid+&quot;)&quot; ; <span class=cmt>//since we are using the same ADOCOmmand object //for this statement too to set the CommandText property</span> vicomm.CommandText = strCon ; <span class=cmt>//Since this statement will result in no resultset //use &quot;ExecuteNonQuery()&quot; </span> vicomm.ExecuteNonQuery() ; <span class=cmt> //close the connection</span> myConn.Close(); } } } <span class=cmt>// This method is called when the submit button is clicked </span> public void Submit_Click(Object sender, EventArgs e) { <span class=cmt>//Get the postid</span> postid = Request.Params[&quot;postid&quot;] ; <span class=cmt> //proceed only if all the required fields are filled-in</span> if(Page.IsValid&amp;&amp;name.Text!=&quot;&quot;&amp;&amp;subject.Text!=&quot;&quot;&amp;&amp;email.Text!=&quot;&quot;) {DateTime now = DateTime.Now ; errmess.Text=&quot;&quot; ; <span class=cmt>//We have to call the postmessage.aspx page with a //query to post the data to the Database. //Hence we have to first build the custom query from the Data //posted by the user also since we are using a query we have to //encode the data into UTF8 format</span> string req = &quot;name=&quot;+ System.Web.HttpUtility.UrlEncodeToString(name.Text, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;email=&quot;+ System.Web.HttpUtility.UrlEncodeToString(email.Text, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;subject=&quot;+System.Web.HttpUtility.UrlEncodeToString(subject.Text, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;ip=&quot;+System.Web.HttpUtility.UrlEncodeToString( Request.UserHostAddress.ToString(),System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;date=&quot;+ System.Web.HttpUtility.UrlEncodeToString(now.ToString(), System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;message=&quot;+ System.Web.HttpUtility.UrlEncodeToString(message.Text, System.Text.Encoding.UTF8); <span class=cmt>//Encode &quot;no&quot; to indicate that the post is not a new //post but its a reply to a earlier message</span> req+=&quot;&amp;&amp;newpost=&quot;+ System.Web.HttpUtility.UrlEncodeToString(&quot;no&quot;, System.Text.Encoding.UTF8); req+=&quot;&amp;&amp;previd=&quot;+ System.Web.HttpUtility.UrlEncodeToString(postid, System.Text.Encoding.UTF8); <span class=cmt>//Call the postmessage page with our custom query</span> Page.Navigate(&quot;postmessage.aspx?&quot; + req); } else { errmess.Text=&quot;Fill in all the Required Fields !&quot; ; } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt;&lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot;&gt; <span class=cmt>&lt;%-- Include a header file 'header.inc' --%&gt;</span> &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;br&gt; &lt;div align=center&gt; &lt;table border=0 width=80% cellspacing=2&gt; &lt;tr class=fohead&gt;&lt;th width=20%&gt;Author Name&lt;/th&gt; &lt;th width=80%&gt;Message&lt;/th&gt;&lt;/tr&gt; <span class=cmt>&lt;%-- Below I am encapsulating the email of the author over the name of the author so that when you click on the author a e-mail gets sent to him Also I am getting the DateTime from the DataBase and Displaying the Date and Time separately --%&gt; </span> &lt;tr class=folight&gt;&lt;td rowspan=2 align=&quot;center&quot;&gt; &lt;%= &quot;&lt;a href=mailto:&quot;+dr[&quot;email&quot;]+&quot;&gt;&quot;+dr[&quot;name&quot;]+&quot;&lt;/a&gt;&quot; %&gt;&lt;br&gt; &lt;font size=1&gt; &lt;%= dr[&quot;date&quot;].ToString().ToDateTime().ToShortDateString() %&gt; &lt;br&gt; &lt;%= dr[&quot;date&quot;].ToString().ToDateTime().ToShortTimeString() %&gt; &lt;/font&gt; &lt;/td&gt; &lt;td&gt;&lt;b&gt;Subject: &lt;/b&gt;&lt;%=dr[&quot;subject&quot;] %&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class=folight&gt; &lt;td&gt;&lt;pre&gt;&lt;%=dr[&quot;message&quot;] %&gt;&lt;/pre&gt; &lt;/td&gt; &lt;/tr&gt; <span class=cmt>&lt;%-- Get all the replies to the Original post and show them --%&gt;</span> &lt;% int no = rs.Tables[&quot;reply&quot;].Rows.Count ; if(no&gt;0) { for(int j=0 ;j&lt;no ; j++) { DataRow rd = rs.Tables[&quot;reply&quot;].Rows[j] ; %&gt; &lt;tr class=fodark&gt; &lt;td align=&quot;center&quot;&gt; &lt;%=&quot;&lt;a href=mailto:&quot;+rd[&quot;email&quot;]+&quot;&gt;&quot;+rd[&quot;name&quot;]+&quot;&lt;/a&gt;&quot; %&gt;&lt;br&gt; &lt;font size=1&gt; &lt;%= rd[&quot;date&quot;].ToString().ToDateTime().ToShortDateString() %&gt;&lt;br&gt; &lt;%= rd[&quot;date&quot;].ToString().ToDateTime().ToShortTimeString() %&gt;&lt;/font&gt; &lt;/td&gt; &lt;td&gt;&lt;pre&gt;&lt;%=rd[&quot;message&quot;] %&gt;&lt;/pre&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% } } %&gt; &lt;/table&gt; &lt;/div&gt; &lt;h3 align=&quot;center&quot; class=&quot;fodark&quot;&gt;&lt;a href=forum.aspx&gt;Click Here&lt;/a&gt; to go to back to Forum. &lt;br&gt;Reply to the Above Post.&lt;/h3&gt; &lt;br&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;COLOR:#ff0000&quot; runat=&quot;server&quot; /&gt; &lt;form runat=&quot;server&quot;&gt; &lt;table border=&quot;0&quot; width=&quot;80%&quot; align=&quot;center&quot;&gt; &lt;tr &gt; &lt;td class=&quot;fohead&quot; colspan=2&gt;&lt;b&gt;Reply to the Post&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot; &gt; &lt;td&gt;Name :&lt;/td&gt; &lt;td &gt;&lt;asp:textbox text=&quot;&quot; id=&quot;name&quot; runat=&quot;server&quot; /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#ff0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;E-Mail&amp;nbsp;:&lt;/td&gt; &lt;td&gt;&lt;asp:textbox text=&quot;&quot; id=&quot;email&quot; runat=&quot;server&quot;/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#ff0000&gt;*&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt; Subject:&lt;/td&gt; &lt;td&gt;&lt;asp:textbox test=&quot;&quot; id=&quot;subject&quot; width=200 runat=&quot;server&quot;/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#ff0000&gt;*&lt;/font&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;Message&amp;nbsp;:&lt;/td&gt; &lt;td&gt; &lt;asp:TextBox id=message runat=&quot;server&quot; Columns=&quot;30&quot; Rows=&quot;15&quot; TextMode=&quot;MultiLine&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=folight&gt; &lt;td colspan=2&gt; &lt;asp:Button class=fodark id=write onClick=Submit_Click runat=&quot;server&quot; Text=&quot;Submit&quot;&gt;&lt;/asp:Button&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt;&lt;br&gt; &lt;br&gt;&lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt;&lt;/html&gt;</pre> </td> </tr> </table> <P>&nbsp;</P> <P>3) <b> postmessage.aspx </b> : The page to save the posted data to the database.</P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre>&lt;%@ Import Namespace=&quot;System&quot; %&gt; &lt;%@ Assembly Name=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data.ADO&quot; %&gt; &lt;%@ Page Language=&quot;C#&quot; Debug=&quot;true&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Thank You for Posting !&lt;/title&gt; &lt;script language=&quot;C#&quot; runat=&quot;server&quot; &gt; <span class=cmt>//execute this script when the page loads</span> void Page_Load(Object Src, EventArgs E) { <span class=cmt>//if the page is called from another page</span> if (!Page.IsPostBack) { <span class=cmt>//Get all the Parameters from the Query string</span> string name = Request.Params[&quot;name&quot;] ; string email = Request.Params[&quot;email&quot;] ; string subject = Request.Params[&quot;subject&quot;] ; string ip = Request.Params[&quot;ip&quot;] ; string date = Request.Params[&quot;date&quot; ]; string message = Request.Params[&quot;message&quot;] ; bool newmess =true ; string previd =&quot;1&quot;; <span class=cmt>//Check if the post is a New topic or a reply to a new topic</span> if(Request.Params[&quot;newpost&quot;].Equals(&quot;no&quot;)) { <span class=cmt>//if its a reply then get the postid called as previd here </span> newmess =false ; previd = Request.Params[&quot;previd&quot;] ; } <span class=cmt>//If the post is a new topic then follow the below routine</span> if(newmess) { <span class=cmt>//The string for the path to the database , if your //database is in some other directory then edit the path //of this variable </span> string strConn=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ; Data Source=&quot;+Server.MapPath(&quot;.\\db\\board.mdb&quot;) ; <span class=cmt> //Get a ADOConnection to the database</span> ADOConnection myConn = new ADOConnection(strConn) ; <span class=cmt>//The SQL Select statement </span> string strCom = &quot;Select postid from newpost&quot; ; <span class=cmt>//Create a ADOCommand since we want a ADODataReader later</span> ADOCommand myCommand =new ADOCommand(strCom,myConn); <span class=cmt> //Open the connection</span> myConn.Open(); ADODataReader reader; <span class=cmt> //Execute the command and get the Data into &quot;reader&quot;</span> myCommand.Execute(out reader) ; int i=1 ; <span class=cmt> //Get the current number of records present in the database.</span> while(reader.Read()) { i++ ; } reader.Close() ; <span class=cmt> //build the SQL statement to insert into the Database</span> string insertStr =&quot; INSERT INTO newpost VALUES (&quot; +i +&quot;, '&quot; +name+&quot;', '&quot; +email+&quot;', '&quot; +subject+&quot;', '&quot; +ip+&quot;', '&quot; +date+&quot;', '&quot; +message+&quot;',0, 0)&quot; ; myCommand.CommandText =insertStr ; <span class=cmt> //Since the SQL statement does not return any //output use &quot;ExecuteNonQuery() method</span> myCommand.ExecuteNonQuery() ; <span class=cmt> //Close the connection</span> myConn.Close() ; } else { <span class=cmt> //If the posted data is a reply to a topic then follow //the below procedure string for the path to the database, //if your database is stored in some other directory then //edit the path here </span> string strConn=@&quot;Provider=Microsoft.Jet.OLEDB.4.0 ; Data Source=&quot;+Server.MapPath(&quot;.\\db\\board.mdb&quot;) ; ADOConnection myConn = new ADOConnection(strConn) ; <span class=cmt>//SQL statement to select the replyid </span> string strCom = &quot;Select replyid from reply&quot; ; <span class=cmt>//create a ADOCommand </span> ADOCommand myCommand =new ADOCommand(strCom,myConn); <span class=cmt>//Open the Connection</span> myConn.Open(); ADODataReader reader; <span class=cmt>//Execute the command and get the Data into &quot;reader&quot;</span> myCommand.Execute(out reader) ; int i=1 ; <span class=cmt> //Get the current number of records present in the database.</span> while(reader.Read()) { i++ ; } reader.Close() ; <span class=cmt> //Build a statement to insert the values into the reply table</span> string insertStr =&quot; INSERT INTO reply VALUES (&quot; +i +&quot;, '&quot; +name+&quot;', '&quot; +email+&quot;', '&quot; +subject+&quot;', '&quot; +ip+&quot;', '&quot; +date+&quot;', '&quot; +message+&quot;', &quot; +previd+&quot;)&quot;; myCommand.CommandText =insertStr ; <span class=cmt> //ExecuteNonQuery - since the command does not return anything</span> myCommand.ExecuteNonQuery() ; <span class=cmt> //string to get the replies column from the newpost table</span> string replyno = &quot;SELECT replies FROM newpost WHERE postid =&quot; +previd ; myCommand.CommandText =replyno ; <span class=cmt>//Execute command and get the reader </span> myCommand.Execute(out reader) ; <span class=cmt> //read the first record (remember there can only be one record //in the reader since postid is unique)</span> reader.Read(); <span class=cmt> //Get the &quot;Int16&quot; value of the number of replies from the replies //column in the newpost table</span> int rep =reader.GetInt16(0) ; reader.Close() ; rep++ ; <span class=cmt> //SQL statement to update the replies field in the newpost table</span> string updtStr =&quot;UPDATE newpost SET replies = &quot;+rep +&quot; WHERE (postid = &quot;+previd+&quot;)&quot; ; myCommand.CommandText = updtStr; <span class=cmt> //ExecuteNonQuerry why ?? I guess U should know by now !</span> myCommand.ExecuteNonQuery(); myConn.Close() ; } <span class=cmt> //get the different Parameters from the query string and store it //to respective Labels</span> NameLabel.Text = name; EmailLabel.Text= email ; SubjectLabel.Text=subject; MessageLabel.Text=message ; } else { <span class=cmt> //else display an error</span> errmess.Text=&quot;This Page Cannot be called directly. It has to be called from the Form posting page.&lt;br&gt;&quot; ; } } &lt;/script&gt; &lt;LINK href=&quot;mystyle.css&quot; type=text/css rel=stylesheet&gt; &lt;/head&gt; &lt;body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot;&gt; &lt;!-- #Include File=&quot;header.inc&quot; --&gt; &lt;center&gt; &lt;asp:label id=&quot;errmess&quot; text=&quot;&quot; style=&quot;color:#FF0000&quot; runat=&quot;server&quot; /&gt; &lt;h2 class=&quot;fodark&quot;&gt;&lt;b&gt;Thank You , for posting on the Message Board. &lt;/b&gt;&lt;/h2&gt; &lt;table align=center width=60% border=0 cellspacing=2 cellpadding=1 &gt; &lt;tr class=&quot;fohead&quot;&gt;&lt;td colspan=&quot;2&quot;&gt;The information You Posted!&lt;/td&gt;&lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;Name :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;NameLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;E-Mail :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;EmailLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;Subject :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;SubjectLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class=&quot;folight&quot;&gt; &lt;td&gt;Message :&lt;/td&gt; &lt;td&gt;&lt;asp:label id=&quot;MessageLabel&quot; text=&quot;&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;h4 class=&quot;fodark&quot;&gt;&lt;a href=&quot;forum.aspx&quot;&gt;Click here &lt;/a&gt; to go back to the Forum.&lt;br&gt; <span class=cmt>&lt;%-- A little work to show the link to return back to the page if, the post was a reply --%&gt;</span> &lt;% if(Request.Params[&quot;previd&quot;]!=null) { %&gt; &lt;a href='reply.aspx?postid=&lt;%=Request.Params[&quot;previd&quot;] %&gt;'&gt; Click here &lt;/a&gt;to go back where you came from. &lt;% } %&gt; &lt;/h4&gt; &lt;/center&gt; &lt;!-- #Include File=&quot;footer.inc&quot; --&gt; &lt;/body&gt; &lt;/html&gt;</pre> </td> </tr> </table>

Comments

Add Comment