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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Customer Group Creation Aif

(0) ShareShare
ReportReport
Posted on by 1,883

Dear all,

I can create customer group with below code. But my issue is if there is multiple data rows in the table then each row is repeating for me with message "Customer group is Created". I wants to upload all the table rows with one click only and with one message "Customer group is Created".  Please help me with this issue. What I have to change in below code!

private void Upload_Click(object sender, EventArgs e)
{
string Query = "select *from CustGroup; ";
SqlCommand cmdDatabase = new SqlCommand(Query, con);
SqlDataReader myReader;

con.Open();
myReader = cmdDatabase.ExecuteReader();

while(myReader.Read())
{
string sGroup = myReader.GetString(myReader.GetOrdinal("Group"));
string sName = myReader.GetString(myReader.GetOrdinal("Name"));

CustomerGroupServiceClient client = new CustomerGroupServiceClient();

CallContext context = new CallContext();
context.Company = "enpr";

AxdCustomerGroup document = new AxdCustomerGroup();

document.CustGroup_1 = new AxdEntity_CustGroup_1[1];
document.CustGroup_1[0] = new AxdEntity_CustGroup_1();
document.CustGroup_1[0].CustGroup = sGroup;
document.CustGroup_1[0].Name = sName;

try
{
EntityKey[] entityKey = client.create(context, document);
MessageBox.Show("CustomerGroup Created");
client.Close();
}
catch
{
client.Abort();
throw;
}
}
myReader.Close();
con.Close();
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    The message box is shown for every record because you call MessageBox.Show() inside the while loop. If you want to execute it just once, put it below the while block. Like this:

    private void Upload_Click(object sender, EventArgs e)
    {
        string Query = "select *from CustGroup; ";
        SqlCommand cmdDatabase = new SqlCommand(Query, con);
        SqlDataReader myReader;
    
        con.Open();
        myReader = cmdDatabase.ExecuteReader();
    
        while(myReader.Read())
        {
            string sGroup = myReader.GetString(myReader.GetOrdinal("Group"));
            string sName = myReader.GetString(myReader.GetOrdinal("Name"));
    
            CustomerGroupServiceClient client = new CustomerGroupServiceClient();
    
            CallContext context = new CallContext();
            context.Company = "enpr";
    
            AxdCustomerGroup document = new AxdCustomerGroup();
    
            document.CustGroup_1 = new AxdEntity_CustGroup_1[1];
            document.CustGroup_1[0] = new AxdEntity_CustGroup_1();
            document.CustGroup_1[0].CustGroup = sGroup;
            document.CustGroup_1[0].Name = sName;
    
            try
            {
                EntityKey[] entityKey = client.create(context, document);
                client.Close();
            }
            catch
            {
                client.Abort();
                throw;
            }
        }
    
    if (...)
    { MessageBox.Show("CustomerGroup Created");
    } myReader.Close(); con.Close(); }

    By the way, please use </> button in the rich formatting view to paste code (as I did). It preserves indentation and makes code easier to copy.

    Also note that SqlDataReader is a pretty low-level API; .NET offers much more powerful ones. For instance, wouldn't you prefer writing myTable.Name instead of myReader.GetString(myReader.GetOrdinal("Group")) and get compile-time control of types?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans