0% found this document useful (0 votes)
5 views

Coding input date sales

The document contains a C# Windows Forms application for managing sales transactions. It includes methods for connecting to a SQL database, displaying sales data, inserting new sales records, and calculating total sales price based on product quantity and price. The application also populates a dropdown with product information upon loading the form.

Uploaded by

eko ardhiyanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Coding input date sales

The document contains a C# Windows Forms application for managing sales transactions. It includes methods for connecting to a SQL database, displaying sales data, inserting new sales records, and calculating total sales price based on product quantity and price. The application also populates a dropdown with product information upon loading the form.

Uploaded by

eko ardhiyanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace salesapp
{
public partial class penjualan : Form
{
public penjualan()
{
InitializeComponent();
}
private SqlConnection koneksi()
{
string connectionString = @"Data Source=eko;Initial Catalog=salesdb;Integrated
Security=true";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}

private void tampilkandata()


{
SqlConnection conn = koneksi();
SqlDataAdapter DA = new SqlDataAdapter("SELECT * FROM sales", conn);
DataSet DS = new DataSet();
DA.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];

private void button1_Click(object sender, EventArgs e)


{
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Please select a product.");
return;
}

int productId = ((dynamic)comboBox1.SelectedItem).ProductID;


int quantity = int.Parse(textBox1.Text);
DateTime saleDate = dateTimePicker1.Value;

using (SqlConnection conn = koneksi())


{
string query = "INSERT INTO Sales (ProductID, Quantity, SaleDate) VALUES (@ProductID,
@Quantity, @SaleDate)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@ProductID", productId);
cmd.Parameters.AddWithValue("@Quantity", quantity);
cmd.Parameters.AddWithValue("@SaleDate", saleDate);
cmd.ExecuteNonQuery();
}

MessageBox.Show("Sale saved successfully!");


tampilkandata();

using (SqlConnection conn = koneksi())


{
string query = "SELECT sales.Quantity, products.Price, (sales.Quantity * products.Price) AS
TotalPrice FROM Sales INNER JOIN Products ON sales.ProductID = products.ProductID";

SqlCommand cmd = new SqlCommand(query, conn);


object result = cmd.ExecuteScalar();
label4.Text = "Total : " + result.ToString();
}

private void penjualan_Load(object sender, EventArgs e)


{
tampilkandata();

SqlConnection conn = koneksi();


string query = "SELECT ProductID, ProductName FROM Products";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(new { ProductID = reader.GetInt32(0), ProductName =
reader.GetString(1) });
}
}

private void button2_Click(object sender, EventArgs e)


{

}
}
}

You might also like