con.Open() why not working?

Apr 11 2021 3:54 AM
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 WindowsFormsApp10
{
public partial class Form1 : Form
{
String path = @"Data Source = DESKTOP - 2PC2RB2\SQLEXPRESS; Initial Catalog = edu; Integrated Security = True";
SqlConnection con;
public Form1()
{
InitializeComponent();
con = new SqlConnection(path);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'eduDataSet1.lesson' table. You can move, or remove it, as needed.
this.lessonTableAdapter.Fill(this.eduDataSet1.lesson);
this.reportViewer1.RefreshReport();
this.reportViewer1.RefreshReport();
UpdateData();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("insert into lesson (LID, LName, LCredit, LRel) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3 + "','" + textBox4 + "')", con);
cmd.ExecuteReader();
con.Close();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
UpdateData();
MessageBox.Show("????????? ?????????");
}
public void UpdateData()
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (textBox5.Text != "")
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from lesson where LID = '" +
textBox5.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("????????? ??????????.");
}
else
{
MessageBox.Show("?? ?????? ?????? ????? ??????? ??!!!");
}
}
catch (Exception ex)
{
MessageBox.Show("?? ????? ???? ???????? ?????!!!");
}
UpdateData();
}
}
}
 

Answers (6)