private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=YUVV-PC\SQLEXPRESS;Initial Catalog=barcode;Integrated Security=True";
con.Open();
//MessageBox.Show("connection open");
SqlDataAdapter ada = new SqlDataAdapter();
//ada.SelectCommand = new SqlCommand("select * from barcode", con);
ada.MissingSchemaAction = MissingSchemaAction.AddWithKey;
ada.InsertCommand = new SqlCommand("INSERT INTO barcode (bcd) " +
"VALUES (@bcd)", con);
ada.InsertCommand.Parameters.Add("@bcd", SqlDbType.NChar, 20,"bcd").Value = textBox1.Text;
ada.SelectCommand = new SqlCommand("select bcd from barcode", con);
DataSet ds = new DataSet();
ada.Fill(ds, "barcode");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); con.ConnectionString =
Share
You have 2 options for amending your code as follows (after
con.Open()):Alternatively you can use a
SqlCommandlike so:I’d also recommend using a
usingstatement around yourSqlConnectionandSqlDataAdapterinstances to ensure all resources are correctly disposed of.