Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload image to SQL Database
    text
    copied!<p>I have been looking at solutions but don't know where I am doing it wrong. But I am doing it for the first time. </p> <p>I have a class Students</p> <pre><code>class Students { public Image photo { get; set; } public bool AddStudent(Students _student) { Settings mySettings = new Settings(); SqlCeConnection conn = new SqlCeConnection(mySettings.StudentsConnectionString); SqlCeCommand cmd = new SqlCeCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = conn; cmd.CommandText = "insert into students (firstname, lastname, dob, allergic, allergydetails, memo, address, photo) " + "Values (" + "'" + @FirstName + "'," + "'" + @LastName + "'," + "'" + @Dob + "'," + "'" + @isAllergic + "'," + "'" + @AllergyDetails + "'," + "'" + @Memo + "'," + "'" + @photo + "'," + "'" + @Address + "')"; cmd.Parameters.Add("@FirstName", _student.FirstName); cmd.Parameters.Add("@LastName", _student.LastName); cmd.Parameters.Add("@Dob", _student.Dob); cmd.Parameters.Add("@isAllergic", _student.isAllergic); cmd.Parameters.Add("@AllergyDetails", _student.AllergyDetails); cmd.Parameters.Add("@Memo", _student.Memo); cmd.Parameters.Add("@photo", _student.photo); cmd.Parameters.Add("@Address", _student.Address); try { conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); return true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return false; } finally { if (conn.State == System.Data.ConnectionState.Open) conn.Close(); cmd = null; } } </code></pre> <p>Now I pass the property values from my form like this.</p> <pre><code> private void btnAdd_Click(object sender, EventArgs e) { Students myStudent = new Students(); myStudent.FirstName = txtFirstName.Text.Trim(); myStudent.LastName = txtLastName.Text.Trim(); myStudent.Dob = dtPicker1.Value; myStudent.Memo = txtMemo.Text.Trim(); myStudent.Address = txtAddress.Text.Trim(); myStudent.photo = Image.FromFile(openFileDialog1.FileName); // Insert New Record if (myStudent.AddStudent(myStudent)) MessageBox.Show("Student Added Successfully"); } </code></pre> <p>I am getting the following error</p> <blockquote> <p>No mapping exists from DbType System.Drawing.Bitmap to a known SqlCeType.</p> </blockquote> <p>But I don't know why I am not succedding. Any Suggestion would be highly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload