I'm trying to generate a barcode with user text as well as a value selected from a databound checkbox. My code compiles fine, but when I generate the barcode, it is not reading the selected value from the checkbox and instead prints with the barcode:
SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOXLIST'USER INPUT'
Here is the code to bind the data from the database.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BarcodeBind();
}
}
public void BarcodeBind()
{
SqlConnection conn = new SqlConnection(GetConnectionString());
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Statement"
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["BarcodeNum"].ToString();
cmd.Parameters.AddWithValue("@BarcodeNum", BarcodeNum);
BarcodeNum.Items.Add(item);
}
}
conn.Close();
}
}
Here is the code to generate the barcode:
protected void btnGenerate_Click(object sender, EventArgs e)
{
string barCode = Barcode + txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 50, 90))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 18);
PointF point = new PointF(3f, 3f);
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);
}
plBarCode.Controls.Add(imgBarCode);
}
And here is my html code:
<html xmlns="http://ift.tt/lH0Osb">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="BarCodeBind" runat="server"
</asp:CheckBoxList>
<hr />
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
<hr />
<asp:PlaceHolder ID="plBarCode" runat="server" />
</div>
</form>
</body>
I believe it has something to do with stating that one of the values is selected. But I'm not sure how to do it with something databound. Or if the databound item in question should be controlled by a boolean value in the database itself?
Aucun commentaire:
Enregistrer un commentaire