Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

explain me how to do this in C# visual studio 2015 (Graphic User Interface) I wa

ID: 3850622 • Letter: E

Question

explain me how to do this in C# visual studio 2015 (Graphic User Interface)

I want to write a program that store in database by insert the following inputs (the inputs are typed in a textbox by the user):

-date

- name of products

-price

-quantity

-sales (which equals: price *quantity)

after typing the input the user should click on the insert button to insert this data. another button "delete button" to delete data in the database after selecting it. and another button to update data.

can you explain me how to do it, and the code that I need.

Explanation / Answer

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
  
namespace InsertDelete
{
public partial class frmMain : Data
{
SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
SqlCommand cmd;
SqlDataAdapter adapt;
//ID variable used in Updating and Deleting Record
int ID = 0;
public frmMain()
{
InitializeComponent();
DisplayData();
}
//Insert Data
private void btn_Insert_Click(object sender, EventArgs e)
{
if (price.Text != "" && product.Text != "")
{
cmd = new SqlCommand("insert into tbl_Record(product,price) values(@product,@price)", con);
con.Open();
cmd.Parameters.AddWithValue("@product", price.Text);
cmd.Parameters.AddWithValue("@price", product.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
//Display Data in DataGridView
private void DisplayData()
{
con.Open();
DataTable dt=new DataTable();
adapt=new SqlDataAdapter("select * from tbl_Record",con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
price.Text = "";
product.Text = "";
ID = 0;
}
//dataGridView1 RowHeaderMouseClick Event
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
price.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
product.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
}
  
//Delete Record
private void btn_Delete_Click(object sender, EventArgs e)
{
if(ID!=0)
{
cmd = new SqlCommand("delete tbl_Record where ID=@id",con);
con.Open();
cmd.Parameters.AddWithValue("@id",ID);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Select Record to Delete");
}
}
}
}