Я пытаюсь вставить данные в базу данныхНе удалось преобразовать значение параметра из строки в Int32
Но после нажатия кнопки он показывает мне Thos messege ошибка
Не удалось преобразовать значение параметров из Строка в Int32.
мне нужна помощь
Я не знаю, что делать больше. Благодаря!!
private void InsertNewRecord()
{
SqlCommand cmdInsert = new SqlCommand();
cmdInsert.Connection = cn;
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText = "INSERT INTO Customers (" +
" LastName, FirstName, MiddleName, ContactNumber, EmailAddress, BirthDate, CompanyName, Address, BillingAddress, CustomerStatus, CustPicture" +
") VALUES (" +
" @LastName, @FirstName, @MiddleName, @ContactNumber, @EmailAddress, @BirthDate, @CompanyName, @Address, @BillingAddress, @CustomerStatus, @CustPicture" +
") ";
cmdInsert.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = txtLastName.Text;
cmdInsert.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = txtFirstName.Text;
cmdInsert.Parameters.Add("@MiddleName", SqlDbType.VarChar, 50).Value = txtMiddleName.Text;
cmdInsert.Parameters.Add("@ContactNumber", SqlDbType.Int).Value = txtContactNumber.Text;
cmdInsert.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 100).Value = txtEmailAddress.Text;
cmdInsert.Parameters.Add("@BirthDate", SqlDbType.Date).Value = dtpBirthdate.Value;
cmdInsert.Parameters.Add("@CompanyName", SqlDbType.VarChar, 50).Value = txtCompanyName.Text;
cmdInsert.Parameters.Add("@Address", SqlDbType.VarChar, 100).Value = txtAddress.Text;
cmdInsert.Parameters.Add("@BillingAddress", SqlDbType.VarChar, 100).Value = txtBillingAddress.Text;
cmdInsert.Parameters.Add("@CustomerStatus", SqlDbType.VarChar, 10).Value = cboCustomerStatus.Text;
Image bmp;
System.IO.MemoryStream ms;
if (PicImage.Image == null)
{
bmp = Bitmap.FromFile(Application.StartupPath + @"\Anonymous.jpg"); //read the default picture
ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //put it in the memory stream
cmdInsert.Parameters.Add("@CustPicture", SqlDbType.Image).Value = ms.GetBuffer(); //add it as the value of custpictureparameter.
}
else
{
bmp = Bitmap.FromFile(openFileDialog1.FileName);
ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
cmdInsert.Parameters.Add("@CustPicture", SqlDbType.Image).Value = ms.GetBuffer();
}
cmdInsert.ExecuteNonQuery();
Console.WriteLine("Successfully added row to Customers table!");
} //end of InsertNewRecord()
}
Неверные. Преобразование автоматически создается, если возможно (т. Е. Поле может быть преобразовано в int). – jgauffin