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
Related
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
I have function which calculates quantity .Its working fine but some due to null values in field it does not return anything . these field can be null .i want to calculate only where both fields are not null. i think i need to use foreach but i dont know the way how can i use it here .
Help appreciated
private double Get_Quantity()
{
try
{
double sum = 0;
sum = (double)(from s in ManagerClass.oSqlData.table
where s.Demand.Order.Order_ID == Order_ID
where s.Demand.Order.Order_Date >= ManagerClass.startDate && s.Demand.Order.Order_Date <= ManagerClass.EndDate
select s.ValueA - s.ValueB ).Sum();
return sum;
}
catch
{
return 0;
}
}
Try like this;
sum = (double)(from s in ManagerClass.oSqlData.table
where (s.Demand != null && s.Demand.Order != null && s.Demand.Order.Order_ID == Order_ID)
&& (s.Demand != null && s.Demand.Order != null && s.Demand.Order.Order_Date >= ManagerClass.startDate && s.Demand.Order.Order_Date <= ManagerClass.EndDate)
&& s.ValueA != null
&& s.ValueB != null
select s.ValueA - s.ValueB).DefaultIfEmpty(0).Sum();
Let's say I have a 5 textbox and 1 combobox and I make the value inputted pass to a variable like this
name = textboxName.Text.ToString();
address = txtboxAddress.Text.ToString();
destination = comboboxDestination.Text.ToString(); // combobox
position = txtboxPosition.Text.ToString();
station = txtboxStation.Text.ToString();
purpose = richtxtboxPurpose.Text.ToString();
Then I wanted to check if their value is null or just blank space.
int switchCase = 0;
if (string.IsNullOrWhiteSpace(name) && name.Length > 0 ||
string.IsNullOrWhiteSpace(address) && address.Length > 0 ||
string.IsNullOrWhiteSpace(destination) && destination.Length > 0 ||
string.IsNullOrWhiteSpace(position) && position.Length > 0 ||
string.IsNullOrWhiteSpace(station) && station.Length > 0 ||
string.IsNullOrWhiteSpace(purpose) && purpose.Length > 0
)
{
switchCase = 2;
}
else
{
switchCase = 1;
}
then for my trap is this
switch (switchCase)
{
case 1:
MessageBox.Show("Profile created!");
break;
case 2:
MessageBox.Show("Please complete the form to procede.");
break;
}
The problem is if I just load up the page and didnt typed anything it is going to the case 1 which means that all of my textboxes and combobox have a value. Then if I key in just space in all of the textboxes it reads the condition and go to case 2 what is going on?
EDIT:
My first code is like this and it is working well
if (name == "" || address == "" || destination == "" || position == "" || station == "" || purpose == "")
Although it is prone to the blank space.
Your code here:
string.IsNullOrWhiteSpace(name) && name.Length > 0 is checking if the string is empty, entirely whitespace, or null. Then you add a length check, effectively changing it into 'only accept strings which contain whitespace, but not blank.
Removing the length check should work as expected.
In addition, you can clean up the code a bit, to reduce repetitiveness:
if (new[] { name, address, destination, position, station, purpose }
.Any(str => string.IsNullOrEmpty(str)))
{
....
}
I have a list of YES/NO questions and each question has a radio button indicating the answer. When the user selects YES, a panel will be visible and it has textboxes inside it for the additional required input. When the user answers YES, they MUST fill in the textboxes that appear.
Currently I'm hard-coding it this way:
if (txtQ1Specify.Visible == true)
{
if (txtQ1Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ2Specify.Visible == true)
{
if (txtQ2Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ3Specify.Visible == true)
{
if (txtQ3Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ4SpecifyCompany.Visible == true || txtQ4SpecifyRelative.Visible == true)
{
if (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ5SpecifyCompany.Visible == true || txtQ5SpecifyRelative.Visible == true)
{
if (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ6Specify.Visible == true)
{
if (txtQ6Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ7Specify.Visible == true)
{
if (txtQ7Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
After this checking I want to execute some code.
The page looks like this:
How can I check for textbox inputs based in visibility?
You could use LINQ to find out if there are any visible and empty TextBoxes like so:
var query =
from t in Page.Controls.OfType<TextBox>()
where t.Visible && t.Text == ""
select t;
bool hasUnanswered = query.Any();
This can be easily done on the client side .
First you need to identify which all text box are visible . For this you can use jquery Visible Selector
Now If more than one text box is visible . Show some message to the user and focus and highlight the text box which need to be filled .
$(document).ready(function(){
var visibleCount =$('input[type="text"]:visible').length;
if (visibleCount > 0)
{
// Add your logic here
}
});
I managed to do it using a very long if statement. Here goes nothing:
if ((pnlQ1Yes.Visible == true && txtQ1Specify.Text.Length == 0) ||
(pnlQ2Yes.Visible == true && txtQ2Specify.Text.Length == 0) ||
(pnlQ3Yes.Visible == true && txtQ3Specify.Text.Length == 0) ||
(pnlQ4Yes.Visible == true && (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)) ||
(pnlQ5Yes.Visible == true && (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)) ||
(pnlQ6Yes.Visible == true && txtQ6Specify.Text.Length == 0) ||
(pnlQ7Yes.Visible == true && txtQ7Specify.Text.Length == 0))
{
lblError.Text = "Please answer all questions.";
}
else
{
...
}
It first checks if the panel is visible, then it checks the textbox inside the panel for values. Then I repeat this kind of checking for the rest of the panels and textboxes.
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 == "")