Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在序列中动态创建图像并重置序列_C#_Asp.net_Sql Server - Fatal编程技术网

C# 在序列中动态创建图像并重置序列

C# 在序列中动态创建图像并重置序列,c#,asp.net,sql-server,C#,Asp.net,Sql Server,作为一个新手,我正在做一些事情,我想知道是否能达到预期的最终产品,以及它是否有效。我在这里问了几个问题,每一次小小的胜利,我都遇到了困难,研究结果喜忧参半。我不知道这样的问题是应该在这里还是在其他Stack Exchange站点上提出的 我有一个动态生成条形码的web应用程序。web应用程序有一个数据绑定的复选框列表。用户选中所需的复选框。然后,用户输入他们想要的数量和日期。然后,生成条形码,代码使用复选框列表中的数据、日期和输入的金额的串联。使用WebControl,图像被放置在网页上的一个面

作为一个新手,我正在做一些事情,我想知道是否能达到预期的最终产品,以及它是否有效。我在这里问了几个问题,每一次小小的胜利,我都遇到了困难,研究结果喜忧参半。我不知道这样的问题是应该在这里还是在其他Stack Exchange站点上提出的

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

我想知道的是,每次用户生成图像时,他们都会在复选框列表中选择一个或多个名称,输入所需的数量以及日期,如果用户希望生成更多相同的条形码,那么是否可以在同一事件中再次检索存储在数据库中的信息

我将添加我的代码以了解发生了什么

编辑

详细说明我的要求

用户选择一个复选框

[X] AB
[] BC
[] CD
[] DE
然后输入金额并选择日期。比如说10号,日期是2016年1月25日。该代码生成10个条形码。为这些条形码生成的代码是
AB01251601 AB01251602 AB01251603…AB01251610
,以10结尾。我想知道的是,如果有可能,每次用户运行此操作时,如果此信息可以添加到数据库中,然后在另一个页面事件或甚至相同的页面事件(可能使用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();
     }

   } 

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

[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循环中使用了这个值,在通过数据库提交时导致了一个问题

创建另一个列表并将新列表应用到另一个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();
       }


    }


 }

我不明白问题是什么。你在表格上做得很好,展示了你所做的尝试。我想我的问题是,我所期望的方法的逻辑是否有效。我会编辑我的初始帖子。是的,这是可能的。但是为了使事情更容易,你应该考虑不要把这两个值塞进这样的一个元组中。有两个值组合成一个值,违反1NF。如果将这两个值拆分为两列会更好。那你猜怎么着…做这件事变得超级简单。当这些价值观被合并时,这是一个严重的后遗症。我不明白。你是说AccountID和RowID?哦,天哪。在sql方面,您似乎已经对它进行了适当的规范化。我的错。为什么不能在表中找到该AccountID的行并再次开始递增计数器?
//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();
       }


    }


 }