I have a table with 42 columns and one photo column.
But when I insert a photo, it gets inserted into the next row.
Any idea what is going wrong?
My table has a primary key with auto increment
This is my code:
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Brother\Documents\Visual Studio 2013\Projects\EMYS\EMYS\EMYS.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[Member] ([Id], [FirstName], [LastName], [DateOfBirth], [Sex], [Hometown], [Region], [Single], [Married], [Student], [Employed], [Unemployed], [NameOfSpouse], [School], [Occupation], [Telephone], [Email], [HomeAddress], [HomeArea], [PostalAddress], [Baptism], [Confirmation], [ChurchStatus], [ClassLeader], [Brigade], [ChristLittleBand], [GirlsFellowship], [Guild], [MenFellowship], [SingingBand], [SussanaWesley], [WomenFellowship], [YouthChoir], [YouthFellowship], [Choreography], [Drama], [ShiningArmour], [Ushers], [EvangelismPrayerTower], [Organisers], [PraisesWorship]) VALUES (@Id, @FirstName, @LastName, @DateOfBirth, @Sex, @Hometown, @Region, @Single, @Married, @Student, @Employed, @Unemployed, @NameOfSpouse, @School, @Occupation, @Telephone, @Email, @HomeAddress, @HomeArea, @PostalAddress, @Baptism, @Confirmation, @ChurchStatus, @ClassLeader, @Brigade, @ChristLittleBand, @GirlsFellowship, @Guild, @MenFellowship, @SingingBand, @SussanaWesley, @WomenFellowship, @YouthChoir, @YouthFellowship, @Choreography, @Drama, @ShiningArmour, @Ushers, @EvangelismPrayerTower, @Organisers, @PraisesWorship)",con);
cmd.Parameters.AddWithValue("@Id", idTextBox.Text);
cmd.Parameters.AddWithValue("@FirstName", firstNameTextBox.Text);
cmd.Parameters.AddWithValue("@LastName", lastNameTextBox.Text);
cmd.Parameters.AddWithValue("@DateOfBirth", dateOfBirthDateTimePicker.Text);
cmd.Parameters.AddWithValue("@Sex", sexComboBox.Text);
cmd.Parameters.AddWithValue("@Hometown", hometownTextBox.Text);
cmd.Parameters.AddWithValue("@Region", regionComboBox.Text);
cmd.Parameters.AddWithValue("@Single", singleCheckBox.Checked);
cmd.Parameters.AddWithValue("@Married", marriedCheckBox.Checked);
cmd.Parameters.AddWithValue("@Student", studentCheckBox.Checked);
cmd.Parameters.AddWithValue("@Employed", employedCheckBox.Checked);
cmd.Parameters.AddWithValue("@Unemployed", unemployedCheckBox.Checked);
cmd.Parameters.AddWithValue("@NameOfSpouse", nameOfSpouseTextBox.Text);
cmd.Parameters.AddWithValue("@School", schoolTextBox.Text);
cmd.Parameters.AddWithValue("@Occupation", occupationTextBox.Text);
cmd.Parameters.AddWithValue("@Telephone", telephoneMaskedTextBox.Text);
cmd.Parameters.AddWithValue("@Email", emailTextBox.Text);
cmd.Parameters.AddWithValue("@HomeAddress", homeAddressTextBox.Text);
cmd.Parameters.AddWithValue("@HomeArea", homeAreaTextBox.Text);
cmd.Parameters.AddWithValue("@PostalAddress", postalAddressTextBox.Text);
cmd.Parameters.AddWithValue("@Baptism", baptismCheckBox.Checked);
cmd.Parameters.AddWithValue("@Confirmation", confirmationCheckBox.Checked);
cmd.Parameters.AddWithValue("@ChurchStatus", churchStatusComboBox.Text);
cmd.Parameters.AddWithValue("@ClassLeader", classLeaderComboBox.Text);
cmd.Parameters.AddWithValue("@Brigade", brigadeCheckBox.Checked);
cmd.Parameters.AddWithValue("@ChristLittleBand", christLittleBandCheckBox.Checked);
cmd.Parameters.AddWithValue("@GirlsFellowship", girlsFellowshipCheckBox.Checked);
cmd.Parameters.AddWithValue("@Guild", guildCheckBox.Checked);
cmd.Parameters.AddWithValue("@MenFellowship", menFellowshipCheckBox.Checked);
cmd.Parameters.AddWithValue("@SingingBand", singingBandCheckBox.Checked);
cmd.Parameters.AddWithValue("@SussanaWesley", sussanaWesleyCheckBox.Checked);
cmd.Parameters.AddWithValue("@WomenFellowship", womenFellowshipCheckBox.Checked);
cmd.Parameters.AddWithValue("@YouthChoir", youthChoirCheckBox.Checked);
cmd.Parameters.AddWithValue("@YouthFellowship", youthFellowshipCheckBox.Checked);
cmd.Parameters.AddWithValue("@Choreography", choreographyCheckBox.Checked);
cmd.Parameters.AddWithValue("@Drama", dramaCheckBox.Checked);
cmd.Parameters.AddWithValue("@ShiningArmour", shiningArmourCheckBox.Checked);
cmd.Parameters.AddWithValue("@Ushers", ushersCheckBox.Checked);
cmd.Parameters.AddWithValue("@EvangelismPrayerTower", evangelismPrayerTowerCheckBox.Checked);
cmd.Parameters.AddWithValue("@Organisers", organisersCheckBox.Checked);
cmd.Parameters.AddWithValue("@PraisesWorship", praisesWorshipCheckBox.Checked);
cmd.ExecuteNonQuery();
con.Close();
if (photoPictureBox.Image != null)
{
MemoryStream ms = new MemoryStream();
photoPictureBox.Image.Save(ms, photoPictureBox.Image.RawFormat);
byte[] a = ms.GetBuffer();
ms.Close();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@photo", a);
cmd.CommandText = "INSERT INTO Member (photo) values (@photo)";
photoPictureBox.Image = null;
con.Open();
cmd.ExecuteNonQuery();
}
MessageBox.Show("RECORDS Successfully INSERTED","Insert Info");