Note that there are some explanatory texts on larger screens.

plurals
  1. POKeep stuck at the reading the data from database
    text
    copied!<p>I would like to minus the data from the database with the value that I give when I run the program. Everything works, but I keep stuck at the <code>newVal</code>. I already did it like this, but the <code>newVal</code> keep appear <code>0</code> (because I declared <code>decimal newVal = 0</code>, but on this question, I just used <code>decimal newVal;</code>). Two more problems: if I move the <code>newVal = ...</code> to the top, it is useless, because one of the calculations in the <code>newVal</code> is reading data from the database (since I want database minus with the new value given when i run the program, required <code>dReader = cmd.ExecuteReader();</code>), but if I put the <code>newVal</code> at the bottom after reading data, it is useless as well, because I set the <code>Quantity = ?</code> and the value of <code>?</code> is <code>newVal..</code> Well, here is the code:</p> <pre><code>private void AddObjects(object sender, EventArgs e, Form theForm) { button1.Visible = true; textBoxQuantityContainer = new List&lt;NumericUpDown&gt;(); textBoxCodeContainer = new List&lt;NumericTextBox&gt;(); textBoxDescContainer = new List&lt;TextBox&gt;(); textBoxSubTotalContainer = new List&lt;TextBox&gt;(); textBoxTotalContainer = new List&lt;TextBox&gt;(); textBoxAllTotalContainer = new TextBox(); OleDbDataReader dReader; OleDbConnection conn = new OleDbConnection(connectionString); conn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Seranne]", conn); dReader = cmd.ExecuteReader(); AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection(); while (dReader.Read()) { string numString = dReader[0].ToString().PadLeft(4, '0'); codesCollection.Add(numString); } dReader.Close(); conn.Close(); if (firstForm.comboBox1.SelectedIndex == 0) { label1.Text = "Code:"; label1.Location = new Point(60, 125); label2.Text = "Welcome to the Selling System."; label2.Location = new Point(600, 30); label3.Text = "Quantity:"; label3.Location = new Point(155, 125); label4.Text = "Description:"; label4.Location = new Point(580, 125); label5.Text = "Sub Total on Rp:"; label5.Location = new Point(1020, 125); label6.Text = "Total on Rp:"; label6.Location = new Point(1210, 125); label7.Text = "Total on Rp:"; label7.Location = new Point(1080, 580); } else if (firstForm.comboBox1.SelectedIndex == 1) { label1.Text = "Kode:"; label1.Location = new Point(60, 125); label2.Text = "Selamat datang di Selling System."; label2.Location = new Point(600, 30); label3.Text = "Banyaknya:"; label3.Location = new Point(145, 125); label4.Text = "Keterangan:"; label4.Location = new Point(580, 125); label5.Text = "Sub Total di Rp:"; label5.Location = new Point(1020, 125); label6.Text = "Total di Rp:"; label6.Location = new Point(1210, 125); label7.Text = "Total di Rp:"; label7.Location = new Point(1080, 580); } //****TextBox for Code**** for (int y = 0; y &lt;= 16; y++) { textBoxCodeContainer.Add(new NumericTextBox()); textBoxCodeContainer[y].Size = new Size(100, 50); textBoxCodeContainer[y].Location = new Point(25, 150 + (y * 25)); textBoxCodeContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged); textBoxCodeContainer[y].AutoCompleteMode = AutoCompleteMode.Suggest; textBoxCodeContainer[y].AutoCompleteSource = AutoCompleteSource.CustomSource; textBoxCodeContainer[y].AutoCompleteCustomSource = codesCollection; theForm.Controls.Add(textBoxCodeContainer[y]); } //****TextBox for Quantity**** for (int y = 0; y &lt;= 16; y++) { textBoxQuantityContainer.Add(new NumericUpDown()); textBoxQuantityContainer[y].Size = new Size(100, 50); textBoxQuantityContainer[y].Location = new Point(125, 150 + (y * 25)); textBoxQuantityContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged); textBoxQuantityContainer[y].Maximum = 1000; theForm.Controls.Add(textBoxQuantityContainer[y]); } //****TextBox for Description**** for (int y = 0; y &lt;= 16; y++) { textBoxDescContainer.Add(new TextBox()); textBoxDescContainer[y].Size = new Size(750, 50); textBoxDescContainer[y].Location = new Point(225, 150 + (y * 25)); theForm.Controls.Add(textBoxDescContainer[y]); } //****TextBox for Sub Total**** for (int y = 0; y &lt;= 16; y++) { textBoxSubTotalContainer.Add(new TextBox()); textBoxSubTotalContainer[y].Size = new Size(175, 50); textBoxSubTotalContainer[y].Location = new Point(975, 150 + (y * 25)); theForm.Controls.Add(textBoxSubTotalContainer[y]); } //****TextBox for Total**** for (int y = 0; y &lt;= 16; y++) { textBoxTotalContainer.Add(new TextBox()); textBoxTotalContainer[y].Size = new Size(175, 50); textBoxTotalContainer[y].Location = new Point(1150, 150 + (y * 25)); textBoxTotalContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged); theForm.Controls.Add(textBoxTotalContainer[y]); } //****TextBox for Total All**** textBoxAllTotalContainer.Size = new Size(175, 50); textBoxAllTotalContainer.Location = new Point(1150, 575); textBoxAllTotalContainer.TextChanged += new System.EventHandler(this.textBox_TextChanged); theForm.Controls.Add(textBoxAllTotalContainer); } private void UpdateDatas() { int codeValue = 0; int index = 0; string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN ("; OleDbConnection conn = new OleDbConnection(connectionString); conn.Open(); if (int.TryParse(this.textBoxCodeContainer[0].Text, out codeValue)) { query = query + codeValue.ToString(); } for (int i = 1; i &lt; 17; i++) { if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue)) { query = query + "," + codeValue.ToString(); } } query = query + ")"; OleDbCommand cmd = new OleDbCommand(query, conn); OleDbDataReader dReader; dReader = cmd.ExecuteReader(); while (dReader.Read()) { if (textBoxCodeContainer[index].TextLength != 0) { this.textBoxQuantityContainer[index].Maximum = Convert.ToInt32(dReader["Quantity"].ToString()); this.textBoxDescContainer[index].Text = dReader["Description"].ToString(); this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString(); } index += 1; } dReader.Close(); conn.Close(); } private void UpdatePrice() { int totalPrice = 0; int quantity = 0; int price = 0; for (int i = 0; i &lt; 17; i++) { if (textBoxQuantityContainer[i].Value &gt; 0) { quantity = (int)textBoxQuantityContainer[i].Value; price = Convert.ToInt32(textBoxSubTotalContainer[i].Text); textBoxTotalContainer[i].Text = (quantity * price).ToString(); } else { textBoxSubTotalContainer[i].Text = ""; textBoxTotalContainer[i].Text = ""; } } for (int i = 0; i &lt; 17; i++) { if (textBoxTotalContainer[i].TextLength != 0) { totalPrice += Convert.ToInt32(textBoxTotalContainer[i].Text); } } textBoxAllTotalContainer.Text = totalPrice.ToString("n2"); } private void UpdateQuantity() { int index = 0; int codeValue = 0; decimal newVal; List&lt;int&gt; integers = new List&lt;int&gt;(); foreach (var tb in textBoxCodeContainer) { if (int.TryParse(tb.Text, out codeValue)) { integers.Add(codeValue); } } string command = "UPDATE [Seranne] SET [Quantity]=? WHERE [Code] IN(" + string.Join(", ", integers) + ")"; OleDbConnection conn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(command, conn); cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer); cmd.Parameters["Quantity"].Value = this.newVal.ToString(); OleDbDataReader dReader; conn.Open(); dReader = cmd.ExecuteReader(); while (dReader.Read()) { if (textBoxQuantityContainer[index].Value != 0) { newVal = (Convert.ToInt32(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value); int numberOfRows = cmd.ExecuteNonQuery(); } index += 1; } if (newVal == 0) { System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav"); sounds.Play(); MessageBox.Show("Cannot Update", "Error"); } else { System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav"); sound.Play(); MessageBox.Show("Was Updated Successfully", "Success"); } dReader.Close(); conn.Close(); } private void textBox_TextChanged(object sender, EventArgs e) { UpdateDatas(); UpdatePrice(); } private void button1_Click(object sender, EventArgs e) { UpdateQuantity(); } </code></pre> <p>Thanks a bunch</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload