Hello,
I have a C# code that encodes image data from webform and in the database table. I tried to make the same thing for some data I have in two web forms. The first is an invoice data with label controls and gridview and another webform with labels and image control but it is giving me a hard time.
With the C# code, how can I encode data from database table into QR and bind the QR code image to an image control.
Here is my C#
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True";
if (Request.QueryString["CardId"] != null)
{
Session["CardId"] = Convert.ToInt32(Request.QueryString["CardId"]);
}
BindCardAndQR();
}
private void BindCardAndQR()
{
if (Session["CardId"] != null)
{
int id = Convert.ToInt32(Session["CardId"]);
string sql = "SELECT * FROM CardTbl WHERE Id = @Id";
SqlParameter[] parameters = new[] {
new SqlParameter("@Id", id)
};
// This is where I Bind Image3 for Card Image, But I want to use this method for binding of label and image controls for the QR image so that when QR code is scanned, it will display what is in the information in the data on a webform that has the address path.
DataTable dt = SelectFromDatabase(sql, parameters);
if (dt.Rows.Count > 0)
{
Image3.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dt.Rows[0]["image"]);
Image3.AlternateText = "The Image has been found";
}
else
{
Image3.ImageUrl = "#";
Image3.AlternateText = "The Image does not exist in DataBase";
}
// Bind Image4 for QR code
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/AuthenticateID.aspx") + "?Id=" + id);
Image4.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}
}
public byte[] GetQRCodeBytes(string url)
{
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
public static int ExecuteScalar(string sql, SqlParameter[] parameters)
{
// -1 means the query is not successful
int result = -1;
using (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);
}
object obj = cmd.ExecuteScalar();
if (obj != null)
{
result = Convert.ToInt32(obj);
}
}
}
catch (SqlException ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
con.Close();
}
}
return result;
}
public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
{
using (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;
}
}
}
}