web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Copy picture from one DB to another

(0) ShareShare
ReportReport
Posted on by

Hi everyone.

I need copy picture from one DB to another. 

InventItemImage table have 3 field. Format - Enum, Image - Bitmap and ItemId - str.

I try 

loginProperty    = new LoginProperty();

        loginProperty.setServer  (systemParameters.Ax4ServerName);
        loginProperty.setDatabase(systemParameters.Ax4DatabaseName);

        odbc =  new ODBCConnection(loginProperty);

        if(odbc)
        {
            binData = new BinData();

            statement       = odbc.createStatement();

            queryText   = strfmt(<>);

            perm = new SqlStatementExecutePermission(queryText);
            
            perm.assert();

            odbc.ttsbegin();

            resultSet = statement.executeQuery(queryText);
            CodeAccessPermission::revertAssert();

            resultSet.next();

            inventItemImage = InventItemImage::findOrCreate(_inventTable.ItemId, InventItemImageFormat::FullAx, true);
            
            // in this place i can't get picture from resultSet

            odbc.ttscommit();

            inventItemImage.Image = binData.getData();
            inventItemImage.update();

            _inventTable.IsImage = NoYes::Yes;
            _inventTable.update();

I try another

sqlConnection = new System.Data.SqlClient.SqlConnection(strFmt("Data Source=%1;Initial Catalog=%2;Integrated Security=True",
        systemParameters.Ax4ServerName,
        systemParameters.Ax4DatabaseName));

    queryText   = strfmt(<>);

    sqlCommand    = new System.Data.SqlClient.SqlCommand(queryText, sqlConnection);

    try
    {
        sqlConnection.Open();

        sqlCommand.ExecuteNonQuery();
        
        binData = new BinData();
       
        binData.setData(sqlCommand.get_Container()); 
// in this place i have error "setData Invoked with invalid parameters"

        sqlCommand.Dispose();

        sqlConnection.Close();
        sqlConnection.Dispose();

        ttsBegin;

        inventItemImage = InventItemImage::findOrCreate(_itemId, InventItemImageFormat::FullAx, true);

        inventItemImage.Image = binData.getData();
        inventItemImage.update();

        ttsCommit;
    }
    catch
    {
        info(CLRInterop::getLastException().ToString());
        sqlConnection.Close();
    }

Please help

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    239,257 Most Valuable Professional on at

    Let's focus on your latter attempt.

    If you want get data from the database, you must run a query, therefore using ExecuteNonQuery() won't help you. Use ExecuteReader() instead, which will give you SqlDataReader object. Use its Read() method to iterate records and the Item property to access columns. Of course, you'll need a meaningful select statement in your queryText variable.

  • Community Member Profile Picture
    on at

    Thanks. Now code looks like

           Bitmap    image;

            .......        

           sqlConnection.Open();

           sqlDataReader = sqlCommand.ExecuteReader();     

           if (sqlDataReader.get_HasRows())

           {

               while (sqlDataReader.Read())

               {

                   idx = sqlDataReader.get_FieldCount();  // return '1' - it's true

                   fieldName = sqlDataReader.GetName(0);  // return 'Image' - it's true

                   //image = sqlDataReader.get_Item(0); // error "Wrong types"

                   binData = new BinData();

                   binData.setData(sqlDataReader.get_Item(0)); // error "invalid parameters"

                   .....

               }

    What am I doing wrong?

  • Suggested answer
    Martin Dráb Profile Picture
    239,257 Most Valuable Professional on at

    SQL Server doesn't know anything about X++ Bitmap type, so it shouldn't be surprising that such an assignment fails.

    If you don't know what the type is, use GetFieldType(0), or assign a return value of GetValue() to a System.Object variable and check its type in debugger.

    I think you'll be able to assign the value to a variable of type System.Byte[].

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 26

#2
imran ul haq Profile Picture

imran ul haq 8

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 4 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans