动态地按序列创建图像,并对序列进行重置

本文关键字:按序 创建 图像 动态 | 更新日期: 2023-09-27 18:03:43

作为一个新手,我正在做一些事情,想知道是否可以实现预期的最终产品,如果它是有效的。我在这里问了几个问题,每一个小的成功,我都遇到了瓶颈,研究的结果好坏参半。我不知道这样的问题应该在这里还是在其他Stack Exchange网站上问。

我有一个动态生成条形码的web应用程序。web应用程序有一个绑定复选框列表。用户选中他们想要的复选框。然后,用户输入他们想要的数量和日期。这样,条形码就生成了,代码使用复选框列表中的数据、日期和输入的金额的串联。使用WebControls,图像被放置在网页的面板上,用户可以简单地打印图像,这部分工作得很好。

我想知道它是否至少是可能的,每次用户生成图像,所以他们选择1个或更多的名称在CheckBoxList,输入所需的数量,以及日期,如果信息被存储在数据库中,然后可以在同一事件中再次检索,如果用户想要生成更多相同的条形码?

我将添加我的代码,让大家知道发生了什么。

编辑

详细说明我的问题。

用户选择一个复选框

[X] AB
[] BC
[] CD
[] DE

然后输入金额并选择日期。假设是10,日期是2016年1月25日。该代码生成10个条形码。这些条形码生成的代码是结束于10的AB01251601 AB01251602 AB01251603...AB01251610。我想知道的是,如果它是可能的,每次用户运行这个,如果这个信息可以添加到一个数据库,然后在另一个页面事件或甚至相同(也许与TextChanged事件)如果用户想要另外3个条形码,他们可以再次输入信息,生成的代码开始于11。AB01251611, AB01251612, AB01251613 .

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
       this.CheckBoxListDataBind();
       //Binds Database Data to CheckBoxList1
   }
}
private string GetConnectionString()
{
   //SQL Connection String
   return System.Configuration.ConfigurationManager.ConnectionString["DatabaseConnection"].ConnectionString;
}

//method to bind CheckBoxList
public void CheckBoxListDataBind()
{
  SqlConnection conn = new SqlConnection(GetConnectionString());
  using (SqlCommand cmd = new SqlCommand())
  {
     cmd.CommandText = "SELECT AccountID, AccountName FROM AccountTable ORDER BY AccountID ASC;"
     cmd. Connection = conn;
     conn.Open();
     using (SqlDataReader sdr = cmd.ExecuteReader())
     {
          while(sdr.Read())
          {
            ListItem. item = new ListItem();             
            item.Text = sdr["AccountID"].ToString() + "-" + sdr["AccountName].ToString(); 
            item.Value = sdr["AccountID"].ToString();
            item.Text = sdr{"AccountName].ToString();  
            cmd.Parameters.AddWithValue("AccountID", CheckBoxList1)
            cmd.Parameters.AddWithValue("AccountName", CheckBoxList1)
            CheckBoxList1.Items.Add(item);
         }
     }
     conn.Close();
 }

//method to generate image
public Bitmap DrawBarcode(string data, string label)
{
   Bitmap dyn_image = new Bitmap(date,Length * 27, 150);
   PointF point = new PointF(2,2)
   Font dyn_image_font = new Font("ImageFontName", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
  Font label_font = new Font("Tahoma", 9, System.Drawing.FontStyle.Reguar, System.Drawing.GraphicsUnit.Point);
  Graphics graphics = Graphics.FromImage(dyn_image);
  graphics = Graphics.FromImage(dyn_image);
  graphics.Clear(Color.White);
  graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
  graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, dyn_image.Width, dyn_image.Height);
  graphics.DrawString(dyn_image_font, label_font, new SolidBrush(Color.Black), point);
  RectangleF rectF = new RectangleF(5 , 100, 250, 170); 
  graphics.DrawString(label, label_font, new SolidBrush(Color.Black), rectF);
  graphics.Flush();
  graphics.Dispose();
  System.Web.UI.Controls.Image gen_image = new System.Web.UI.WebControls.Image();
  using (MemoryStream ms = new MemoryStream())
  {
     dyn_image.Save(ms, ImageFormat.Jpeg);
     byte[] byteImage = ms.ToArray();
     Convert.ToBase64String(byteImage);
   gen_image.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(byteImage);
  }
     return dyn_image;
}

  //button click method handles user inputs
protected void Generate(object sender, EventArgs e)
{
  CultureInfo provider = CultureInfo.InvariantCulture;
  System.Globalization.DateTimeStyle style = DateTimeStyle.None;
  DateTime dt;
  DateTime.TryParseExact(datepicker.Text, "mmddyyyy", provider, style out dt);
  int i = Int32.Parse(amount.Text);
  SqlConnection conn = new SqlConnection(GetConnectionString());
  SqlCommand cmd = new SqlCommand();
  cmd.Connection = conn;

  foreach(List item in CheckBoxList1.Items)
  {   
     //this forloops is for the DrawImage() method to generate more than
     //one image from user input amount 
     for(int n = 1; n <= i; n++) 
     {
        if (item.Selected)
        {
            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
            string barcode_label = item.Text + "QTY:___________"
            //When image generates, it will show 1 to user input amount                 
            string barode_data = item.Value + datepicker.Text + n.ToSTring("D2");
             Bitmap dynImage = DrawBarcode(barcode_data, barcode_label)
             MemoryStream ms = new MemoryStream();
             dynImage.Save(ms, ImageFormat.Jpeg);
             byte[] byteImage = ms.ToArray();
             Convert.ToBase64String(byteImage);
             img.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(byteImage);
             panel1.Controls.Add(img);
              double spacing;
              double mg = 5;
              spacing = img.Width.Value + mg;
        }
     } 

   }
     conn.Open();
     foreach(ListItem item in CheckBoxList1.Items)
     {
        if(item.Selected)
        {
        //handling parameters in loop.
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "Update_Account_Table";
          cmd.Parameters["@SeqNum"].Value = amount.Text;
          cmd.Parameters["@SeqDate"].Value = DateTime.ParseExact(datepicker.Text, "mmddyyyy", CultureInfo.InvariantCulture);
          cmd.Parameters["@Account_ID"].CheckBoxList1.SelectedValue;
          cmd.ExecuteNonQuery();
        }
           conn.Close();
     }
   } 
}
SQL代码

CREATE TABLE AccountTable
(
  RowID int IDENTITY(1, 1),
  AccountID varchar(2),
  AccountName varchar(50),
  SeqNum int,
  SeqDate datetime
)
CREATE PROCEDURE [ACCOUNTTABLE_UPDATE]
(
  @SeqNum int,
  @SeqDate datetime,
  @Account_ID varchar(2)
)
AS 
SET NOCOUNT ON
  BEGIN
    UPDATE AccountTable
      SET SeqNum = @SeqNum, SeqDate = @SeqDate
      WHERE AccountID = @AccountID
  END

如果用户选择一个框,运行应用程序,用户的输入被发送到数据库

[X] AB
[] BC
[] CD
[] DE
 Please Enter Amount [   4]
 Please Enter Date   [08/24/2016] 

用户在webform上提交数据,数据库更新

RowID|AccountID|AccountName|SeqNum|SeqDate              |
    1|AB       |Account A  |     4|2016-24-08 00:00:0000|
    2|BC       |Account B  |NULL  |NULL                 |
    3|CD       |Account C  |NULL  |NULL                 |
    4|DE       |Account D  |NULL  |NULL                 |

最终目标是当用户选择多个复选框并输入值时,表中的不止一行会更新。

[X] AB
[X] BC
[X] CD
[X] DE
 Please Enter Amount [   4]
 Please Enter Date   [08/24/2016]
RowID|AccountID|AccountName|SeqNum|SeqDate              |
    1|AB       |Account A  |     4|2016-24-08 00:00:0000|
    2|BC       |Account B  |     4|2016-24-08 00:00:0000|
    3|CD       |Account C  |     4|2016-24-08 00:00:0000|
    4|DE       |Account D  |     4|2016-24-08 00:00:0000|

动态地按序列创建图像,并对序列进行重置

我明白了。

在我的ListItem中我有一个Accound_ID的值。因为我在一个foreach循环中使用它,当通过我的数据库提交时导致了一个问题。

创建另一个List并将新List应用到另一个foreach循环中就实现了。

//In CheckBoxList1Bind() Method
ListItem item2 = new ListItem();
item.Value = sdr["AccountID"].ToString();
 //In Generate() Method
 cmd.CommandType = CommandType.StoredProcedure;
 cmd.CommandText = "Update_Account_Table";
 foreach(ListItem item2 in CheckBoxList1.Items)
 {
    if(item2.Selected)
    {
    //handling parameters in loop.
      cmd.Parameters["@SeqNum"].Value = amount.Text;
      cmd.Parameters["@SeqDate"].Value = DateTime.ParseExact(datepicker.Text, "mmddyyyy", CultureInfo.InvariantCulture);
      cmd.Parameters["@Account_ID"].item2.Value;
      cmd.ExecuteNonQuery();
       try
       {
           conn.Open();
           cmd.ExecuteNonQuery();
       }
       catch (SqlException ex)
       {
          MessageBox.Show(ex.Message);
       }
       finally
       {
          conn.Close();
       }

    }

 }