Ошибка "Свойство Connection не инициализировано"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
namespace Pop2
{
public partial class Form2 : Form
{
SqlConnection con;
SqlDataAdapter adap;
DataSet ds;
SqlCommandBuilder cmdb1;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection();
con.Open();
string dbLocation = ("C:\\MDK\\Pop2\\Pop2\baza.mdf");
con.ConnectionString = @"Data Source=.\SQLEXPRESS; User Instance=true;Integrated Security=SSPI; AttachDBFilename=" + dbLocation;
SqlConnection connection1 = new SqlConnection(con.ConnectionString);
adap = new SqlDataAdapter("select Name as 'Name', Mesta as 'Mesta', idSeansa as 'Id', Type as 'Type_Zala' from Kino", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "Person Details");
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void save_Click(object sender, EventArgs e)
{
try
{
cmdb1 = new SqlCommandBuilder(adap);
adap.Update(ds, "Person_Details");
MessageBox.Show("Изменения в базе данных выполнены!",
"Уведомление о результатах", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("Изменения в базе данных выполнить не удалось!",
"Уведомление о результатах", MessageBoxButtons.OK);
}
}
private void button2_Click(object sender, EventArgs e)
{
int ind = dataGridView1.SelectedCells[0].RowIndex;
dataGridView1.Rows.RemoveAt(ind);
}
}
}
Это часть программы, при попытки открыть выдает ошибку: свойство connectionstring
не инициализировано, что делать?
Источник: Stack Overflow на русском