Я пытаюсь вставить значения в базу данных, но получаю сообщение об ошибке
"Не удалось преобразовать значение nvarchar 'Year' в тип данных int"
в c#. Я не знаю, как это исправить.
Вот мой код:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("Are you sure you want to save this book?", "Saving Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//open connection to the database
cn.Open();
//command to be executed on the database
cm = new SqlCommand("INSERT INTO Books(BookTitle, Edition, Genre, Author, Publisher, Year, ISBN) VALUES (@BookTitle, @Edition, @Genre, @Author, @Publisher, @Year, @ISBN )", cn);
//set paramaters value
cm.Parameters.AddWithValue("@booktitle", txtBook.Text);
cm.Parameters.AddWithValue("@edition", txtEdition.Text);
cm.Parameters.AddWithValue("@genre", txtGenre.Text);
cm.Parameters.AddWithValue("@author", txtAuthor.Text);
cm.Parameters.AddWithValue("@publisher", txtPublisher.Text);
cm.Parameters.AddWithValue("@year", txtYear.Text);
cm.Parameters.AddWithValue("@isbn", txtISBN.Text);
//ask db to execute query
cm.ExecuteNonQuery();
//close connection
cn.Close();
MessageBox.Show("Record has been sucessfully saved!");
Clear();
frmlist.LoadRecords();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}