Showing posts with label software testing. Show all posts
Showing posts with label software testing. Show all posts

LINQ Single and SingleOrDefault



Single means you are expecting only one result from LINQ query. If you get more than one or zero matches, then it throws exception.

SingleOrDefault means if you get single result, it returns that result but if it doesn't find any matches it returns default value but you will not get any exception.
Read more...

Return only one result from LINQ Query

You can do it in two ways, either use First or use Single.

First returns only first row, even there are multiple rows

Single expects only one row to be returned, and if there are multiple rows, it throws exception.

So, use Single if you are expecting only one row to be returned.

EX: var book= from book in store where (book.price > 100) select book).First();
Generally , the query returns all books having price greater than 100. but , as we are using  " .First() " only first book will be returned.


Read more...

return more than one value from a method


We can't return more than one values from a method.

An alternative way would be, add all those values to be returned into  Array and return that array.

Ex: public int[] returnArray ()
{
int[] myArray=new int[3];
myArray[0] = value1;
myArray[1] = value2;
myArray[2] = value3;

      return myArray;
}

Read more...

Collection was modified; enumeration operation may not execute c#


You will get this error when you are trying to remove list item in foreach loop.

either you have to use removeAll or removeAt.

It's good to iterate backward by index. like below,

for(int i = list.Count - 1; i >= 0; i--)
{
 if({some condition})
    list.RemoveAt(i);
}

Read more...

Sending email with attachments from C#


In many cases you may required to send emails using ASP.NET. You can have all the classes required to send an email in the System.Net.Mail namespace. You can send an email from ASP.NET as follows :

First you have to add these namespaces to your code behind file:

1. using System.Net;
2. using System.Net.Mail;

Then write the following method

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your [email protected]");
    mail.To.Add("[email protected]");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}

Using the above you can send attachemebts also. Hope this helps :-)
Read more...

No connection could be made because the target machine actively refused it 127.0.0.1:4444

I had this issue and it took a lot of time to find the solution for this. I searched many sites for the solutions. After few hours, i have found the solution for this issue. (This is the solution which worked for me . I hope this will work for others also.)

The problem is i din't start the selenium server. After starting the server, issue gone and all worked fine.

You can download selenium server here. Download Selenium server

After downloading start the selenium server. See screen shot below


Read more...