using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Insert01
{
public partial class Insert02 : Form
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader Dr;
string Pcode,Pname,Bname,Cname,Aname;
int Price,Psize;
public Insert02()
{
InitializeComponent();
conn = new SqlConnection("data source=.\\krittin;initial catalog=ConsumerProducts;integrated security=True;");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Pcode = textBox1.Text;
Pname = textBox2.Text;
Bname = textBox3.Text;
Psize = Convert.ToInt32(textBox4.Text);
if (radioButton1.Checked == true)
{
Cname = "อุปโภค";
}
if (radioButton2.Checked == true)
{
Cname = "บริโภค";
}
Price = Convert.ToInt32(textBox6.Text);
comm = new SqlCommand("select * from Consumer where ProductConsumer='" + Pcode + "'", conn);
conn.Open();
Dr = comm.ExecuteReader();
if (Dr.Read())
{
MessageBox.Show("รหัสนี้มีอยู่แล้ว กรุณาป้อนใหม่อีกครั้ง");
conn.Close();
}
else
{
conn.Close();
comm = new SqlCommand("INSERT into Consumer ('"+Pcode+"' , '"+Pname+"' , '"+Bname+"' , "+Psize+" , '"+Cname+"' , "+Price+")", conn);
comm = new SqlCommand("select * from Consumer where ProductConsumer='" + Pcode + "'", conn);
Check ว่า Field ProductConsumer มีอยู่ใน Table Consumer ไหม