Generating a sequence using user input and database values - c#

I'm trying to figure out how to create a sequence of numbers using values stored in a database and user input. I have a webapp that is generating barcodes. I know my issue lies with my nested for loop. Whatever value I have stored on my database, I want the user to start from that number and generate the amount of barcodes entered by the user starting from that number. The loop iterates strangely where it will not generate any images if the value entered is less than the what is in the database. If I enter a value greater than what is in the database it will only generate a number of images that is the difference between what the use entered and what's in the database.
CREATE TABLE AccountTable
(
RowID int IDENTITY(1, 1),
AccountID varchar(2),
AccountName varchar(50),
SeqNum int,
SeqDate datetime
)
protected void Btn_Click(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.Text = "SELECT MAX(SeqNum) as k FROM AccountTable;";
cmd.Connection = conn;
foreach(List item in CheckBoxList1.Items)
{
conn.Open();
var k = Convert.Int31(cmd.ExecuteScalar());
for(int n = k; n <= i; n++)
{
if (item.Selected)
{
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
string barcode_label = item.Text + "QTY:___________"
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.Close();
}
}

Related

Inserting decimal numbers from DataGridView into MySQL using INSERT

everybody,
I want to insert data from a DateGridView table into MySQL. It works so far but as soon as there are decimal numbers then they are not stored as decimal in MySQL but as integers. The decimal places are not transferred. I tried to convert the data but without success. They are not stored as decimal numbers.
Thanks for the tips !!!!
Here is the code
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) // dòng
{
string str = "server=test; database=test; uid=test; pwd=test;";
MySqlConnection constr = new MySqlConnection(str);
constr.Open();
String cmdText = "INSERT INTO KPI_Kosten (Betrag, Tower, Jahr, Periode, Land) VALUES ('"
+ Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value) + "','"
+ dataGridView1.Rows[i].Cells[1].Value + "','"
+ dataGridView1.Rows[i].Cells[2].Value + "','"
+ dataGridView1.Rows[i].Cells[3].Value + "','"
+ dataGridView1.Rows[i].Cells[4].Value + "' )";
MySqlCommand cmd = new MySqlCommand(cmdText, constr);
cmd.ExecuteNonQuery();
constr.Close();
}
}
Since decimal places are truncated because column data type definition set to integer, first you should modify Betrag column to decimal data type with certain precision and scale like example below:
ALTER TABLE KPI_Kosten MODIFY COLUMN Betrag decimal(10, 2);
Afterwards, try setting MySqlParameter with parameterized query to enforce decimal data type with specified settings (see documentation):
string str = "server=test; database=test; uid=test; pwd=test;";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
using (MySqlConnection constr = new MySqlConnection(str))
{
constr.Open();
// if '#' prefix for parameters not working, change to '?'
String cmdText = "INSERT INTO KPI_Kosten (Betrag, Tower, Jahr, Periode, Land) VALUES (#Betrag, #Tower, #Jahr, #Periode, #Land)";
using (MySqlCommand cmd = new MySqlCommand(cmdText, constr))
{
// if you're experiencing too many parameters error, uncomment this line below
// cmd.Parameters.Clear();
MySqlParameter betrag = new MySqlParameter("#Betrag", MySqlDbType.Decimal);
betrag.Precision = 10;
betrag.Scale = 2;
betrag.Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);
cmd.Parameters.Add(betrag);
cmd.Parameters.Add("#Tower", MySqlDbType.VarChar).Value = dataGridView1.Rows[i].Cells[1].Value;
// other parameters
cmd.ExecuteNonQuery();
}
}
}
Note: You can also try simplified version without precision like cmd.Parameters.Add("#Betrag", MySqlDbType.Decimal).Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);.

Retrieve fingerprints from sql server data VarBinary(Max) in winForm pictureBox

i am using secugen fingerprint reader and sdk ...with sdk i am able to scan fingerprint,match,verify and display fingerprint image in pictureBox on runtime.. but not able to save in database.. i trien my on code to save fingerprints in database..
this is insertion code
private void BtnVerify_Click(object sender, EventArgs e)
{
Int32 iError;
bool matched1 = false;
bool matched2 = false;
SGFPMSecurityLevel secu_level;
secu_level = (SGFPMSecurityLevel)comboBoxSecuLevel_V.SelectedIndex;
iError = m_FPM.MatchTemplate(m_RegMin1, m_VrfMin, secu_level, ref matched1);
iError = m_FPM.MatchTemplate(m_RegMin2, m_VrfMin, secu_level, ref matched2);
if (iError == (Int32)SGFPMError.ERROR_NONE)
{
if (matched1 & matched2)
{
StatusBar.Text = "Verification Success";
//string name = "sumi";
SqlConnection con = new SqlConnection("Data Source=HP; user=sa; password=Admin1; Initial Catalog=schoolmanagement; Integrated Security=True");
string qry = "INSERT into teacher_fingerprint (teacher_id,name,teacher_fingerprints)values (#teacher_id,#name,#teacher_fingerprints)";
SqlCommand cmd = new SqlCommand(qry, con);
MemoryStream stream = new MemoryStream();
pictureBoxV1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
con.Open();
cmd.Parameters.AddWithValue("#teacher_id", comboBox2.Text);
cmd.Parameters.AddWithValue("#name", textBox2.Text + " " + textBox3.Text);
//cmd.Parameters.AddWithValue("#teacher_fingerprints", m_VrfMin);
cmd.Parameters.AddWithValue("#teacher_fingerprints",pic);
cmd.ExecuteNonQuery();
con.Close();
}
else
{
StatusBar.Text = "Verification Failed";
}
}
else
DisplayError("MatchTemplate()", iError);
}
and this my DrawImage function
private void DrawImageImg(Byte[] imgData, PictureBox picBox)
{
int colorval;
Bitmap bmp = new Bitmap(m_ImageWidth, m_ImageHeight);
picBox.Image = (Image)bmp;
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
colorval = (int)imgData[(j * m_ImageWidth) + i];
bmp.SetPixel(i, j, Color.FromArgb(colorval, colorval, colorval));
}
}
picBox.Refresh();
}
this my database .. i dont know this fingerprint binary is valid or not?
enter image description here
i tried to select fingerprint data in pictureBox but its not working
it gives out of index error in drawImage function
this is my selection code
m_RegMin5 = new Byte[400];
if (iError == (Int32)SGFPMError.ERROR_NONE)
{
DrawImage(fp_image, pictureBox1);
StatusBar.Text = "Device Message: Finger On";
SqlConnection conn = new SqlConnection(#"Data Source=HP; user=sa; password=Admin1; Initial Catalog=schoolmanagement; Integrated Security=True");
string query = "select teacher_fingerprints from teacher_fingerprint where teacher_id=2 ";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
m_RegMin5 = cmd.ExecuteScalar() as byte[];
DrawImageImg(m_RegMin5,pictureBox2);
}
else
{
DisplayError("GetImage()", iError);
}

Dynamically creating images in a sequence and having the sequence reset

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

Update Stored Procedure with a DataBound CheckBoxList

I have a webapp that dynamically generates images (i.e. Barcodes). The images are generated using user input. The user chooses data from a databound checkbox lists, that data is then concatenated a date using a date picker, and lastly the user enters an amount. That then creates a number of images from the amount entered by the user.
My program runs and generates the images with no issue. I want to eventually have the information entered by the user sent to a database, that way, the amount entered will be sequenced, which resets itself after every 24 hours.
My issue is at the moment, when I run my app, and enter the information, if 1 checkbox is selected, it updates the information with no issue, along with successfully generating the images. If I choose more than one checkbox my database will only update the first checkbox selected in my CheckBoxList and it's respective row in the database.
Here is my C# code:
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 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
On my webpage I have a checkboxlist, a textbox that reads an int value, another textbox that uses a JQuery Calender for input, as well as a javascript print function that prints the panel from a webpage and finally, the CheckBoxList uses another javascript function to check all the boxes or uncheck them.
As stated before, my code works fine, but it's not updating the database as intended. It will only update one row in the database which is determined by the Account_ID selected in the checkboxlist. As long as 1 is selected, any selection can be update. If I select all in the checkboxlist, or more than one, only the first checkbox in the list is updated.

Creating a Sequence of numbers in dynamically generated images using database values

I have a web app that generates barcodes. It creates the barcodes by the amount entered by the user, then generates the code using the amount, a date value, and values in a checkboxlist. The data entered by the user is then sent to a database using a stored procedure. The date values and amount values in the corresponding table are reset every 24 hours, but if the user enters the values it increases, in a sequence throughout the day. What I'm trying to figure out is, if I can implement that sequence in my images that I am generating. I've attempted to use an SQL Reader but I don't know how to implement it correctly since it's in the foreach and for loop. The foreach takes the selected values in the checkbox list which then in the for loop creates the amount desired starting from the amount entered.
For example
1st. Attempt
[X] AB
[] BC
[] CD
[] DE
Please Enter Amount [ 4]
Please Enter Date [08/24/2016]
Barcodes:
AB08241601 AB08241602 AB08241603 AB08241604
2nd. Attempt
[X] AB
[] BC
[] CD
[] DE
Please Enter Amount [ 3]
Please Enter Date [08/24/2016]
Barcodes:
AB08241605 AB08241606 AB08241607
Here is the code that works
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)
{
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;
}
}
}
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();
}
}
}
}
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 + #SeqNum, SeqDate = #SeqDate
WHERE AccountID = #AccountID
END
SqlDataReader() Attempt
cmd.CommandText = "SELECT SeqNum FROM AccountTable;";
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
foreach(List item in CheckBoxList1.Items)
{
for(int n = 1; n <= i; n++)
{
if (item.Selected)
{
if(sdr.Read())
{
n = (int)sdr["SeqNum"];
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;
}
}
}
}
while the database updates correctly it just displays the value found in the database and generates the first barcode with the database value and all other barcodes with with the value of 0. The amount of barcodes displayed corresponds to the Length of total items in my checkbox list. For an example below, if the Data for the checkbox selected = 4, if I run my code, the value in my database correctly changes to 6, but I get the results below
[X] AB
[] BC
[] CD
[] DE
Please Enter Amount [ 2]
Please Enter Date [08/24/2016]
Barcodes:
AB08241604 AB08241600 AB08241600 AB08241600
I don't know how to execute the reader with the items selected in the foreach loop since they are already being used to generate the barcodes themselves.

Categories

Resources