public ActionResult Create(LivreBean livreModel) { if (livreModel != null) { SqlConnection cnn = null; string connetionString = Properties.Settings.Default.dbConnectionString; string sql = "INSERT INTO Livre(CodeISBN, Nom, Image, EtatLivre) VALUES(@CodeIsbn, @Nom, @Image, @EtatLivre)"; var fileBytes = new byte[0]; HttpPostedFileBase file = Request.Files["Livre.Image"]; if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; fileBytes = new byte[file.ContentLength]; file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); } cnn = new SqlConnection(connetionString); try { cnn.Open(); var command = new SqlCommand(sql, cnn); var paramCodeIsbn = new SqlParameter("@CodeIsbn", SqlDbType.NVarChar) { Value = livreModel.CodeIsbn ?? "" }; command.Parameters.Add(paramCodeIsbn); var paramNom = new SqlParameter("@Nom", SqlDbType.NVarChar) { Value = livreModel.NomLivre }; command.Parameters.Add(paramNom); var paramFileField = new SqlParameter("@Image", SqlDbType.Image) { Value = fileBytes }; command.Parameters.Add(paramFileField); var paramEtat = new SqlParameter("@EtatLivre", SqlDbType.NChar) { Value = livreModel.NoLivre }; command.Parameters.Add(paramEtat); command.ExecuteNonQuery(); command.Dispose(); cnn.Close(); } catch (Exception ex) { } } ViewBag.menuItemActive = "Livre"; return View("/Views/Account/Manage.cshtml"); }
public ActionResult GetInfoLivreJson(String codeIsbn) { var url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + codeIsbn; var webClient = new System.Net.WebClient(); var json = Encoding(webClient.DownloadString(url)); var gRresponse = JsonConvert.DeserializeObject<GoogleResponse>(json); if (gRresponse.Items == null) return null; var volumeInfo = gRresponse.Items[0].VolumeInfo; if (volumeInfo == null) return null; var livre = new LivreBean(); livre.NomLivre = volumeInfo.Title; if (volumeInfo.Authors != null) { livre.Auteur = ""; foreach (var aut in volumeInfo.Authors.ToList()) { if (livre.Auteur != "") { livre.Auteur += " ; "; } livre.Auteur += aut; } } return Json(livre, JsonRequestBehavior.AllowGet); }