|
 |
|
เมื่อเลือกข้อมูลใน Combobox แล้วให้แสดงราคาที่ดึงมาจากฐานข้อมุลมาแสดงใน Textbox C# Access |
|
 |
|
|
 |
 |
|
คือผมต้องการให้ราคามาแสดงบน Textbox โดย ราคาต้องตรงกับ สินค้าที่เลือก ซึ่งเลือกจาก combobox ผมใช้ Access เป็นฐานข้อมูล


Code (C#)
namespace Product
{
public partial class PO : Form
{
private OleDbConnection connection = new OleDbConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public PO()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\Product.accdb;";
}
private void Form1_Load(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from Product ";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
cbname.DataSource = dt;
cbname.DisplayMember = "NamePro";
cbname.ValueMember = "IDPro";
connection.Close();
txtname.Text = "";
}
private void btcal_Click(object sender, EventArgs e)
{
int amounts = 0, prices = 0, pricetotal = 0;
prices = Convert.ToInt32(txtprice.Text);
amounts = Convert.ToInt32(txtamount.Text);
pricetotal = amounts * prices;
txttotal.Text = pricetotal.ToString();
}
private void btadd_Click(object sender, EventArgs e)
{
try
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO PO ([NamePO],[NamePoPro],[PricePoPro],[AmountPo],[TotalPo]) values(@Para1,@Para2,@Para3,@Para4,@Para5)";
command.Parameters.Clear();
command.Parameters.AddWithValue("@Para1", txtnamePO.Text);
command.Parameters.AddWithValue("@Para2", txtname.Text);
command.Parameters.AddWithValue("@Para3", txtprice.Text);
command.Parameters.AddWithValue("@Para4", txtamount.Text);
command.Parameters.AddWithValue("@Para5", txttotal.Text);
int result = command.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Data Saved");
}
else
{
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
string show;
show = "ชื่อผู้สี่งซื้อ :" + txtnamePO.Text +
"\n" +
"ชื่อสินค้า :" + txtname.Text +
"\n" +
"ราคาสินค้าต่อหน่วย :" + txtname.Text + " บาท" +
"\n" +
"จำนวนสินค้าที่สั่ง :" + txtname.Text + " ชิ้น" +
"\n" +
"ราคารวมสินค้า :" + txtname.Text + " บาท";
MessageBox.Show(show, "สั่งซื้อสินค้า");
}
private void cbname_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
if (cbname.Text == cbname.SelectedItem.ToString())
{
txtname.Text = cbname.Text;
}
else
{
txtname.Text = cbname.Text;
}
}
}
}
Tag : Ms Access, VS 2012 (.NET 4.x)

|
|
 |
 |
 |
 |
Date :
2017-08-09 10:24:25 |
By :
sarasakza01 |
View :
3424 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 04
|