
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)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?