When the check against the emailaddress is added to the if statement, as per below, the if statement if (origin.Equals(true)).
bool origin = false;
Contact contact = item as Contact;
foreach (Item subItem in contactItems)
{
Contact subcontact = subItem as Contact;
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)) && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)) && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
{
if (origin.Equals(true))
{
try
{
Console.WriteLine(contact.DisplayName + " " + subcontact.DisplayName);
Console.WriteLine(contact.EmailAddresses[EmailAddressKey.EmailAddress1]);
subcontact.Delete(DeleteMode.HardDelete);
}
catch
{
Console.WriteLine("Cannot delete" + " " + subcontact.DisplayName);
}
}
origin = true;
}
}
If I remove the if (origin.Equals(true)) or removed && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])) from the other if statement, the contact goes through the try-catch block.
Can anyone see why?
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)) && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)) && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
should be:
if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null)
&& ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName))
&& ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))
the problem as I can see, is in the amount of ( ) you use, which I believe is incorrect.
Try to clean up your if statements to prevent these types of mistakes from happening, since this one was pretty long, and a few of your parenthesis are unnecessary
EDIT:
I tried running some code and it works. I did make some mistakes in the sample of the IF statement I mentioned above.. Maybe try copying my code, and substituting your specific code into it. It should work this way.
public static void Main()
{
bool origin = false;
int[] t = {0,4,6,8,5,6,4,5};
foreach(int i in t){
if ((true || (true && true)) // if ((contact.DisplayName.Equals(subcontact.DisplayName) || (contact.DisplayName is null && subcontact.DisplayName is null))
&& ((true && true) || (true)) // && ((contact.CompanyName is null && subcontact.CompanyName is null) || (contact.CompanyName.Equals(subcontact.CompanyName)))
&& ((true && true) || (true))) // && ((contact.EmailAddresses[EmailAddressKey.EmailAddress1] is null && subcontact.EmailAddresses[EmailAddressKey.EmailAddress1] is null) || (contact.EmailAddresses[EmailAddressKey.EmailAddress1].Equals(subcontact.EmailAddresses[EmailAddressKey.EmailAddress1])))))
{
if (origin.Equals(true))
{
try
{
Console.WriteLine("here");
}
catch
{
Console.WriteLine("Cannot delete" );
}
}
origin = true;
}
}
}
I suggest a little Helper:
private bool EqualOrBothNull( string fromContact, string fromSubcontact )
{
if ( fromContact == null && fromSubcontact == null ) return true;
if ( fromContact != null && fromContact.Equals(fromSubcontact) ) return true;
return false;
}
then you can use it like this:
if ( EqualOrBothNull( contact.DisplayName, subContact.DisplayName ) &&
EqualOrBothNull( contact.CompanyName, subContact.CompanyName ) &&
EqualOrBothNull( contact.EmailAddresses[EmailAddressKey.EmailAddress1],
subContact.EmailAddresses[EmailAddressKey.EmailAddress1])
)
{
// ...
}
This increases readability and will make it easier to find the bug if any.
Thanks for all the potential answers unfortunately they didn't fix the issue, the code is much easier to read though! In the end I separated the email address check to another IF statement which seems to be working
Related
I have a piece of code, in which I set true or false depending upon the conditions.
Below is that code
public bool HackerTextExistOrNot(string text)
{
bool flgValid = false;
var attackChars = new char[] { '=', '+', '-', '#' };
if(attackChars.Contains(text[0]))
{
flgValid = false;
}
else
{
flgValid = true;
}
return flgValid;
}
I have checked for both the bool conditions, but it always goes in strReturnId in main function.
Below is the code.
public static string SaveRecord(RRSOCSaving RRSOCSaving, string Indication)
{
string strReturnId = "";
string strAppURL = ConfigurationManager.AppSettings["AppUrl"].ToString();
string strmail_Content = "";
CommonDB commonObj = new CommonDB();
GET_DATA_BY_STORE objGetData = new GET_DATA_BY_STORE();
try
{
if (objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_CODE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STATE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.CITY) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SITE_STORE_FORMAT) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_SITENAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_SITENAME_LANDL_1) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_SITENAME_LANDL_2) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_ASST_MANAGER_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_ASST_MANAGER_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_MANAGER_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.MANAGER_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.EMP_NEAREST_STORE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.EMP_NEAREST_STORE_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SUPERVISOR_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SUPERVISOR_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SECURITY_SUP_NAME_STORE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SECURITY_SUP_MOBNO_STORE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NAME_ALIGNED_LPO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LPO_MOBILENO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_ALPO_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_ALPO_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.AREA_MANAGER_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.AREA_MANAGER_MOBNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ZONAL_HEAD_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ZONAL_HEAD_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.DVR_IP_ADDRESS) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SIGNET_IP_ADDRESS) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_POLICE_STN_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_POLICE_STN_CONTNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_HOSP_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_HOSP_CONTNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_FIRE_STN_CONTNAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NEAREST_FIRE_STN_CONTNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_ADDRESS) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_SPACE_SQFT) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LAUNCH_DATE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.CST_TIN_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STORE_EMAILID) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NO_OF_POS) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NO_OF_CAMERA) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.DVR_MODEL_GESECURITY) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.CAMERA_MODEL) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALIGNED_LPO_MAILDID) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.FACILTY_TEAMNAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.FACILITY_TEAMNO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STATE_HEAD_OPS_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.STATE_HEAD_OPS_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LPA) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SLP_STATE_HEAD) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SLP_STATE_HEAD_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.UserName) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.CREATED_DATE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.UserName) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LAST_UPDATED_DATE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ISACTIVE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LATITUDE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.LONGITUDE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SLP_EMAILID) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ZONAL_ECNUMBER) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ZONAL_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SLP_STATE_ECNUMBER) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_ALPO_ECNUMBER) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.IS_STORE_IN_MALL) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.MALL_CONTROL_ROOM_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.IS_NIGHT_SEC_GUARD_AVAIL) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NIGHT_SEC_GUARD_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.NIGHT_SEC_GUARD_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.IS_NIGHT_PATROL_PARTY_AVAIL) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.PATROL_PARTY_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.PATROL_PARTY_NO) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_ALPO_EMAILID) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALIGNED_LPO_ECNUMBER) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.SLP_STATE) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.FORMAT_GROUP) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_NAME) ||
objGetData.HackerTextExistOrNot(RRSOCSaving.ALPM_ECNUMBER))
{
strReturnId = "Something went wrong due to malicious script attack..!!!";
}
else
{
if (RRSOCSaving.ROLE_ASSIGNED == "SLP State Head")
{
bool blnState1 = Array.Exists(RRSOCSaving.ASSIGNED_STATE.ToString().ToUpper().Split(','), element => element == (RRSOCSaving.STATE).ToString().ToUpper());
if (blnState1)
{
strmail_Content = Get_Email_Content(RRSOCSaving.STORE_CODE, RRSOCSaving.UserName, Indication, RRSOCSaving.STATE, RRSOCSaving.SITE_STORE_FORMAT, RRSOCSaving.STORE_SITENAME);
// SendEmail(RRSOCSaving.UserName, RRSOCSaving.STORE_CODE, RRSOCSaving.SLP_EMAILID, ConfigurationManager.AppSettings["NHQEmail"].ToString(), strmail_Content, Indication);
strReturnId = CommonDB.INSERT_INTO_RRSOC_INFO(RRSOCSaving, Indication);
}
else
{
strReturnId = "User can add data for " + RRSOCSaving.ASSIGNED_STATE + " only";
}
}
else if (RRSOCSaving.ROLE_ASSIGNED == "NHQ Admin")
{
strmail_Content = Get_Email_Content(RRSOCSaving.STORE_CODE, RRSOCSaving.UserName, Indication, RRSOCSaving.STATE, RRSOCSaving.SITE_STORE_FORMAT, RRSOCSaving.STORE_SITENAME);
// SendEmail(RRSOCSaving.UserName, RRSOCSaving.STORE_CODE, RRSOCSaving.SLP_EMAILID, ConfigurationManager.AppSettings["NHQEmail"].ToString(), strmail_Content, Indication);
strReturnId = CommonDB.INSERT_INTO_RRSOC_INFO(RRSOCSaving, Indication);
//strReturnId = "Record Saved Succesfully";
}
}
}
catch (Exception)
{
throw;
}
return strReturnId;
}
UPDATE I mean to say always in
strReturnId = "Something went wrong due to malicious script attack..!!!";
It seems like your
HackerTextExistOrNot
method returns true when hacker text does NOT exist. Instead of using flgValid just return attackChars.Contains(text[0]) and it should be working correctly.
One more thing - you are creating table each time entering this method, you might consider refactoring this code.
I have a ConcurrentDictionary of Attributes for products. These attributes have the product ID and values for the names of the attribute and any options the attribute has. I have this ConcurrentDictionary because I have threads that are created to handle each attribute in the dictionary by attribute name.
if (knownAttribute.AttributeType.Value.Equals("Product Specification"))
{
Console.WriteLine("Started a thread for: " + knownAttribute.AttributeTypeId + ", " + knownAttribute.Value);
while (true)
{
/* if (AS400SpecificationAttributes.IsEmpty && knownSpecificationBag.IsEmpty && gatherRowsTasks.All(x => x.IsCompleted))
break;*/
AS400SpecificationAttribute AS400SpecificationAttributeWork = null;
AS400SpecificationAttributeWork = knownSpecificationBag.Keys.FirstOrDefault(x => x.AttributeName == knownAttribute.Value);
if (AS400SpecificationAttributeWork != null)
{
var product = ctx.Products.FirstOrDefault(x => x.ProductNumber == AS400SpecificationAttributeWork.ProductNumber);
if (product == null)
continue;
var productAttribute = new ProductAttribute();
productAttribute.Attribute = knownAttribute;
if (AS400SpecificationAttributeWork.AttributeValue != null)
{
var knownAttributeOption = ctx.AttributeOptions.FirstOrDefault(x => x.Attribute.Equals(knownAttribute) && x.Value.Equals(AS400SpecificationAttributeWork.AttributeValue));
if (knownAttributeOption == null)
{
knownAttributeOption = new AttributeOption();
knownAttributeOption.Value = AS400SpecificationAttributeWork.AttributeValue;
knownAttributeOption.Attribute = knownAttribute;
ctx.AttributeOptions.InsertOnSubmit(knownAttributeOption);
ctx.SubmitChanges();
}
productAttribute.AttributeOption = knownAttributeOption;
productAttribute.AttributeOptionId = knownAttributeOption.Id;
}
product.ProductAttributes.Add(productAttribute);
ctx.SubmitChanges();
string tmpstr = null;
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
Thread.Sleep(50);
}
else
{
if (tryCounter < 5)
{
tryCounter++;
Thread.Sleep(1000);
Console.WriteLine("Thread waiting for work: Product Specification:" + knownAttribute.Value);
continue;
}
else
{
int outVal;
threadTracker.TryRemove("Product Specification:" + knownAttribute.Value, out outVal);
Console.WriteLine("Closing Thread: Product Specification:" + knownAttribute.Value);
break;
}
}
Thread.Sleep(50);
}
It seems like the following Attribute element refuses to be removed.
I don't understand why. If i put it in a while(!dic.tryRemove(ele)) it will forever be stuck and never move from there.
There may be an error somewhere within the thread but I have no idea why.
This statement
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr))
will always return true or false. It won't block. That's the behavior of ConcurrentDictionary. It will return false if the key is not in the dictionary.
If you're looping while that method returns false and it's stuck, that means that the item isn't in the dictionary when the loop begins. Either it either was never in the dictionary or that another thread already removed it.
Is your intention to loop until the item is not in the dictionary?
You could try this:
if (!knownSpecificationBag.TryRemove(AS400SpecificationAttributeWork, out tmpstr)
&& !knownSpecificationBag.ContainsKey(AS400SpecificationAttributeWork))
Implement proper equals and gethashcode when using TryRemove
public override int GetHashCode()
{
return new { this.name, this.value, this.group, this.productNumber }.GetHashCode();
}
public bool Equals(AS400SpecificationAttribute other)
{
if (other == null)
return false;
return (this.ProductNumber.Equals(other.productNumber) && ((this.group != null && this.group.Equals(other.AttributeGroup)) || (this.group == null && other.AttributeGroup == null)) && ((this.name!= null && this.name.Equals(other.AttributeName)) || (this.name == null && other.AttributeName == null)) && ((this.value != null && this.value.ToUpper().Equals(other.AttributeValue.ToUpper())) || (this.value == null && other.AttributeValue == null)));
}
how to solve the error which contains operator '&&' cannot be applied to operands of type 'string' and 'string'. The code fallows as :
bool AttorneysData()
{
if (((txtFirstName.Text != "")
&& ((txtSurname.Text != "")
&& (txtAddress.Text
&& (txtTelNo.Text
&& (txtEmail.Text
&& (txtCreatePassword.Text
&& (txtConfirmPassword.Text
&& (cboDisciplineExpertise.Text
&& (cboGeographicalLocation.Text
&& (cboBudgetEstimation.Text
&& (txtNoofIndividuals.Text
&& (cboTypeofClients.Text
&& (txtCompanyName.Text && txtYearsofExperience.Text))))))))))))))
{
return true;
}
else
{
return false;
}
}
You need to use logical operators each time. IF you want to chack all strings to inequality to empty string then use String.IsNullOrEmpty() method instead of != operator.
Also there is no reason to use () in your expression. You need to use brackets to prioritize operations but in your code there is no prioritets conflict because all operations can executes successively from left to right.
Your if statement already contains logical expression that returns boolean result then you can swap it with return operator.
return String.IsNullOrEmpty(txtFirstName.Text)
&& String.IsNullOrEmpty(txtSurname.Text)
&& String.IsNullOrEmpty(txtAddress.Text)
&& String.IsNullOrEmpty(txtTelNo.Text)
&& String.IsNullOrEmpty(txtEmail.Text)
&& String.IsNullOrEmpty(txtCreatePassword.Text)
&& String.IsNullOrEmpty(txtConfirmPassword.Text)
&& String.IsNullOrEmpty(cboDisciplineExpertise.Text)
&& String.IsNullOrEmpty(cboGeographicalLocation.Text)
&& String.IsNullOrEmpty(cboBudgetEstimation.Text)
&& String.IsNullOrEmpty(txtNoofIndividuals.Text)
&& String.IsNullOrEmpty(cboTypeofClients.Text)
&& String.IsNullOrEmpty(txtCompanyName.Text)
&& String.IsNullOrEmpty(txtYearsofExperience.Text);
Every term (term1 && term2 && ...) of the if statement's condition has to resolve to a boolean (true/false) value
You could solve it this way:
bool AttorneysData()
{
if (txtFirstName.Text != ""
&& txtSurname.Text != ""
&& txtAddress.Text != ""
&& txtTelNo.Text != ""
&& txtEmail.Text != ""
&& txtCreatePassword.Text != ""
&& txtConfirmPassword.Text != ""
&& cboDisciplineExpertise.Text != ""
&& cboGeographicalLocation.Text != ""
&& cboBudgetEstimation.Text != ""
&& txtNoofIndividuals.Text != ""
&& cboTypeofClients.Text != ""
&& txtCompanyName.Text != ""
&& txtYearsofExperience.Text != "")
{
return true;
}
else
{
return false;
}
}
Or in a slightly more elegant way:
return txtFirstName.Text != ""
&& txtSurname.Text != ""
&& txtAddress.Text != ""
&& txtTelNo.Text != ""
&& txtEmail.Text != ""
&& txtCreatePassword.Text != ""
&& txtConfirmPassword.Text != ""
&& cboDisciplineExpertise.Text != ""
&& cboGeographicalLocation.Text != ""
&& cboBudgetEstimation.Text != ""
&& txtNoofIndividuals.Text != ""
&& cboTypeofClients.Text != ""
&& txtCompanyName.Text != ""
&& txtYearsofExperience.Text != "";
Also, consider using the string.IsNullOrEmpty method which will test for spaces and other empty characters:
return !string.IsNullOrEmpty(txtSurname.Text)
&& !string.IsNullOrEmpty(txtAddress.Text)
...
I have an IF statement that is supposed to make sure the TextBox1.Text and TextBox2.Text do not match or are not blank. If they don't match or are not blank then it is supposed to assign the text in the boxes to two string variable. What I can't figure out is why when I leave the two textboxes blank the true statement still fires.
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text == "" && tbEndBreak2.Text == ""))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
There are two conditions in your if statement:
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text == "" && tbEndBreak2.Text == ""))
The first one checks to make sure they don't match (so, good). The second checks to make sure that they are blank (so, bad). You want this:
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != "" && tbEndBreak2.Text != ""))
Also, what are you trying to do? The second condition is the only one you need if you really want them not to match OR not be blank - because the only time this will be false is if they are both blank.
You wrote "OR textbox are blank", you need "OR textbox are not blank"
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != "" && tbEndBreak2.Text != ""))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
As a side note, I'd replace "" with string.Empty for readability.
if ((tbStartBreak2.Text != tbEndBreak2.Text) || (tbStartBreak2.Text != string.Empty && tbEndBreak2.Text != string.Empty))
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
And for even more readability, you can extract these big conditions
if (TextboxesDoNotMatch() || TextboxesAreNotEmpty())
{
sb2 = tbStartBreak2.Text;
se2 = tbStartBreak2.Text;
}
private bool TextboxesDoNotMatch()
{
return tbStartBreak2.Text != tbEndBreak2.Text;
}
private bool TextboxesAreNotEmpty()
{
return tbStartBreak2.Text != string.Empty && tbEndBreak2.Text != string.Empty;
}
If you want it to return that they are NOT blank then you need to do this
(tbStartBreak2.Text != "" && tbEndBreak2.Text != "")
you have an OR between both conditions so when both are empty the second part will be true regardless the first part
(tbStartBreak2.Text == "" && tbEndBreak2.Text == "")
I am still in the process of learning Asp.Net. I have a question . I was using an if condition in which i was checking dataset values. It is throwing me an exception whenever it is checking the condition as the dataset has not got any value. How to overcome this.
Code for this is here:
DataSet ds = merchant.getIsactiveIsconfirmedforcancelaccount(merchantID);
if (_merchant.PackageID == (int)CommonHelper.Package.Free && _merchant.AccountStatus.Contains("Awaiting"))
{
spnMerchantActiveStatus.InnerHtml = ApplicationData.MSG_AWAITING_CONFIRMATION;
}
***else if ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" && (ds.Tables[0].Rows[0]["Active"]).ToString() == "N")***
{
_merchant.AccountStatus = "Cancelled";
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
else if(_merchant.PackageID != (int)CommonHelper.Package.Free && ds1.Tables[0].Rows.Count == 0 && (ds2.Tables[0].Rows[0]["ConfirmationSrc"]).ToString() == "Admin")
{
_merchant.AccountStatus = "Free Trial";
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
else
spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
The exception here is "There is no row at position 0."
Seems like you do not have any rows in your tables[0]. You can add condition to check is rows > 0 and then continue with other conditions in the IF.
You are assuming you have rows in your DataSet.
Instead of
if ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
you should do something like
if ((ds.Tables[0].Rows.Count() > 0) &&
(ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
But you really need to do fuller error checking than just that one condition affectin you right now.
EDIT: If it's not obvious why the above works, read up on short-circuiting of logical operators: http://msdn.microsoft.com/en-us/library/2a723cdk(VS.71).aspx
Try changing your else statement :
else if (
(ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True" &&
(ds.Tables[0].Rows[0]["Active"]).ToString() == "N")
{
_merchant.AccountStatus = "Cancelled"; spnMerchantActiveStatus.InnerHtml = _merchant.AccountStatus;
}
To
if ((null != ds.Tables[0]) && ((ds.Tables[0].Rows[0]["IsConfirmed"]).ToString() == "True") && ((ds.Tables[0].Rows[0]["Active"]).ToString() == "N"))
So that you check that the data set is not null before you check conditions on it.
or (as other posters say) check that you actually have rows in the dataset.
ds.Tables[0].Rows.Count() != 0