我正在尝试将数据插入数据库
但是按下按钮后,它会显示错误信息
无法将参数值从 String 转换为 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()
}