Lerroy Fernandes

Lerroy Fernandes

  • NA
  • 17
  • 10.3k

Need help optimizing my code.

Feb 16 2021 12:45 PM
Hi,
I am designing an application for a new project during the pandemic, it would be great if you could assist me in resolving the problems I am facing.
 
I am getting the following error. 
 
 
Also, Below is the code I'm using, it would be great if you could help me optimize the code,
I am looking to add data onto a datagridview table with SQL in the backend to be used as an Inventory Management System. It would be great if you could assist me in accomplishing the same. 
  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.IO;  
  11. using System.Data.SqlClient;  
  12. using System.Configuration;  
  13. namespace Amal_Inventory  
  14. {  
  15. public partial class Form1 : Form  
  16. {  
  17. public Form1()  
  18. {  
  19. InitializeComponent();  
  20. }  
  21. SqlConnection con = new SqlConnection(@"Data Source=D.\SQLEXPRESS;Initial Catalog=ReportServer$SQLEXPRESS;Integrated Security=True");  
  22. DataTable table = new DataTable();  
  23. int indexRow;  
  24. private void label1_Click(object sender, EventArgs e)  
  25. {  
  26. }  
  27. private void button3_Click(object sender, EventArgs e)  
  28. {  
  29. }  
  30. private void button4_Click(object sender, EventArgs e)  
  31. {  
  32. /*Microsoft.Office.Interop.Excel._Application = new Microsoft.Office.Interop.Excel.Application(); 
  33. Microsoft.Office.Interop.Excel._Workbook workbook = Application.workbooks.Add(Type.Missing); 
  34. */  
  35. }  
  36. private void button6_Click(object sender, EventArgs e)  
  37. {  
  38. }  
  39. private void button5_Click(object sender, EventArgs e)  
  40. {  
  41. if (MessageBox.Show("Sure ??""Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)==DialogResult.OK)  
  42. {  
  43. if (this.dataGridView1.SelectedRows.Count > 0)  
  44. {  
  45. dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);  
  46. }  
  47. textBox1.Text = "";  
  48. textBox2.Text = "";  
  49. textBox3.Text = "";  
  50. textBox4.Text = "";  
  51. textBox5.Text = "";  
  52. textBox6.Text = "";  
  53. textBox7.Text = "";  
  54. textBox8.Text = "";  
  55. textBox9.Text = "";  
  56. pictureBox1.Image = null;  
  57. }  
  58. }  
  59. private void button7_Click(object sender, EventArgs e)  
  60. {  
  61. textBox1.Text = "";  
  62. textBox2.Text = "";  
  63. textBox3.Text = "";  
  64. textBox4.Text = "";  
  65. textBox5.Text = "";  
  66. textBox6.Text = "";  
  67. textBox7.Text = "";  
  68. textBox8.Text = "";  
  69. textBox9.Text = "";  
  70. pictureBox1.Image = null;  
  71. }  
  72. private void button8_Click(object sender, EventArgs e)  
  73. {  
  74. Update();  
  75. }  
  76. private void button2_Click(object sender, EventArgs e)  
  77. {  
  78. MemoryStream mmst = new MemoryStream();  
  79. pictureBox1.Image.Save(mmst, pictureBox1.Image.RawFormat);  
  80. byte[] img = mmst.ToArray();  
  81. dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text,  
  82. textBox6.Text, textBox7.Text, textBox8.Text, img);  
  83. }  
  84. private void Form1_Load(object sender, EventArgs e)  
  85. {  
  86. string mainconn = ConfigurationManager.ConnectionStrings["MyCONN"].ConnectionString;  
  87. SqlConnection sqlconn = new SqlConnection(mainconn);  
  88. string sqlquery= "select * from [dbo].[tbl.Amal]";  
  89. sqlconn.Open();  
  90. SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);  
  91. SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);  
  92. DataTable dt = new DataTable();  
  93. sdr.Fill(dt);  
  94. dataGridView1.DataSource = dt;  
  95. sqlconn.Close();  
  96. }  
  97. private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  98. {  
  99. DialogResult confirm = MessageBox.Show("Are you sure?""Exit", MessageBoxButtons.YesNo);  
  100. }  
  101. private void button1_Click(object sender, EventArgs e)  
  102. {  
  103. OpenFileDialog openfiledialogue1 = new OpenFileDialog();  
  104. openfiledialogue1.Filter = "Choose Image (*.jpg;*.png;*.gif;*.jpeg)|*.jpg;*.png;*.gif;*.jpeg";  
  105. if (openfiledialogue1.ShowDialog() == DialogResult.OK)  
  106. {  
  107. pictureBox1.Image = new Bitmap(openfiledialogue1.FileName);  
  108. }  
  109. }  
  110. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  111. {  
  112. }  
  113. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)  
  114. {  
  115. }  
  116. private void button2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)  
  117. {  
  118. if (e.Control == true && e.KeyCode == Keys.L)  
  119. {  
  120. button2.PerformClick();  
  121. }  
  122. }  
  123. private void textBox1_TextChanged(object sender, EventArgs e)  
  124. {  
  125. }  
  126. private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)  
  127. {  
  128. }  
  129. private void pictureBox1_Click(object sender, EventArgs e)  
  130. {  
  131. }  
  132. private void Form1_TextChanged(object sender, EventArgs e)  
  133. {  
  134. }  
  135. private void pictureBox1_BackgroundImageChanged(object sender, EventArgs e)  
  136. {  
  137. }  
  138. private void pictureBox1_BackgroundImageChanged_1(object sender, EventArgs e)  
  139. {  
  140. }  
  141. private void groupBox2_Enter(object sender, EventArgs e)  
  142. {  
  143. }  
  144. private void textBox11_TextChanged(object sender, EventArgs e)  
  145. {  
  146. string mainconn = ConfigurationManager.ConnectionStrings["MyCONN"].ConnectionString;  
  147. SqlConnection sqlconn = new SqlConnection(mainconn);  
  148. string sqlquery= "select * from [dbo].[tbl.Amal] where Size='"+textBox1.Text+"'";  
  149. sqlconn.Open();  
  150. SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);  
  151. SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);  
  152. DataTable dt = new DataTable();  
  153. sdr.Fill(dt);  
  154. dataGridView1.DataSource = dt;  
  155. sqlconn.Close();  
  156. }  
  157. }  
  158. }

Answers (2)