Asked by:
Image export

Question
-
User-527908287 posted
Hello,
On my web page, I have different images of ID card templates that displays in the thumbnail. I want a situation where I can click on an image in the thumbnail it should take that image to another web page for editing, but when I click on an image nothing happens. I have an error which is confusing to me. The error is in the yellow highlighted line in the ".cs" code page which I have attached to this thread. (Debug.WriteLine(ex.ToString());)
Then when editing, I want to be able to add texts like name and signature; I also want to be able to add something like a passport photograph and a logo on the image that was imported from the previous page in the thumbnail. I do not want to edit or change the entire imported image but to add those things mentioned above on the image as I'm trying to give users access to make Identity Cards and tickets for printing.
I need assistance on this issues and I will be grateful if these are resolved. Here is the code:
The Template Page (Template.aspx)
<!DOCTYPE html>
<head runat="server"> <style type="text/css"> .card { width: 200px; height:200px; } </style> </head> <body> <form id="form1" runat="server"> <div class="row" style="padding-top: 100px;"> <asp:Repeater ID="rptrCards" runat="server"> <ItemTemplate> <div class="col-sm-3 col-md-4"> <div class="thumbnail"> <asp:ImageButton ID="ImageButton" ImageUrl='<%#Eval("card") %>' CssClass="card" runat="server" AlternateText="note" CommandArgument='<%#Eval("Id") %>' CommandName="Edit" /> <div class="caption"> <div class="probrand"><%#Eval("card_title") %></div> <div class="proname"><%#Eval("content") %></div> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </div> </div> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>Code behind the Template Page (Template.aspx.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class CardsTemplates : System.Web.UI.Page { SqlCommand cmd = new SqlCommand(); SqlDataAdapter sda = new SqlDataAdapter(); DataSet ds = new DataSet(); // SqlConnection con = new SqlConnection(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRepeater(); } } public DataTable SelectFromDatabase(string sql, SqlParameter[] parameters) { SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"); { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (parameters != null) { cmd.Parameters.AddRange(parameters); } using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } } private void BindRepeater() { string sql = "Select * from blog"; rptrCards.DataSource = SelectFromDatabase(sql, null); rptrCards.DataBind(); } protected void rptrCards_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName.Equals("Edit")) { string id = e.CommandArgument.ToString(); Response.Redirect("RepeaterThumbnailEdit.aspx?id=" + id); } }
The Edit page(EditPage.aspx)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Edit</title> <link href="css/bootstrap.min.css" rel="stylesheet" /> <link href="css/StyleSheet.css" rel="stylesheet" /> <style type="text/css"> .card { width: 200px; height:200px; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView ID="ImageDetails" DefaultMode="Edit" runat="server" AutoGenerateRows="false" OnItemCommand="ImageDetails_ItemCommand"> <Fields> <asp:TemplateField HeaderText="Image"> <EditItemTemplate> <asp:Image ID="DisplayImage" CssClass="card" runat="server" ImageUrl='<%#Eval("card") %>' /> <asp:FileUpload ID="ImageUploader" runat="server" /> <asp:Button ID="ChangeImageBtn" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="ChangeImage" Text="Change Image" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Card Title"> <EditItemTemplate> <asp:TextBox ID="titleTxt" runat="server" Text='<%#Eval("card_title") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Card Title"> <EditItemTemplate> <asp:TextBox ID="contentTxt" runat="server" Text='<%#Eval("content") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Save Change"> <EditItemTemplate> <asp:Button ID="SaveBtn" CommandName="Save" CommandArgument='<%#Eval("Id") %>' runat="server" Text="Save" /> <asp:Button ID="CancelBtn" CommandName="Cancel" runat="server" Text="Cancel" /> </EditItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div> </form> </body> </html>
Code behind the edit page (EditPage.aspx.cs)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class Accure : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string id = Request.QueryString["id"]; if (id != null) { if (!IsPostBack) { BindDetailsView(id); } } } private void BindDetailsView(string id) { string sql = "Select * From blog where Id = @Id"; SqlParameter[] parameters = new[] { new SqlParameter("@Id", id) }; ImageDetails.DataSource = SelectFromDatabase(sql, parameters); ImageDetails.DataBind(); } public DataTable SelectFromDatabase(string sql, SqlParameter[] parameters) { SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"); { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (parameters != null) { cmd.Parameters.AddRange(parameters); } using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } } protected void ImageDetails_ItemCommand(object sender, DetailsViewCommandEventArgs e) { if (e.CommandName.Equals("ChangeImage")) { DetailsView dv = (DetailsView)((Button)e.CommandSource).NamingContainer; FileUpload upload = (FileUpload)dv.FindControl("ImageUploader"); string id = e.CommandArgument.ToString(); if (upload.HasFile) { string newPath = SaveFile(upload, upload.PostedFile); //You might need to delete the previous image //Update Image URL string sql = "Update blog Set card = @card Where Id=@Id"; SqlParameter[] parameters = new[] { new SqlParameter("@card", newPath), new SqlParameter("@Id", id) }; ExecuteNonQuery(sql, parameters); // Rebind DetailsView BindDetailsView(id); } } else if (e.CommandName.Equals("Cancel")) { Response.Redirect("RepeaterThumbnail.aspx"); } else if (e.CommandName.Equals("Save")) { DetailsView dv = (DetailsView)((Button)e.CommandSource).NamingContainer; string titleText = ((TextBox)dv.FindControl("titleTxt")).Text; string contentText = ((TextBox)dv.FindControl("contentTxt")).Text; string id = e.CommandArgument.ToString(); //Update Image title and content string sql = "Update blog Set card_title = @card_title, content = @content Where Id=@Id"; SqlParameter[] parameters = new[] { new SqlParameter("@card_title", titleText), new SqlParameter("@content", contentText), new SqlParameter("@Id", id) }; ExecuteNonQuery(sql, parameters); Response.Redirect("RepeaterThumbnail.aspx"); } } public int ExecuteNonQuery(string sql, SqlParameter[] parameters) { // -1 means the query is not successful int result = -1; SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"); { try { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { if (null != parameters) { cmd.Parameters.AddRange(parameters); } result = cmd.ExecuteNonQuery(); } } catch (SqlException ex) { Debug.WriteLine(ex.ToString()); } finally { conn.Close(); } } return result; } private string SaveFile(FileUpload upload, HttpPostedFile file) { // Specify the path to save the uploaded file to. string imageFolder = "images/"; string savePath = Server.MapPath(imageFolder); // Get the name of the file to upload. string fileName = upload.FileName; // Create the path and file name to check for duplicates. string pathToCheck = savePath + fileName; // Create a temporary file name to use for checking duplicates. string tempfileName = ""; // Check to see if a file already exists with the // same name as the file to upload. if (System.IO.File.Exists(pathToCheck)) { int counter = 2; while (System.IO.File.Exists(pathToCheck)) { // if a file with this name already exists, // prefix the filename with a number. tempfileName = counter.ToString() + fileName; pathToCheck = savePath + tempfileName; counter++; } fileName = tempfileName; } // Append the name of the file to upload to the path. savePath += fileName; // Call the SaveAs method to save the uploaded // file to the specified directory. upload.SaveAs(savePath); return imageFolder + fileName; } }
Donald Simon
Saturday, May 2, 2020 8:29 AM
All replies
-
User753101303 posted
Hi,
Ok but how about showing actually the error message you have so that others don't have to read your code and guess which message you SEE ?
Not directly related but your approach is likely counterproductive ie on a production server you won't have a console to show the error message and you don't check the return value. So the error will be basically hidden or you'll see another later error caused by this hidden errror.
Edit: for now you we know you have an error on the SQL side but it could be absolutely anything from a wrong connection string, to a wrong column name or a string size that is exceeding the allowed length etc etc... It's best to always start from the actual error message rather than trying to guess.
Saturday, May 2, 2020 11:05 AM -
User288213138 posted
Hi Donal416,
On my web page, I have different images of ID card templates that displays in the thumbnail. I want a situation where I can click on an image in the thumbnail it should take that image to another web page for editing, but when I click on an image nothing happensThen when editing, I want to be able to add texts like name and signature; I also want to be able to add something like a passport photograph and a logo on the image that was imported from the previous page in the thumbnail. I do not want to edit or change the entire imported image but to add those things mentioned above on the image as I'm trying to give users access to make Identity Cards and tickets for printing.I have answered the same question in this thread.
https://forums.asp.net/t/2166516.aspx
The error is in the yellow highlighted line in the ".cs" code page which I have attached to this thread. (Debug.WriteLine(ex.ToString());)
As PatriceSc said, you should post your error message instead of telling us which line of code is wrong.
Best regards,
Sam
Monday, May 4, 2020 7:32 AM