データベースにデータを挿入しようとしています
しかし、ボタンを押すと、エラーメッセージが表示されます
パラメータ値を文字列から 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()
}