用asp.net寫的論壇程序--上貼保存
2024-07-10 13:07:57
供稿:網友
 
 
3) postmessage.aspx :- the page which saved data to the database
<%@ import namespace="system" %>
<%@ assembly name="system.data" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.ado" %>
<%@ page language="c#" debug="true" %>
<html>
<head>
<title>thank you for posting !</title>
<script language="c#" runat="server" >
//execute this script when the page loads 
void page_load(object src, eventargs e) 
{
//if the page is called from another page 
if (!page.ispostback) {
//get all the parameters from the query string 
string name = request.params["name"] ;
string email = request.params["email"] ;
string subject = request.params["subject"] ;
string ip = request.params["ip"] ;
string date = request.params["date" ];
string message = request.params["message"] ;
bool newmess =true ;
string previd ="1"; 
//check if the post is a new topic or a reply to a new topic 
if(request.params["newpost"].equals("no"))
{
//if its a reply then get the postid called as previd here 
newmess =false ;
previd = request.params["previd"] ; 
}
//if the post is a new topic then follow the below routine 
if(newmess)
{
//the string for the path to the database , if your database is in some other 
directory then edit the path
//of this variable 
string [email protected]"provider=microsoft.jet.oledb.4.0 ;data source=
"+server.mappath(".//db//board.mdb") ;
//get a adoconnection to the database 
adoconnection myconn = new adoconnection(strconn) ;
//the sql select statement 
string strcom = "select postid from newpost" ;
//create a adocommand since we want a adodatareader later 
adocommand mycommand =new adocommand(strcom,myconn);
//open the connection 
myconn.open();
adodatareader reader;
//execute the command and get the data into "reader" 
mycommand.execute(out reader) ;
int i=1 ;
//get the current number of records present in the database. 
while(reader.read())
{
i++ ;
}
reader.close() ;
//build the sql statement to insert into the database 
string insertstr =" insert into newpost values ("
+i +", '"
+name+"', '"
+email+"', '"
+subject+"', '"
+ip+"', '"
+date+"', '"
+message+"',0, 0)" ;
mycommand.commandtext =insertstr ;
//since the sql statement does not return any output use "executenonquery() method 
mycommand.executenonquery() ;
//close the connection 
myconn.close() ;
}
else
{
//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 
string [email protected]"provider=microsoft.jet.oledb.4.0 ;data source="+
server.mappath(".//db//board.mdb") ;
adoconnection myconn = new adoconnection(strconn) ;
//sql statement to select the replyid 
string strcom = "select replyid from reply" ;
//create a adocommand 
adocommand mycommand =new adocommand(strcom,myconn);
//open the connection 
myconn.open();
adodatareader reader;
//execute the command and get the data into "reader" 
mycommand.execute(out reader) ;
int i=1 ;
//get the current number of records present in the database. 
while(reader.read())
{
i++ ;
}
reader.close() ;
//build a statement to insert the values into the reply table 
string insertstr =" insert into reply values ("
+i +", '"
+name+"', '"
+email+"', '"
+subject+"', '"
+ip+"', '"
+date+"', '"
+message+"', " 
+previd+")";
mycommand.commandtext =insertstr ;
//executenonquery - since the command does not return anything 
mycommand.executenonquery() ;
//string to get the replies column from the newpost table 
string replyno = "select replies from newpost where postid ="+previd ;
mycommand.commandtext =replyno ;
//execute command and get the reader 
mycommand.execute(out reader) ;
//read the first record (remember there can only be one record in the reader since postid is unique) 
reader.read();
//get the "int16" value of the number of replies from the replies column in the newpost table 
int rep =reader.getint16(0) ;
reader.close() ;
rep++ ;
//sql statement to update the replies field in the newpost table 
string updtstr ="update newpost set replies = "+rep
+" where (postid = "+previd+")" ; 
mycommand.commandtext = updtstr;
//executenonquerry why ?? i guess u should know by now ! 
mycommand.executenonquery();
myconn.close() ;
}
//get the different parameters from the query string and store it 
//to respective labels 
namelabel.text = name;
emaillabel.text= email ;
subjectlabel.text=subject; 
messagelabel.text=message ; 
}
else
{
//else display an error 
errmess.text="this page cannot be called directly. it has to be called from the form posting page.<br>" ;
}
}
</script>
<link href="mystyle.css" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<!-- #include file="header.inc" --> 
<center>
<asp:label id="errmess" text="" runat="server" />
<h2 class="fodark"><b>thank you , for posting on the message board.</b></h2>
<table align=center width="60%" border="0" cellspacing="2" cellpadding="1" >
<tr class="fohead"><td colspan="2">the information you posted!</td></tr>
<tr class="folight">
<td>name :</td> 
<td><asp:label id="namelabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>e-mail :</td> 
<td><asp:label id="emaillabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>subject :</td>
<td><asp:label id="subjectlabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>message :</td>
<td><asp:label id="messagelabel" text="" runat="server" /></td>
</tr>
</table>
<br>
<h4 class="fodark"><a href="forum.aspx">click here </a> to go back to the forum.<br>
<%-- a little work to show the link to return back to the page if, the post was a reply --%> 
<% if(request.params["previd"]!=null)
{ %>
<a href='reply.aspx?postid=<%=request.params["previd"] %>'> click here </a>to go back 
where you came from.
<% } %>
</h4>
</center>
<!-- #include file="footer.inc" --> 
</body>
</html>