Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm having a little problem inserting images within a database, it just won't work, what am I doing wrong please?

C#
//1. Open database connection using default database
         try
         {
             string connectionString = TestUI.Properties.Settings.Default.UserStoreConnectionString;
             using (SqlConnection con = new SqlConnection(connectionString))
             {
                 bool userExists = false;
                 con.Open();
                 if (userExists)
                 {
                     string commandText = "UPDATE [UserStore].[dbo].[user] SET Name = @Name, Surname = @Surname, @Userimage WHERE UserID = @UserID;";
                     SqlCommand command = new SqlCommand(commandText, con);
                     command.Parameters.AddWithValue("@UserId", 0);
                     MemoryStream _stream = new MemoryStream();
                     pcbMember.Image.Save(_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                     byte[] _pic = _stream.ToArray();
                     command.Parameters.AddWithValue("@UserImage", _pic);
                     if (txtName.Text != null) { command.Parameters.AddWithValue("@Name", txtName.Text); }
                     if (txtSurname.Text != null) { command.Parameters.AddWithValue("@Surname", txtSurname.Text); }
                     command.ExecuteNonQuery();
                     Debug.WriteLine("Existing user saved.");
                 }
                 else if (!userExists)
                 {
                     int _count = 0;
                     string commandText = "INSERT INTO [UserStore].[dbo].[user] VALUES (Name = @Name, Surname = @Surname, UserPicture = @UserPicture, UserID = @UserID);";
                     SqlCommand command = new SqlCommand(commandText, con);
                     command.Parameters.AddWithValue("@UserID", _count++);
                     command.Parameters.AddWithValue("@Name", txtName.Text);
                     command.Parameters.AddWithValue("@Surname", txtSurname.Text);
                     command.Parameters.AddWithValue("@UserPicture", pcbMember.Image);
                     command.ExecuteNonQuery();
                     Debug.WriteLine("New user saved.");
                     con.Close();
                 }
             }
         }
         catch (Exception ex)
         {
             Debug.WriteLine(ex.Message);
             Debug.WriteLine(ex.Source);
             Debug.WriteLine(ex.StackTrace);
         }
         finally
         {

         }
Posted
Comments
DiponRoy 18-Jul-14 15:20pm    
rather than saving image in database, Because after few years it may would turn into a huge problem
save the image at a folder location, and save the location url at database.

1 solution

SQL
No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type.
System.Data


I spent a few hours debugging this, my bookmarks are available here.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900