Bug with simple if/else statement in C#

Bug with simple if/else statement in C#

Author
Discussion

Z064life

Original Poster:

1,926 posts

250 months

Sunday 16th December 2007
quotequote all

I keep on getting this error with an if/else statement based on a simple 2 part enumeration.


c:\inetpub\wwwroot\raceaheadwebservice_\BusinessLogic.asmx.cs(510): Operator '==' cannot be applied to operands of type 'raceaheadwebservice_.CRMSDK.QuoteState' and 'raceaheadwebservice_.statuscode1'


c:\inetpub\wwwroot\raceaheadwebservice_\BusinessLogic.asmx.cs(505): 'raceaheadwebservice_.CRMSDK.QuoteState' denotes a 'class' where a 'variable' was expected

c:\inetpub\wwwroot\raceaheadwebservice_\BusinessLogic.asmx.cs(507): 'raceaheadwebservice_.statuscode1' denotes a 'class' where a 'variable' was expected





raceaheadwebservice_.CRMSDK.QuoteState = new raceaheadwebservice_.statuscode1();

raceaheadwebservice_ = new statuscode1();


if (ThisQuote.statecode.Value == raceaheadwebservice_.statuscode1.Closed)

{
throw new Exception("Record is read only.&quotwink;

}






else
{
}

What is the best way to write an if/else statement in CRM? QuoteState is a data type in CRM for state, as is quotestateinfo I believe, so some casting had to be done.

The enum is 1 = Closed and 2 is open. What if I used ints/magic numbers which are hard coded? When I use a retrieve single, does a retrieve multiple have to proceed it?

What is wrong with this code? Whats the solution?

Thanks

Ordinary Bloke

4,559 posts

200 months

Sunday 16th December 2007
quotequote all
If (author=="Z064life&quotwink
{
throwrandomerrormesage=TRUE
}
else
{
easylife()
}

Z064life

Original Poster:

1,926 posts

250 months

Sunday 16th December 2007
quotequote all
ThisQuote.statecode.Value == raceaheadwebservice_.statuscode1.Closed

So I should put quotation marks around this?

c:\inetpub\wwwroot\raceaheadwebservice_\BusinessLogic.asmx.cs(505): 'raceaheadwebservice_.CRMSDK.QuoteState' denotes a 'class' where a 'variable' was expected


How would this error be solved? It's a little vague on detail.

Statuscode1 is an enum class. There is no other class reference written in this block of code. :S

Edited by Z064life on Sunday 16th December 14:22

m12_nathan

5,138 posts

261 months

Sunday 16th December 2007
quotequote all
Can you post the definition of the class that the ThisQuote object is? Is Statecode defined as being of type statuscode1 ?

pm me with the source code if you want to keep it off the forum.

GnuBee

1,275 posts

217 months

Sunday 16th December 2007
quotequote all
Z064life said:
ThisQuote.statecode.Value == raceaheadwebservice_.statuscode1.Closed

So I should put quotation marks around this?

c:\inetpub\wwwroot\raceaheadwebservice_\BusinessLogic.asmx.cs(505): 'raceaheadwebservice_.CRMSDK.QuoteState' denotes a 'class' where a 'variable' was expected


How would this error be solved? It's a little vague on detail.

Statuscode1 is an enum class. There is no other class reference written in this block of code. :S

Edited by Z064life on Sunday 16th December 14:22
There's nothing vague about this error at all - look at the line that's causing this error:

Z064life said:
raceaheadwebservice_.CRMSDK.QuoteState = new raceaheadwebservice_.statuscode1();
You're trying to assign a value/object reference to a class.

Normally the syntax would be <type e.g. class name> <variableName> = <some value> or <some class constructor> etc. However you're not doing this for some reason.

You then do the same thing on the next line with...

Z064life said:
raceaheadwebservice_ = new statuscode1();
Only looking at your code it would appear raceaheadwebservice_ is a namespace so I'm not entirely clear what you are trying to achieve.

Perhaps if you could post more code to place this in context but I suspect that it's a basic issue with understanding how C# deals with the typing of variables.


Edited by GnuBee on Sunday 16th December 23:25

Z064life

Original Poster:

1,926 posts

250 months

Wednesday 19th December 2007
quotequote all
Thanks for the help on that guys, but have another issue now:

As in the first problem of this thread, I have made a webservice which does the following:

Retrieves a set of quotes, casts them as quotedetail.

There is a boolean setup that returns false if the quote cannot be edited - that is if its in one of the states which are read only (active/closed). Otherwise it should return as true and do some calculations based on some fields being used. Casting has been done where appropriate.

Problem is, when I use a quote which is in draft state, and invoke the service using the quote's GUID from the URL, I get an exception and a 500. What I want is to get the quotedetail because the quote is dependent on this, as quotedetail holds the products in the quote. I was getting a parent can't be accessed error before in one of my catch handlers when I did the retrieve of the quotedetails. But the parent record would be the quote record and this is is not read only, so I'm confused. So I've got some code to get records which are relevant (or = entityid).

At the moment, the exception is:

"\n0x80041103\nThe specified attribute does not exist on this entity.\nPlatform\n"

This seems like a simple fix but believe me I have moved my code around so much and I get new errors as I do.

Here's some relevant code:

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

ColumnSet columns = new ColumnSet();
columns.Attributes = new string [] {"quotedetailid, quoteid"};

// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();

condition.Values = new string [] {"quoteid.ToString()"};
condition.Operator = ConditionOperator.Equal;
condition.Values = new string [] {"EntityID.ToString()"};

GnuBee

1,275 posts

217 months

Wednesday 19th December 2007
quotequote all
Z064life said:
Thanks for the help on that guys, but have another issue now:

As in the first problem of this thread, I have made a webservice which does the following:

Retrieves a set of quotes, casts them as quotedetail.

There is a boolean setup that returns false if the quote cannot be edited - that is if its in one of the states which are read only (active/closed). Otherwise it should return as true and do some calculations based on some fields being used. Casting has been done where appropriate.

Problem is, when I use a quote which is in draft state, and invoke the service using the quote's GUID from the URL, I get an exception and a 500. What I want is to get the quotedetail because the quote is dependent on this, as quotedetail holds the products in the quote. I was getting a parent can't be accessed error before in one of my catch handlers when I did the retrieve of the quotedetails. But the parent record would be the quote record and this is is not read only, so I'm confused. So I've got some code to get records which are relevant (or = entityid).

At the moment, the exception is:

"\n0x80041103\nThe specified attribute does not exist on this entity.\nPlatform\n"

This seems like a simple fix but believe me I have moved my code around so much and I get new errors as I do.

Here's some relevant code:

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

ColumnSet columns = new ColumnSet();
columns.Attributes = new string [] {"quotedetailid, quoteid"};

// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();

condition.Values = new string [] {"quoteid.ToString()"};
condition.Operator = ConditionOperator.Equal;
condition.Values = new string [] {"EntityID.ToString()"};
Which line is raising the exception?

And do you really mean...

condition.Values = new string [] {"quoteid.ToString()"};
condition.Operator = ConditionOperator.Equal;
condition.Values = new string [] {"EntityID.ToString()"};

You do realise that this means that condition.Values is being set to a new string array instance whose single element is the literal "quoteid.ToString()" and is then replaced by another string array instance whose single element is the literal "EntityID.ToString()"



Edited by GnuBee on Wednesday 19th December 17:03

m12_nathan

5,138 posts

261 months

Wednesday 19th December 2007
quotequote all
use system.collections.specialized.stringcollection instead of string[]

Z064life

Original Poster:

1,926 posts

250 months

Thursday 20th December 2007
quotequote all
The exception comes up in the catch (obviously), it points to line 324. That's currently this line of code:

BusinessEntityCollection LineItems = service.RetrieveMultiple(query);


InnerText "\n0x80040216\nAn unexpected error occurred.\nPlatform\n" string
Pointing to line 155, which is:

query2.Criteria = filter2;


And finally:


Pointing to line 324, which is:

BusinessEntityCollection LineItems = service.RetrieveMultiple(query);

An unexpected error occurred (whatever that is)...


Edited by Z064life on Thursday 20th December 11:16

GnuBee

1,275 posts

217 months

Thursday 20th December 2007
quotequote all
We'd need more of the source code in order to help you determine where the problem is...

Z064life

Original Poster:

1,926 posts

250 months

Thursday 20th December 2007
quotequote all
Ok no problem. Just to clarify, I'm using the ms crm sdk.

public bool ProcessLineItemsForQuote(Guid EntityID)
{
try
{
//we need to call retrieve Multiple from CRM to get the collection of line item
// Instantiate up the CRM Service.

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

ColumnSet columns = new ColumnSet();
columns.Attributes = new string [] {"quotedetailid", "quoteid"};

// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();

// condition.Values = new string [] {"quoteid.ToString()"};
// condition.Operator = ConditionOperator.Equal;
// condition.Values = new string [] {"quotedetailid".ToString()"};


System.Collections.Specialized.StringCollection.Equals("quoteid", "EntityID&quotwink;
condition.Operator = ConditionOperator.Equal;

// condition.Values = new string [] {quoteid", "EntityID", "" ()};
// condition.AttributeName = "EntityID";






// result = columns.Attributes.ToString("quoteid&quotwink == condition.AttributeNam;

//condition.Operator = ConditionOperator.NotNull;
// if(condition.Values.IsReadOnly == raceaheadwebservice_.CRMSDK.quotedetail)

//If there are no quotedetail line items, then halt:
//If there are, then run the functions (otherwise we don't)


// Create the FilterExpression.
FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] {condition};



// Set the properties of the filter.

// Create the QueryExpression object.
QueryExpression query = new QueryExpression();

// Set the properties of the QueryExpression object.
query.EntityName = EntityName.quotedetail.ToString();
query.ColumnSet = columns;
query.Criteria = filter;


// All quote IDs that are like quote line item IDs, because we want to process these by applying calculations

// Retrieve the line items.
BusinessEntityCollection LineItems = service.RetrieveMultiple(query);

//then loop thorough the collection
int Counter = 0;


while (Counter <= LineItems.BusinessEntities.GetUpperBound(0))
{
try
{

quotedetail ThisLineItem = (quotedetail) LineItems.BusinessEntities[Counter];

decimal Cost = 0;
int quantity = 0;
decimal SellingPrice = 0;


// Cast quantity into int

// RETRIEVE SINGLE

// Create the column set object that indicates the fields to be retrieved.
ColumnSet relevantcolumn = new ColumnSet();

// Set the properties of the column set.
relevantcolumn.Attributes = new string [] {"productid"};

// Retrieve the contact.
// The EntityName indicates the EntityType of the object being retrieved.
product RelevantProduct = (product)service.Retrieve(EntityName.product.ToString(), EntityID, relevantcolumn);
// quotedetail ABC = (quotedetail)service.Retrieve(EntityName.quotedetail.ToString(), ThisLineItem.productid.Value, relevantcolumn);

quantity = (int) RelevantProduct.quantitydecimal.Value;
SellingPrice = RelevantProduct.price.Value;
Cost = RelevantProduct.currentcost.Value;


CRMSDK.CrmFloat PercentageGP = new CrmFloat();

PercentageGP.Value = (float) CalculateGrossProfitPercentage(SellingPrice, quantity, Cost);

ThisLineItem.new_gp_percentage = PercentageGP;


ThisLineItem.new_gp = CalculateGrossProfit(SellingPrice, quantity, Cost).ToString();

// CRM UPDATE

service.Update(ThisLineItem);



//Cast crm money to cost
// CrmMoney priceperunit = new double(SellingPrice);
Counter ++;
}
catch (System.Web.Services.Protocols.SoapException SoapEx)
{
throw new Exception("Soap Error.",SoapEx);
}
catch(Exception ea)
{
throw new Exception("Error",ea);
}
}//close of While loop
}//close of try that encopasses whole function
catch (System.Web.Services.Protocols.SoapException SoapEx)
{
throw new Exception("ab",SoapEx);
}

catch (Exception error)
{//server unable to process request - readonly issue
throw new Exception("a",error);
}

return true;
}




try
{


// RETRIEVE MULTIPLE



// Instantiate up the CRM Service.

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

ColumnSet columns2 = new ColumnSet();
columns2.Attributes = new string [] {"new_deposit", "new_deposit30", "new_depositwithvat", "new_totallessvat", "vatondeposit175"};

// Create the ConditionExpression.
ConditionExpression condition2 = new ConditionExpression();

// Create the FilterExpression.
FilterExpression filter2 = new FilterExpression();

// Set the properties of the filter.

// Create the QueryExpression object.
QueryExpression query2 = new QueryExpression();

// Set the properties of the QueryExpression object.
query2.EntityName = EntityName.invoice.ToString();
query2.ColumnSet = columns2;
query2.Criteria = filter2;

// All quote IDs that are like quote line item IDs, because we want to process these by applying calculations

// Retrieve the line items.
BusinessEntityCollection Items = service.RetrieveMultiple(query2);

//then loop thorough the collection
int Counter = 0;


// RETRIEVE SINGLE


while (Counter <= Items.BusinessEntities.GetUpperBound(0))
{
try
{

invoice item = (invoice) Items.BusinessEntities[Counter];

bool deposit = true;
decimal deposit30 = 0;
decimal depositwithvat = 0;
decimal totallessvat = 0;
decimal vatondeposit = 0;
decimal totalamount = 0;
decimal vatlevel = (decimal) 17.5;

// Cast quantity into int

// RETRIEVE SINGLE

// Create the column set object that indicates the fields to be retrieved.
ColumnSet relevantcolumn = new ColumnSet();

// Set the properties of the column set.
relevantcolumn.Attributes = new string [] {};


// Retrieve the entity.
// The EntityName indicates the EntityType of the object being retrieved.
invoice aninvoice = (invoice)service.Retrieve(EntityName.invoice.ToString(), item.invoiceid.Value, relevantcolumn);
// quotedetail ABC = (quotedetail)service.Retrieve(EntityName.quotedetail.ToString(), ThisLineItem.productid.Value, relevantcolumn);

deposit = aninvoice.new_deposit.Value;
deposit30 = aninvoice.new_deposit30.Value;
depositwithvat = aninvoice.new_depositwithvat.Value;
totallessvat = aninvoice.new_totallessdeposit.Value;
vatondeposit = aninvoice.new_vatondeposit175.Value;
totalamount = aninvoice.totalamount.Value;



//ASSIGNMENT OF 30% DEPOSIT FIELD

CRMSDK.CrmDecimal depositwith30perc = new CrmDecimal();
depositwith30perc.Value = (decimal) Calculate30percent(totalamount);

//ASSIGNMENT OF DEPOSIT + VAT FIELD

CRMSDK.CrmDecimal depositincvat = new CrmDecimal();
depositincvat.Value = (decimal) totaldeposit (totalamount, vatlevel);


//ASSIGNMENT OF TAX CALCULATION

CRMSDK.CrmDecimal taxcalc = new CrmDecimal();
taxcalc.Value = taxlevel(totalamount, vatlevel);

//ASSIGNMENT OF TOTAL LESS DEPOSIT FIELD

CRMSDK.CrmDecimal total_less_dep = new CrmDecimal();
total_less_dep.Value = totallessdeposit (totalamount, vatlevel);

// CRM UPDATE

service.Update(aninvoice);


//Cast crm money to cost
// CrmMoney priceperunit = new double(SellingPrice);
Counter ++;
}
catch (System.Web.Services.Protocols.SoapException SoapEx)
{
throw new Exception("Soap Exception.",SoapEx);
}
catch(Exception ea)
{
throw new Exception("Error",ea);
}
}//close of While loop
}//close of try that encopasses whole function
catch (System.Web.Services.Protocols.SoapException SoapEx1)
{
throw new Exception("",SoapEx1);
}

catch (Exception error11)
{
throw new Exception("",error11);
}
return true;


Edited by Z064life on Thursday 20th December 11:40