using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using Mono.Data.SqliteClient; using System.Drawing; /// /// Adds Comment functionality to LB /// public partial class Comments : System.Web.UI.Page { HtmlTable feedbackTable = new HtmlTable(); HtmlTable commentTable = new HtmlTable(); Label nameLabel = new Label(); Label urlLabel = new Label(); Label commentLabel = new Label(); TextBox nameTextBox = new TextBox(); TextBox urlTextBox = new TextBox(); TextBox commentTextBox = new TextBox(); Button postButton = new Button(); Label feedbackHeaderLabel = new Label(); Label commentHeaderLabel = new Label(); protected void Page_Load(object sender, EventArgs e) { FileInfo cdb = new FileInfo(DbFile(this.Page)); if (cdb.Exists) { ShowComments(DbFile(this.Page)); //} //if (!cdb.IsReadOnly) //{ LiteralControl lc = new LiteralControl("


"); Form.Controls.Add(lc); ShowCommentForm(); } } public void CtrlAdd(System.Web.UI.Control o, HtmlTable t) { HtmlTableCell c = new HtmlTableCell(); HtmlTableRow r = new HtmlTableRow(); c.Controls.Add(o); r.Cells.Add(c); t.Rows.Add(r); } public void ShowCommentForm() { nameLabel.Text = "Name"; nameTextBox.Text = ""; urlLabel.Text = "Email Address or Website"; urlTextBox.Text = ""; commentLabel.Text = "Comment"; commentTextBox.Text = ""; postButton.Text = "Post"; CtrlAdd(nameLabel, feedbackTable); CtrlAdd(nameTextBox, feedbackTable); CtrlAdd(urlLabel, feedbackTable); CtrlAdd(urlTextBox, feedbackTable); CtrlAdd(commentLabel, feedbackTable); CtrlAdd(commentTextBox, feedbackTable); CtrlAdd(postButton, feedbackTable); nameTextBox.Width = 400; urlTextBox.Width = 400; commentTextBox.Width = 400; commentTextBox.Height = 200; commentTextBox.Rows = 50; commentTextBox.TextMode = TextBoxMode.MultiLine; postButton.Click += new System.EventHandler(PostClick); feedbackHeaderLabel.Text = "Leave A Reply"; feedbackHeaderLabel.Font.Size = FontUnit.XLarge; Form.Controls.Add(feedbackHeaderLabel); Form.Controls.Add(feedbackTable); } protected void PostClick(object sender, EventArgs e) { AddComment(nameTextBox.Text, urlTextBox.Text, commentTextBox.Text, DbFile(this.Page)); Form.Controls.Clear(); Page_Load(sender, e); } public String DbFile(System.Web.UI.Page p) { FileInfo f = new FileInfo(p.Request.Path); string mantissa = f.Name.Split('.')[0]; string basedir = AppDomain.CurrentDomain.BaseDirectory; return basedir + mantissa + ".db"; } public static void AddComment(string name, string url, string comment, string dbfile) { IDbConnection dbcon = (IDbConnection)new SqliteConnection("URI=file:" + dbfile); dbcon.Open(); IDbCommand dbcmd = dbcon.CreateCommand(); DateTime now = DateTime.Now; string _name = name.Replace("'", "''"); string _url = url.Replace("'", "''"); string _comment = comment.Replace("'", "''"); string sql = "INSERT INTO comments VALUES('" + _name + "','" + _url + "','" + _comment + "','" + now.ToShortTimeString() + "','" + now.ToLongDateString() + "',null)"; dbcmd.CommandText = sql; dbcmd.ExecuteScalar(); dbcmd.Dispose(); dbcon.Close(); //TODO: Call Method Here to Write to global bloglog.db. // This is so that blog author can easily see stories with new comments. } public void ShowComments(string dbfile) { SqliteDataAdapter sda = new SqliteDataAdapter("SELECT * FROM comments", "URI=file:" + dbfile); DataSet ds = new DataSet(); sda.Fill(ds); DataTable dt = ds.Tables[0]; String color; bool flip = false; foreach (DataRow r in dt.Rows) { if (flip) color = "F5F5F5"; else color = "FFFFFF"; HtmlTableCell metadata = new HtmlTableCell(); HtmlTableCell comment = new HtmlTableCell(); HtmlTableCell spacer = new HtmlTableCell(); metadata.InnerText = (String)r["name"] + " at " + (String)r["time"] + " on " + (String)r["date"]; comment.InnerText = (String)r["comment"]; spacer.InnerHtml = "
"; HtmlTableRow top = new HtmlTableRow(); top.Cells.Add(metadata); HtmlTableRow middle = new HtmlTableRow(); middle.Cells.Add(comment); HtmlTableRow bottom = new HtmlTableRow(); bottom.Cells.Add(spacer); HtmlTable acomment = new HtmlTable(); acomment.Width = "400px"; acomment.BgColor = color; acomment.Rows.Add(top); acomment.Rows.Add(middle); acomment.Rows.Add(bottom); CtrlAdd(acomment,commentTable); flip = !flip; } commentHeaderLabel.Text = "Comments"; commentHeaderLabel.Font.Size = FontUnit.XLarge; Form.Controls.Add(commentHeaderLabel); Form.Controls.Add(commentTable); } public void CreateDatabase(string dbfile) { IDbConnection dbcon = (IDbConnection)new SqliteConnection("URI=file:" + dbfile); dbcon.Open(); IDbCommand dbcmd = dbcon.CreateCommand(); string sql = "CREATE TABLE comments(name,url,comment,time,date,cuid INTEGER PRIMARY KEY)"; dbcmd.CommandText = sql; dbcmd.ExecuteScalar(); dbcmd.Dispose(); dbcon.Close(); } }