Dynamically creating images in a sequence and having the sequence reset

walangala

Being a novice, I'm working on something and wanted to know if the desired end product can be achieved and if it is efficient. I've asked several questions on here and with each small triumph, I've hit a brick wall and researching has given mixed results. I don't know if a question like this should be asked on here or on one of the other Stack Exchange sites.

I have a web app that dynamically generates barcodes. The web app has a databound checkboxlist. The user checks their desired checkbox. With that, the user enters how many they want as well as a date. With that, the barcode is generated and the code uses a concatenation of the data in the checkboxlist, the date, and the amount entered. Using WebControls, the images are placed in a panel on the webpage and the user can simply print the image, that portion works just fine.

What I would like to know if it's at least possible or not, each time the user generates images, so they choose 1 or more names int the CheckBoxList, enter the amount desired, as well as the date, if that information being stored on a database can then be retrieved again in the same event if the user wants to generate more of the same barcodes?

I'll add my code to give an idea of what's going on.

EDIT

To elaborate more on what I'm asking.

A user selects a checkbox

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

Then enters an amount and selects a date. Let's say 10 and the date is January 25th 2016. The code generates 10 barcodes. The code generated for those barcodes are AB01251601 AB01251602 AB01251603...AB01251610 which ends on 10. What I'd want to know if it's possible, each time the user runs this, if this information can be added to a a database and then on another page event or even the same (maybe with a TextChanged Event) If the user wants another 3 barcodes, they can enter the information again and the generated codes start at 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 Code

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

If a user selects a box, runs the application, the user's input is sent to the database

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

 Please Enter Amount [   4]
 Please Enter Date   [08/24/2016] 

The user submits the data on the webform and the database updates

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                 |

The end goal is to when the user selects more than one checkbox and enters the values, more than one row in the table updates.

[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|
walangala

I figured it out.

In my ListItem I had a value for Accound_ID. Since I was using that in one of the foreach loops that caused an issue when submitting through my database.

Creating another List and applying the new list on another foreach loop did the trick.

//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();
       }


    }


 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related