|
|
|
C# จะให้ Combobox เลือก ข้อมูลจาก Database อย่างเดี่ยว เวลา เพิ่ม ลบ แก้ไขข้อมูล ได้ยังไงครับ |
|
|
|
|
|
|
|
C# จะให้ Combobox เลือก ข้อมูลจาก database อย่างเดี่ยว เวลา เพิ่ม ลบ แก้ไขข้อมูล ได้ยังไงครับ
ในที่นี้มี 2 table เป็น relationship กันครับ customer , agent ที่วงแดง เป็นคีย์นอก คือ agentID
ติดปัญหาคือ เวลาเพิ่มข้อมูลแล้ว เพิ่มไม่ได้ครับ วอนผู้รู้ช่วยทีครับ
Code (VB.NET)
private void loadcomboBox()
{
conn.ConnectionString = strConn;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
string sqlagentID = "select agent.agentID from agent ";
SqlCommand cmmAgent = conn.CreateCommand();
cmmAgent.CommandText = sqlagentID;
SqlDataReader readerAgent = cmmAgent.ExecuteReader();
while (readerAgent.Read())
{
agentIDComboBox.Items.Add(readerAgent.GetValue(0).ToString());
}
conn.Close();
private void btnAddSave_Click(object sender, EventArgs e)
{
if (txtcustomerID.Text != "" && txtcustomerName.Text != "" && txtcontactName.Text !="" && txtplace.Text != "" && txtNo.Text != "" && txtState.Text != "" )
string sql;
conn.ConnectionString = strConn;
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Open();
sql = "SELECT agentID FROM dbo.agent="+agentIDComboBox.Text;
SqlCommand cmmAgent = conn.CreateCommand();
cmmAgent.CommandText = sql;
SqlDataReader readerAgent = cmmAgent.ExecuteReader();
readerAgent.Read();
string agentID = readerAgent.GetValue(0).ToString();
while (readerAgent.Read())
{
agentIDComboBox.Items.Add(readerAgent.GetValue(0).ToString());
}
conn.Close();
conn.Open();
readerAgent.Read();
sql = "insert into product values(@custumerID,@customerName,@contactName,@place,@number,@state)";
SqlCommand cmm = conn.CreateCommand();
cmm.Parameters.AddWithValue("@custumerID", txtcustomerID.Text);
cmm.Parameters.AddWithValue("@customerName", txtcustomerName.Text);
cmm.Parameters.AddWithValue("@contactName", txtcontactName.Text);
cmm.Parameters.AddWithValue("@place", txtplace.Text);
cmm.Parameters.AddWithValue("@number", txtNo.Text);
cmm.Parameters.AddWithValue("@state",txtState.Text);
cmm.CommandText = sql;
cmm.ExecuteNonQuery();
conn.Close();
buttonDefault();
dgvShow();
}
else
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ");
}
}
Tag : Ms SQL Server 2005, C#
|
|
|
|
|
|
Date :
2011-09-16 19:45:39 |
By :
kanokpong |
View :
3047 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.DataSource = dt;
columnComboBox.comboBox.DisplayMember = "Name";
columnComboBox.comboBox.ValueMember = "ID";
|
|
|
|
|
Date :
2011-09-17 08:24:09 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : webmaster เมื่อวันที่ 2011-09-17 08:24:09
รายละเอียดของการตอบ ::
..ขอบคุณครับ
แต่มีปัญหาอีกแล้วครับ Y_Y ท่าน งงๆครับ
เพิ่มไม่ได้อีกแล้ว มีปัญหาแต่ combobox และ ตารางที่เป็น รีเรชั่นกัน
Code (C#)
private void Orderdetail_Load(object sender, EventArgs e)
{
dgvShow();
buttonDefault();
}
private void dgvShow()
{
conn.ConnectionString = strConn;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
string sql ="select [order].orderNo,product.productID,orderdetail.unit,orderdetail.price_unit,orderdetail.discount from product,orderdetail,[order]";
sql += "where [order].orderNo=orderdetail.orderNo and ";
sql += "product.productID=orderdetail.productID";
SqlCommand cmm = conn.CreateCommand();
cmm.CommandText = sql;
SqlDataReader reader = cmm.ExecuteReader();
dgvorderdetail.Rows.Clear();
dgvorderdetail.Columns.Clear();
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.HeaderText = "เลขที่ใบสั่งซื้อ";
col1.Width = 90;
DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.HeaderText = "รหัสสินค้า";
col2.Width = 100;
DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
col3.HeaderText = "จำนวน";
col3.Width = 100;
DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
col4.HeaderText = "ราคาต่อหน่วย";
col4.Width = 125;
DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn();
col5.HeaderText = "ส่วนลด";
col5.Width = 100;
dgvorderdetail.Columns.AddRange(new DataGridViewColumn[] { col1, col2, col3, col4, col5 });
int i = 0;
while (reader.Read())
{
dgvorderdetail.Rows.Add();
dgvorderdetail.Rows[i].Cells[0].Value = reader.GetValue(0).ToString();
dgvorderdetail.Rows[i].Cells[1].Value = reader.GetValue(1).ToString();
dgvorderdetail.Rows[i].Cells[2].Value = reader.GetValue(2).ToString();
dgvorderdetail.Rows[i].Cells[3].Value = reader.GetValue(3).ToString();
dgvorderdetail.Rows[i].Cells[4].Value = reader.GetValue(4).ToString();
i++;
}
conn.Close();
}
private void buttonDefault()
{
btnAddSave.Text = "Add";
btnEditUpdate.Text = "Edit";
btnDelete.Text = "Delete";
btnCancel.Text = "Close";
btnAddSave.Enabled = true;
btnEditUpdate.Enabled = true;
btnDelete.Enabled = true;
btnCancel.Enabled = true;
orderNoComboBox.Items.Clear();
productIDComboBox.Items.Clear();
unitTextBox.Clear();
price_unitTextBox.Clear();
discountTextBox.Clear();
}
private void loadcomboBox()
{
orderNoComboBox.Items.Clear();
productIDComboBox.Items.Clear();
string strConn = "Server= ADMIN-PC\\sqlexpress;User ID=sa;Password=1234567;Database=ITProduct;Connect Timeout =120";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConn;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
string sqlorderNo = "SELECT * FROM [order] ";
SqlCommand cmmOrder = conn.CreateCommand();
cmmOrder.CommandText = sqlorderNo;
SqlDataReader readerOrder = cmmOrder.ExecuteReader();
while (readerOrder.Read())
{
orderNoComboBox.Items.Add(readerOrder.GetValue(0).ToString());
}
conn.Close();
conn.Open();
string sqlproductID = "SELECT * FROM product";
SqlCommand cmmProduct = conn.CreateCommand();
cmmProduct.CommandText = sqlproductID;
SqlDataReader readerProduct = cmmProduct.ExecuteReader();
while (readerProduct.Read())
{
productIDComboBox.Items.Add(readerProduct.GetValue(0).ToString());
}
conn.Close();
}
private void dgvorderdetail_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow row = new DataGridViewRow();
row = dgvorderdetail.Rows[e.RowIndex];
orderNoComboBox.Text = row.Cells[0].Value.ToString();
productIDComboBox.Text = row.Cells[1].Value.ToString();
unitTextBox.Text = row.Cells[2].Value.ToString();
price_unitTextBox.Text = row.Cells[3].Value.ToString();
discountTextBox.Text = row.Cells[4].Value.ToString();
}
private void btnAddSave_Click(object sender, EventArgs e)
{
if (btnAddSave.Text == "Add")
{
btnAddSave.Text = "Save";
btnCancel.Text = "Cancel";
btnAddSave.Enabled = true;
btnCancel.Enabled = true;
btnDelete.Enabled = false;
btnEditUpdate.Enabled = false;
orderNoComboBox.Items.Clear();
productIDComboBox.Items.Clear();
unitTextBox.Clear();
price_unitTextBox.Clear();
discountTextBox.Clear();
loadcomboBox();
}
else
{
if (unitTextBox.Text != "" && price_unitTextBox.Text != "" && discountTextBox.Text != "")
{
string sql;
conn.ConnectionString = strConn;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
sql = "select orderNo from [order] where orderNo='" +orderNoComboBox.Text + "'";
SqlCommand cmmorderNo = conn.CreateCommand();
cmmorderNo.CommandText = sql;
SqlDataReader rederorderNo = cmmorderNo.ExecuteReader();
rederorderNo.Read();
string orderNo = rederorderNo.GetValue(0).ToString();
conn.Close();
conn.Open();
sql = "select productID from product where productID='" +productIDComboBox.Text + "'";
SqlCommand cmmproductID = conn.CreateCommand();
cmmproductID.CommandText = sql;
SqlDataReader rederproductID = cmmproductID.ExecuteReader();
rederproductID.Read();
string productID = rederproductID.GetValue(0).ToString();
conn.Close();
conn.Open();
sql = "insert into orderdetail values("+orderNo+","+productID+"," + unitTextBox.Text +","+price_unitTextBox.Text +","+discountTextBox.Text +")";
SqlCommand cmm = conn.CreateCommand();
cmm.CommandText = sql;
cmm.ExecuteNonQuery();
conn.Close();
buttonDefault();
dgvShow();
}
else
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ");
}
}
|
|
|
|
|
Date :
2011-09-17 15:03:26 |
By :
kanokpong |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แล้วครับ ใช้ parameter เหอๆ
|
|
|
|
|
Date :
2011-09-17 19:19:14 |
By :
kanokpong |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|