0

Hi everyone i am trying to convert an object that is taken from the database and store to change it to the background of the application

 con.Open();
        string sqlSelectQuery = "Select * FROM Backgrounds where UserID = '" + 1 + "'";
        SqlCommand cmd = new SqlCommand(sqlSelectQuery, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {


               byte[] yourImage = (byte[])(dr["Background"]);
                MemoryStream ms = new MemoryStream(yourImage);
                Image image = Image.FromStream(ms);
                this.BackgroundImage = image;
            }
con.Close();

Error that is shown during the conversion

CREATE TABLE [dbo].[Backgrounds] (
    [UserID]     INT   DEFAULT ((1)) NOT NULL,
    [Background] IMAGE NULL,
    [Logo1]      IMAGE NULL,
    [Logo2]      IMAGE NULL,
    PRIMARY KEY CLUSTERED ([UserID] ASC)
);

What i want is the object that is taken from the database to be retrieved and use as the current background.Please let me know any mistakes that was made.

Thanks in advance.

NoobCoder
  • 27
  • 5
  • 1
    I downvoted your question for [this](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). – ProgrammingLlama Dec 06 '17 at 02:14
  • I am screenshotting the error? – NoobCoder Dec 06 '17 at 02:20
  • In that case, please provide an [mcve] – ProgrammingLlama Dec 06 '17 at 02:20
  • dont understand... didn't i filtered out the necessary codes that would cause the error? – NoobCoder Dec 06 '17 at 02:31
  • I don't see any code. Where is your code? You have already said that the purpose of the image is not to show your code. I'm not about to manually type in all of your code just to test your question. No way. – ProgrammingLlama Dec 06 '17 at 02:34
  • done added code but you will not be able to test this unless you use the same database details – NoobCoder Dec 06 '17 at 02:40
  • Can you add the table definition? – ProgrammingLlama Dec 06 '17 at 02:40
  • Does [this](https://stackoverflow.com/questions/2175209/retrieve-images-from-sql-server-database) help? – ProgrammingLlama Dec 06 '17 at 02:45
  • still get the same problem – NoobCoder Dec 06 '17 at 03:14
  • _"you will not be able to test this unless you use the same database"_ -- which is a strong signal you should observe telling you that you haven't provided a suitable [mcve]. In any case, the `ArgumentException` you're seeing is thrown for any variety of reasons, all involving the bitmap data being invalid or unsupported. – Peter Duniho Dec 06 '17 at 03:32
  • i dont really understand what the code means for the duplicate link that was given... Is there any way you can explain it if not it is quite hard for me to visualize and put it into my project... – NoobCoder Dec 06 '17 at 07:13
  • _"it is quite hard for me to visualize and put it into my project"_ -- the point is that you first need to identify what it is about the data that is preventing it from working. There are many different reasons a stream purporting to be a bitmap would not be decoded successfully. One option is to just catch the exception and ignore such images. But if you want to _fix_ the problem, you need to do some more diagnosis yourself, based on the information in the marked duplicate. You haven't provided enough context here for anyone else to help. – Peter Duniho Dec 07 '17 at 06:26

0 Answers0