Web34
Web34

Reputation: 131

To save Barcode Image In Database and retrieve it

I am trying to save Barcode Image in database. What my code currently generating is just a long string of URL. My code is mentioned below:

string barCode = ds.Tables[0].Rows[0]["productId"].ToString();
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();

using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
    using (Graphics graphics = Graphics.FromImage(bitMap))
    {
        // Font oFont = new Font("Free 3 of 9 Extended", 16);
        Font oFont = new Font("IDAutomationHC39M", 16);
        PointF point = new PointF(2f, 2f);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush whiteBrush = new SolidBrush(Color.White);
        graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
        graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
    }

    using (MemoryStream ms = new MemoryStream())
    {
        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] byteImage = ms.ToArray();

        Convert.ToBase64String(byteImage);
        imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);

        **// I think Problem is here, in these lines //**          
        lblChkCopyIMAge.Text = imgBarCode.ToString();
    }

    placehod.Controls.Add(imgBarCode);
}

Can please anyone assist me to save complete Barcode image in specific folder and respective URL in database so that I can use it further. The code written above is just generating barcode image and a long string URL. When I saved it in database it is saving this string in URL: "System.Web.UI.WebControls.Label". I have used nvarchar(max) datatype to save ImageURL.

My code to Save in database is mentioned below:

string strcmd5 = "update  product set symbology=" + "'" + "IDAutomationHC39M" + "'" + "," + "barcodeimage=" + "'" + lblChkCopyIMAge + "'" + "where productId=" + int.Parse(Label1.Text);

SqlCommand objsqlcmd5 = new SqlCommand(strcmd5, objsqlcon4);
int h = objsqlcmd5.ExecuteNonQuery();
objsqlcon4.Close();
if (h > 0)
{
    Response.Write("Record has submitted Successfully");
    ScriptManager.RegisterStartupScript(this, this.GetType(), "barcode", "alert('Saved Successfully');", true);

}

Upvotes: 0

Views: 2526

Answers (1)

David
David

Reputation: 218960

You're setting the value to a label control:

"barcodeimage=" + "'" + lblChkCopyIMAge + "'"

As an object, the default string representation for a label control is the name of the type. You probably want to use its .Text property instead?

"barcodeimage=" + "'" + lblChkCopyIMAge.Text + "'"

Note that you should also really look into using parameterized queries for your database interaction. As it stands right now, any user input used in this query is a SQL injection vulnerability.

Upvotes: 1

Related Questions