Возникло исключение: 'System.Data.SqlClient.SqlException' in System.Data.dll проблема
ошибка:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll Additional information: Cannot insert the value NULL into column 'Id', table 'Login.dbo.Rota'; column does not allow nulls. INSERT fails. The statement has been terminated.
это в основном добавление пустых данных(NULL) в мою базу данных, когда этого не должно быть... я думаю
форма рис: http://postimg.org/image/skqp12obj/[^]
мой код:
private void button2_Click(object sender, EventArgs e) { const string con = "Data Source = dqq5ndqef2.database.windows.net; Initial Catalog = Login; Integrated Security = False; User ID = richardjacobs97; Password = ********; Connect Timeout = 15; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False"; using (SqlConnection connection = new SqlConnection(con)) { connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) using (SqlCommand cmd = new SqlCommand("INSERT INTO Rota (Id, Name, DateWorking) Values (@Id, @Name, @DateWorking)", connection, transaction)) { cmd.CommandType = CommandType.Text; for (int i = 0; i < dataGridView1.Rows.Count; i++) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@Id", dataGridView1.Rows[i].Cells["Id"].Value); cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells["Name"].Value); cmd.Parameters.AddWithValue("@DateWorking", dataGridView1.Rows[i].Cells["DateWorking"].Value); cmd.ExecuteNonQuery(); } transaction.Commit(); } using (SqlCommand cmd = new SqlCommand("SELECT Id, Name, DateWorking FROM Rota", connection)) { DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); dataGridView1.DataSource = dt; } } } } }
любой sugguestions?