日期:2011-03-18  浏览次数:20563 次

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 strConn=@"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.Tex