none
SQL Server CE Update Photo RRS feed

  • Question

  • bonjour tous le monde

    //Voici le code j'utilise pour update la photo de ma base de

    //donnée contacts en essaie private void UpdateData(string ID) { SqlCeConnection cn = new SqlCeConnection(C.ConnectString()); MemoryStream ms = new MemoryStream(); if (PhotoBox.Image != null) { PhotoBox.Image.Save(ms, ImageFormat.Jpeg); } else { PhotoBox.BackgroundImage.Save(ms, ImageFormat.Jpeg); } byte[] pic_arr = new byte[ms.Length]; ms.Position = 0; ms.Read(pic_arr, 0, pic_arr.Length); if (cn.State == ConnectionState.Closed) { cn.Open(); } string Sql = "Update Contacts set name='" + TxtName.Text + "', photo='" + pic_arr + "' WHERE id= '" + ID + "'"; SqlCeCommand cmd = new SqlCeCommand(Sql, cn); cmd.Parameters.AddWithValue("@Name", TxtName.Text); cmd.Parameters.AddWithValue("@Photo", pic_arr); cmd.ExecuteNonQuery(); MessageBox.Show("Updated"); DGV.LoadData(); AddNew.ResetText(); }


    et voici l'erreur  que j'obtient sur se code


    System.Data.SqlServerCe.SqlCeException
      HResult=0x80004005
      Message=La conversion n'est pas prise en charge. [ Type to convert from (if known) = nvarchar, Type to convert to (if known) = image ]
      Source=SQL Server Compact ADO.NET Data Provider
      Arborescence des appels de procédure :
       à System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
       à System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
       à System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
       à System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
       à PhotoTestDB.MForm.UpdateData(String ID) dans C:\Users\morai\source\repos\PhotoTestDB\PhotoTestDB\MForm.cs :ligne 190
       à PhotoTestDB.MForm.EditBtn_Click(Object sender, EventArgs e) dans C:\Users\morai\source\repos\PhotoTestDB\PhotoTestDB\MForm.cs :ligne 110
       à System.Windows.Forms.Control.OnClick(EventArgs e)
       à System.Windows.Forms.Button.OnClick(EventArgs e)
       à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       à System.Windows.Forms.Control.WndProc(Message& m)
       à System.Windows.Forms.ButtonBase.WndProc(Message& m)
       à System.Windows.Forms.Button.WndProc(Message& m)
       à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.Run(Form mainForm)
       à PhotoTestDB.Program.Main() dans C:\Users\morai\source\repos\PhotoTestDB\PhotoTestDB\Program.cs :ligne 35

    Jarrive pas a comprendre d'ou cela vient

    on peut m'aider SVP

    DanMor


    Daniel

    jeudi 11 juillet 2019 22:21

Réponses

  • Merci Mais Finalement mon erreur etais 

     string Sql = "Update Contacts set name='" + TxtName.Text + "', photo='" + pic_arr + "' WHERE id= '" + ID + "'";
                SqlCeCommand cmd = new SqlCeCommand(Sql, cn);
                cmd.Parameters.AddWithValue("@Name", TxtName.Text);
                cmd.Parameters.AddWithValue("@Photo", pic_arr); 
                cmd.ExecuteNonQuery();

                correction ici

    string Sql = "Update Contacts set name=@Name, photo=@Photo WHERE id= '" + ID + "'";
                SqlCeCommand cmd = new SqlCeCommand(Sql, cn);
                cmd.Parameters.AddWithValue("@Name", TxtName.Text);
                cmd.Parameters.AddWithValue("@Photo", pic_arr); 
                cmd.ExecuteNonQuery();

    Ca fonctionne maintenent


    Daniel

    jeudi 11 juillet 2019 22:48