TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Lerroy Fernandes
NA
17
10.6k
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.
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.IO;
using
System.Data.SqlClient;
using
System.Configuration;
namespace
Amal_Inventory
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
SqlConnection con =
new
SqlConnection(@
"Data Source=D.\SQLEXPRESS;Initial Catalog=ReportServer$SQLEXPRESS;Integrated Security=True"
);
DataTable table =
new
DataTable();
int
indexRow;
private
void
label1_Click(
object
sender, EventArgs e)
{
}
private
void
button3_Click(
object
sender, EventArgs e)
{
}
private
void
button4_Click(
object
sender, EventArgs e)
{
/*Microsoft.Office.Interop.Excel._Application = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = Application.workbooks.Add(Type.Missing);
*/
}
private
void
button6_Click(
object
sender, EventArgs e)
{
}
private
void
button5_Click(
object
sender, EventArgs e)
{
if
(MessageBox.Show(
"Sure ??"
,
"Delete"
, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)==DialogResult.OK)
{
if
(
this
.dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(
this
.dataGridView1.SelectedRows[0].Index);
}
textBox1.Text =
""
;
textBox2.Text =
""
;
textBox3.Text =
""
;
textBox4.Text =
""
;
textBox5.Text =
""
;
textBox6.Text =
""
;
textBox7.Text =
""
;
textBox8.Text =
""
;
textBox9.Text =
""
;
pictureBox1.Image =
null
;
}
}
private
void
button7_Click(
object
sender, EventArgs e)
{
textBox1.Text =
""
;
textBox2.Text =
""
;
textBox3.Text =
""
;
textBox4.Text =
""
;
textBox5.Text =
""
;
textBox6.Text =
""
;
textBox7.Text =
""
;
textBox8.Text =
""
;
textBox9.Text =
""
;
pictureBox1.Image =
null
;
}
private
void
button8_Click(
object
sender, EventArgs e)
{
Update();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
MemoryStream mmst =
new
MemoryStream();
pictureBox1.Image.Save(mmst, pictureBox1.Image.RawFormat);
byte
[] img = mmst.ToArray();
dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text,
textBox6.Text, textBox7.Text, textBox8.Text, img);
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
string
mainconn = ConfigurationManager.ConnectionStrings[
"MyCONN"
].ConnectionString;
SqlConnection sqlconn =
new
SqlConnection(mainconn);
string
sqlquery=
"select * from [dbo].[tbl.Amal]"
;
sqlconn.Open();
SqlCommand sqlcomm =
new
SqlCommand(sqlquery, sqlconn);
SqlDataAdapter sdr =
new
SqlDataAdapter(sqlcomm);
DataTable dt =
new
DataTable();
sdr.Fill(dt);
dataGridView1.DataSource = dt;
sqlconn.Close();
}
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
{
DialogResult confirm = MessageBox.Show(
"Are you sure?"
,
"Exit"
, MessageBoxButtons.YesNo);
}
private
void
button1_Click(
object
sender, EventArgs e)
{
OpenFileDialog openfiledialogue1 =
new
OpenFileDialog();
openfiledialogue1.Filter =
"Choose Image (*.jpg;*.png;*.gif;*.jpeg)|*.jpg;*.png;*.gif;*.jpeg"
;
if
(openfiledialogue1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image =
new
Bitmap(openfiledialogue1.FileName);
}
}
private
void
dataGridView1_CellContentClick(
object
sender, DataGridViewCellEventArgs e)
{
}
private
void
dataGridView1_CellClick(
object
sender, DataGridViewCellEventArgs e)
{
}
private
void
button2_PreviewKeyDown(
object
sender, PreviewKeyDownEventArgs e)
{
if
(e.Control ==
true
&& e.KeyCode == Keys.L)
{
button2.PerformClick();
}
}
private
void
textBox1_TextChanged(
object
sender, EventArgs e)
{
}
private
void
pictureBox1_LoadCompleted(
object
sender, AsyncCompletedEventArgs e)
{
}
private
void
pictureBox1_Click(
object
sender, EventArgs e)
{
}
private
void
Form1_TextChanged(
object
sender, EventArgs e)
{
}
private
void
pictureBox1_BackgroundImageChanged(
object
sender, EventArgs e)
{
}
private
void
pictureBox1_BackgroundImageChanged_1(
object
sender, EventArgs e)
{
}
private
void
groupBox2_Enter(
object
sender, EventArgs e)
{
}
private
void
textBox11_TextChanged(
object
sender, EventArgs e)
{
string
mainconn = ConfigurationManager.ConnectionStrings[
"MyCONN"
].ConnectionString;
SqlConnection sqlconn =
new
SqlConnection(mainconn);
string
sqlquery=
"select * from [dbo].[tbl.Amal] where Size='"
+textBox1.Text+
"'"
;
sqlconn.Open();
SqlCommand sqlcomm =
new
SqlCommand(sqlquery, sqlconn);
SqlDataAdapter sdr =
new
SqlDataAdapter(sqlcomm);
DataTable dt =
new
DataTable();
sdr.Fill(dt);
dataGridView1.DataSource = dt;
sqlconn.Close();
}
}
}
Reply
Answers (
2
)
How to modify UI from an another thread?
How to create file sharing desktop application with c#?