public List <Coment> GetComents(int idRuta) { List <Coment> coments = new List <Coment>(); String sql = "SELECT comentari.*,nick FROM comentari JOIN users ON comentari.userID=users.userID WHERE idRuta='" + idRuta + "'"; MySqlCommand cmd = new MySqlCommand(sql, connection); MySqlDataReader mdr = cmd.ExecuteReader(); while (mdr.Read()) { Coment c = new Coment { comentariID = Convert.ToInt32(mdr[0].ToString()), data = mdr[1].ToString(), userID = Convert.ToInt32(mdr[2].ToString()), idRuta = Convert.ToInt32(mdr[3].ToString()), comentarioTexto = mdr[4].ToString(), imgPath = mdr[5].ToString(), nick = mdr[6].ToString() }; coments.Add(c); } mdr.Close(); return(coments); }
public void NewComent(Coment c) { string sql; if (c.imgPath != null) { sql = "INSERT INTO comentari(data, userID, idRuta, comentarioTexto, imgpath) VALUES ('" + c.data + "','" + c.userID + "','" + c.idRuta + "','" + c.comentarioTexto + "','" + c.imgPath + "')"; } else { sql = "INSERT INTO comentari(data, userID, idRuta, comentarioTexto) VALUES ('" + c.data + "','" + c.userID + "','" + c.idRuta + "','" + c.comentarioTexto + "')"; } MySqlCommand cmd = new MySqlCommand(sql) { Connection = connection }; cmd.ExecuteNonQuery(); }
protected void newComent_Click(object sender, EventArgs e) { Coment c = new Coment(); c.comentarioTexto = comment.InnerText; if (Uploader.HasFile) { try { if (Uploader.PostedFile.ContentType == "image/jpeg" || Uploader.PostedFile.ContentType == "image/png" || Uploader.PostedFile.ContentType == "image/jpg") { if (Uploader.PostedFile.ContentLength < 102400000) { string filename = Uploader.FileName; Uploader.SaveAs(MapPath("~/img/") + filename); c.imgPath = filename; labelError.Text = "Upload status: File uploaded!"; } } else { labelError.Text = "Upload status: Only Images are accepted!"; System.IO.StreamWriter fp; try { fp = System.IO.File.AppendText(Server.MapPath("~/img/") + "log.txt"); fp.WriteLine("File is not an image"); labelError.Text = "File Succesfully created!"; fp.Close(); } catch (Exception ex) { labelError.Text = "File Creation failed. Reason is as follows" + ex.ToString(); } } } catch (Exception ex) { labelError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; } } else { c.imgPath = null; } Ruta ruta = (Ruta)Session["ruta"]; c.idRuta = ruta.id; Usuari user = (Usuari)Session["user"]; c.userID = int.Parse(user.id); if (Page.IsPostBack) { bd.NewComent(c); } bd.NewRating(ruta.id, int.Parse(user.id), int.Parse(rating.Text)); Response.Redirect(Request.Url.AbsoluteUri); }