我有一个动态生成图像的webapp(即条形码)。使用用户输入生成图像。用户从数据绑定复选框列表中选择数据,然后使用日期选择器将数据连接到日期,最后用户输入金额。然后,从用户输入的数量创建许多图像。
我的程序运行并生成图像没有问题。我希望最终将用户输入的信息发送到数据库,这样,输入的数量将按顺序排序,每24小时后重置一次。
我的问题是,此时,当我运行我的应用程序并输入信息时,如果选中1复选框,则会更新信息而不会出现问题,并成功生成图像。如果我选择多个复选框,我的数据库将只更新在CheckBoxList中选择的第一个复选框及其在数据库中的相应行。
这是我的C#代码:
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;
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Update_Account_Table";
cmd.Parameters.AddWithValue("@SeqNum", SqlDbType.VarChar).Value = i;
cmd.Parameters.AddWithValue("@SeqDate", SqlDbType.DateTime).Value = dt;
cmd.Parameters.AddWithValue("@Account_ID", SqlDbType.VarChar).Value = CheckBoxList1.SelectedValue;
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;
}
}
//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"].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
在我的网页上,我有一个checkboxlist,一个读取int值的文本框,另一个使用JQuery Calender进行输入的文本框,以及一个从网页打印面板的javascript打印函数,最后,CheckBoxList使用另一个javascript函数检查所有框或取消选中它们。
如前所述,我的代码工作正常,但它没有按预期更新数据库。它只会更新数据库中的一行,该行由复选框列表中选择的Account_ID确定。只要选择1,就可以更新任何选择。如果我在复选框列表中选择all,或者多个,则只更新列表中的第一个复选框。