2) reply.aspx : the topic viewing and replying page
<%@ page language="c#" enablesessionstate="false" debug="true" %> <%@ import namespace="system" %> <%@ assembly name="system.data" %> <%@ import namespace="system.data" %> <%@ import namespace="system.data.ado" %> <html><head> <title>post new topic.</title> <%-- these are the imported assemblies and namespaces needed --%> <script language="c#" runat="server"> dataset ds ,rs; datarow dr ; string postid ; public void page_load(object sender , eventargs e) { //check if the page is post back if(!page.ispostback) { //get the postid from the query string postid = request.params["postid"] ; if(postid!=null) { //database connection string. change the path to the database file if you have some other path where //you are saving your database file string [email protected]"provider=microsoft.jet.oledb.4.0 ;data source="+server.mappath(".//db//board.mdb") ; //make a connection to the database adoconnection myconn = new adoconnection(strconn) ; //string to select the records from the newpost table string strcon ="select subject, name, email, message ,date from newpost where postid="+postid ; //set a adodatasetcommand adodatasetcommand mycommand =new adodatasetcommand(strcon,myconn); ds = new dataset(); //don't ever forget to open the connection myconn.open(); //fill the dataset mycommand.filldataset(ds,"newpost") ; //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 dr = ds.tables["newpost"].rows[0] ; //get the "subject" from the datarow and set it up in the post reply form's subject field subject.text="re:"+dr["subject"].tostring() ; //select the replies to the post from the reply table strcon ="select name , email, subject, message ,date from reply where postid="+postid ; //make a new adodatasetcommand and dataset for the reply table adodatasetcommand mycommand2 =new adodatasetcommand(strcon,myconn); rs = new dataset() ; //fill the dataset mycommand2.filldataset(rs, "reply") ; //code to update the "views" field for the newpost table //select the views field from the table for a given postid strcon ="select views from newpost where postid = "+postid ; //make a adocommand here since we want a adodatareader later adocommand vicomm = new adocommand(strcon, myconn) ; adodatareader reader ; //execute the statement and create a adodatareader vicomm.execute(out reader) ; //read the first record (there can only be one record remember !) reader.read() ; //get a "int32" value from the first column (we have one column in the reader, check the select statement above) int i = reader.getint32(0) ; //increase the views count i++ ; reader.close() ; //update the newpost table with the new views value strcon ="update newpost set views = "+i+" where (postid= "+postid+")" ; //since we are using the same adocommand object for this statement too to set the commandtext property vicomm.commandtext = strcon ; //since this statement will result in no output we use "executenonquery()" method vicomm.executenonquery() ; //close the connection myconn.close(); } } } // this method is called when the submit button is clicked public void submit_click(object sender, eventargs e) { //get the postid postid = request.params["postid"] ;
//proceed only if all the required fields are filled-in if(page.isvalid&&name.text!=""&&subject.text!=""&&email.text!=""){ datetime now = datetime.now ; errmess.text="" ; //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 string req = "name="+ system.web.httputility.urlencodetostring(name.text, system.text.encoding.utf8); req+="&&email="+ system.web.httputility.urlencodetostring(email.text, system.text.encoding.utf8); req+="&&subject="+system.web.httputility.urlencodetostring(subject.text, system.text.encoding.utf8); req+="&&ip="+system.web.httputility.urlencodetostring(request.userhostaddress.tostring(), system.text.encoding.utf8); req+="&&date="+ system.web.httputility.urlencodetostring(now.tostring(), system.text.encoding.utf8); req+="&&message="+ system.web.httputility.urlencodetostring(message.text, system.text.encoding.utf8); //encode "no" to indicate that the post is not a new post but its a reply to a earlier message req+="&&newpost="+ system.web.httputility.urlencodetostring("no", system.text.encoding.utf8); req+="&&previd="+ system.web.httputility.urlencodetostring(postid, system.text.encoding.utf8); //call the postmessage page with our custom query page.navigate("postmessage.aspx?" + req); } else { errmess.text="fill in all the required fields !" ; } } </script> <link href="mystyle.css" type=text/css rel=stylesheet></head> <body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0"> <%-- include a header file 'header.inc' --%> <!-- #include file="header.inc" --> <br> <div align=center> <table border=0 width=80% cellspacing=2> <tr class=fohead><th width=20%>author name</th> <th width=80%>message</th></tr> <%-- 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 geting the datetime from the database and displaying the date and time separately --%> <tr class=folight><td rowspan=2 align="center"><%= "<a href=mailto:"+dr["email"]+">"+dr["name"]+"</a>" %><br> <font size=1><%= dr["date"].tostring().todatetime().toshortdatestring() %><br> <%= dr["date"].tostring().todatetime().toshorttimestring() %></font> </td> <td><b>subject: </b><%=dr["subject"] %></td></tr> <tr class=folight> <td><pre><%=dr["message"] %></pre> </td> </tr> <%-- get all the replies to the original post and show them --%> <% int no = rs.tables["reply"].rows.count ; if(no>0) { for(int j=0 ;j<no ; j++) { datarow rd = rs.tables["reply"].rows[j] ; %> <tr class=fodark> <td align="center"><%="<a href=mailto:"+rd["email"]+">"+rd["name"]+"</a>" %><br> <font size=1><%= rd["date"].tostring().todatetime().toshortdatestring() %><br> <%= rd["date"].tostring().todatetime().toshorttimestring() %></font> </td> <td><pre><%=rd["message"] %></pre> </td> </tr> <% } } %> </table> </div> <h3 align="center" class="fodark"><a href=forum.aspx>click here</a> to go to back to forum. <br>reply to the above post.</h3> <br> <asp:label id="errmess" text="" style="color:#ff0000" runat="server" /> <form runat="server"> <table border="0" width="80%" align="center"> <tr > <td class="fohead" colspan=2><b>reply to the post</b></td> </tr> <tr class="folight" > <td>name :</td> <td ><asp:textbox text="" id="name" runat="server" /> <font color=#ff0000>*</font></td> </tr> <tr class="folight"> <td>e-mail :</td> <td><asp:textbox text="" id="email" runat="server"/> <font color=#ff0000>*</font></td> </tr> <tr class="folight"> <td> subject:</td> <td><asp:textbox test="" id="subject" width=200 runat="server"/> <font color=#ff0000>*</font> </td></tr> <tr class="folight"> <td>message :</td> <td> <asp:textbox id=message runat="server" columns="30" rows="15" textmode="multiline"></asp:textbox></td> </tr> <tr class=folight> <td colspan=2> <asp:button class=fodark id=write onclick=submit_click runat="server" text="submit"></asp:button></td></tr> </table> </form><br> <br><!-- #include file="footer.inc" --> </body></html>