รายละเอียดของการตอบ ::
using System.Data.SqlClient;
namespace Project_Consumer
{
public partial class Insert02 : Form
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader dr;
String ProductCode,Name,Brand,Category;
int Sizeml, Price;
public Insert02()
{
InitializeComponent();
conn = new SqlConnection("data source=.\\krittin;initial catalog=ConsumerProducts;integrated security=True;");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ProductCode = textBox1.Text;
Name = textBox2.Text;
Brand = textBox3.Text;
Sizeml = Convert.ToInt32(textBox4.Text);
if (radioButton1.Checked == true)
{
Category = "อุปโภค";
}
if (radioButton2.Checked == true)
{
Category = "บริโภค";
}
Price = Convert.ToInt32(textBox5.Text);
comm = new SqlCommand("select * from Consumer where ProductCode='" + ProductCode + "' ", conn);
conn.Open();
dr = comm.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("รหัสนี้มีอยู่แล้ว กรุณาป้อนใหม่");
conn.Close();
}
else
{
conn.Close();
comm = new SqlCommand("INSERT into Consumer values ('" + ProductCode + "','" + Name + "'," + Sizeml + ",'" + Category + "'," + Price + ")", conn);
if (MessageBox.Show("ยืนยันการบันทึก", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
MessageBox.Show("บันทึกสำเร็จ");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
else
{
this.Close();
}
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
}.