Hello,
I have a school project to submit when we return to school after this covid-19 lockdown. the project is about making ID card templates on a web page using visual studio asp.net web form; this templates will give users access to edit their names, add company logo, and also add employees' position in the company.
I know it is going to be to be a huge workload for me but I am eager to do this. I have searched online for every possible material(s) and tutorials that will help but I can't really get something, which is why I am using this medium to seek for help; if there is anything like posts, video tutorials etc. that will help me please I need directions. I will be so grateful if I get an assistance. Thank you very much
-George
What I have tried:
I tried doing this but its not working;
This is the display page (display.aspx)
' class="img-thumbnail" CssClass="card" runat="server" AllowedFileExtensions=".docx, .doc" AlternateText="note" CommandArgument='' CommandName="Edit" />
code behind the display page: (display.aspx.cs)
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("CardsEdit.aspx?id=" + id);
}
}
}
This is the Edit Cards page (CardsEdit.aspx):
' />
' CommandName="ChangeImage" Text="Change Image" />
'>
'>
' runat="server" Text="Save" />
This is code behind the Edit Cards page (CardsEdit.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;
using System.Diagnostics;
public partial class CardsEdit : 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("display.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("display.aspx");
}
}
public int ExecuteNonQuery(string sql, SqlParameter[] parameters)
{
// -1 means the query is not successful
int result = -1;
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
{
try
{
con.Open();
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (null != parameters)
{
cmd.Parameters.AddRange(parameters);
}
result = cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
con.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;
}
}