Chidiebere Mgbemena

Chidiebere Mgbemena

  • NA
  • 179
  • 14.2k

my Add button is not adding products to datagridview

Apr 21 2020 8:40 AM
Hello guyz,
 
its me again please look at my codes and see why my add button is not adding the numbers to datagridview and i am not getting any error when i debug.
 
"it only underlines dr : SqlDataReader dr; It says "its never been used in frmNewBill"
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  11. namespace NSPM_Sales_Invoice  
  12. {  
  13. public partial class frmNewBill : Form  
  14. {  
  15. SqlConnection con = new SqlConnection(Properties.Settings.Default.NSPM_Sales_InvoiceCon);  
  16. SqlCommand cmd;  
  17. SqlDataReader dr;  
  18. public frmNewBill()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22. private void textBox1_TextChanged(object sender, EventArgs e)  
  23. {  
  24. }  
  25. private void panel1_Paint(object sender, PaintEventArgs e)  
  26. {  
  27. }  
  28. private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)  
  29. {  
  30. }  
  31. private void frmNewBill_Load(object sender, EventArgs e)  
  32. {  
  33. cmbDescription.Items.Clear();  
  34. con.Open();  
  35. cmd = con.CreateCommand();  
  36. cmd.CommandType= CommandType.Text;  
  37. cmd.CommandText = "Select ProName from tbl_Products order by ProName asc";  
  38. cmd.ExecuteNonQuery();  
  39. DataTable dt = new DataTable();  
  40. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  41. da.Fill(dt);  
  42. foreach (DataRow dr in dt.Rows)  
  43. {  
  44. cmbDescription.Items.Add(dr["ProName"].ToString());  
  45. }  
  46. con.Close();  
  47. }  
  48. private void btnAdd_Click(object sender, EventArgs e)  
  49. {  
  50. if (cmbDescription.Text == "")  
  51. {  
  52. MessageBox.Show("Description is Empty");  
  53. }  
  54. else if (txtQuantity.Text == "")  
  55. {  
  56. MessageBox.Show("Quantity is Empty");  
  57. }  
  58. else if (txtUnitPrice.Text == "")  
  59. {  
  60. MessageBox.Show("Unit Price is Empty");  
  61. }  
  62. else if (txtJobNo.Text == "")  
  63. {  
  64. MessageBox.Show("JobNo is Empty");  
  65. if (txtDeleteUpdate.Text == "")  
  66. {  
  67. int row = 0;  
  68. dataGridView1.Rows.Add();  
  69. row = dataGridView1.Rows.Count - 1;  
  70. dataGridView1["Description", row].Value = cmbDescription.Text;  
  71. dataGridView1["Quantity", row].Value = txtQuantity.Text;  
  72. dataGridView1["UnitPrice", row].Value = txtUnitPrice.Text;  
  73. dataGridView1["Value", row].Value = txtValue.Text;  
  74. dataGridView1["JobNo", row].Value = txtJobNo.Text;  
  75. dataGridView1.Refresh();  
  76. cmbDescription.Focus();  
  77. if (dataGridView1.Rows.Count > 0)  
  78. {  
  79. dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];  
  80. }  
  81. }  
  82. else  
  83. {  
  84. int i = 0;  
  85. DataGridViewRow row = dataGridView1.Rows[i];  
  86. row.Cells[1].Value = cmbDescription.Text;  
  87. row.Cells[2].Value = txtQuantity.Text;  
  88. row.Cells[3].Value = txtUnitPrice.Text;  
  89. row.Cells[4].Value = txtValue.Text;  
  90. }  
  91. cmbDescription.Text = "";  
  92. txtQuantity.Text = "";  
  93. txtUnitPrice.Text = "";  
  94. txtValue.Text = "";  
  95. txtJobNo.Text = "";  
  96. }  
  97. }  
  98. private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)  
  99. {  
  100. this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = (e.RowIndex + 1).ToString();  
  101. }  
  102. int i;  
  103. private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)  
  104. {  
  105. i = e.RowIndex;  
  106. DataGridViewRow row = dataGridView1.Rows[i];  
  107. cmbDescription.Text = row.Cells[1].Value.ToString();  
  108. txtQuantity.Text = row.Cells[2].Value.ToString();  
  109. txtUnitPrice.Text = row.Cells[3].Value.ToString();  
  110. txtValue.Text = row.Cells[4].Value.ToString();  
  111. txtDeleteUpdate.Text = row.Cells[0].Value.ToString();  
  112. }  
  113. private void btnDelete_Click(object sender, EventArgs e)  
  114. {  
  115. if (txtDeleteUpdate.Text == "")  
  116. {  
  117. MessageBox.Show("Select Product to Delete");  
  118. }  
  119. else  
  120. {  
  121. foreach (DataGridViewRow row in dataGridView1.SelectedRows)  
  122. {  
  123. if (!row.IsNewRow) dataGridView1.Rows.Remove(row);  
  124. }  
  125. }  
  126. }  
  127. }  
  128. }  
please help. thanks

Answers (3)