Development Desk

Just another WordPress.com site

Monthly Archives: May 2012

Filter Collections (same as ‘Like’ operator in SQL) by using LINQ

A simple collection of objects can easily be filtered by using LINQ rather than traversing through whole collection one by one in loops with the help of complex coding.
Whenever we are using a Like operator in SQL query we perform usually three types of operations with it.

1.    Like ‘abc%’ //grab those values in particular column only starting with abc
2.    Like ‘%abc%’ //grab those values in particular column which contains abc
3.    Like ‘%abc’  //grab only those values from particular column ending with abc

Though System.Data.Linq.SqlClient.SqlMethods name space has a Like() method to filter the data but it only supports to LINQ to SQL, so it can’t be used here

Lets go step by step beginning with 1. Filter Start with ( Like ‘b%’ )

1

I had the BookInformation Table in the Database i fetch all data from the table and then Filter that data according to requirement. In above code you can see, from all collection it will only show the data which is Starting with ‘b’. I filtered the whole collection by using the StartsWith() Method

Now lets go for 2nd Option, 2. Filter Contains ( Like ‘%or%’ )

1

in above code example in above code fetched all data from the database table and then Filter that data on the basis of Books name having ‘or’ in their names by using Contains()  Method

Finally, 3. Filter Ends with ( Like ‘%k’ )

3

in above code fetched all data from the database table and then Filter that data on the basis of Books name ending ‘K’ by using the EndsWith() method

 

Note: Pleas make sure all these filter method StartsWith(), Contains() and EndsWith() are case sensitive mean if suppose the name of the Book is “Border Force” and you are trying to fetch data on the base of “b” it will return nothing, So you should “B” for that.
You may need to convert filterCollection object to List or any other collection to bind

I hope this post will be helpful for you any queries please let me know

Thank your

OnClick Event for Text Box in ASP.NET with C#

these 2 statement realy works no need to write in textbox field just write these two statements

1st one is code behind in PageLoad

TextBox1.Attributes[“onclick”] = “clearTextBox(this.id)”;

and 2nd is in aspx in header

function clearTextBox(textBoxID)
{
document.getElementById(textBoxID).value = “”;
}

thank you

Solution for Exception: System.InvalidOperationException: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

This exception occurs when we assign DataSource to some DataGrid or Calender and after that try to bind that DataSource with that Control.  Actually the  Problem is, Control like Gridview requires a collection to be bound with them instead of on single Object

suppose you have a Person class you fetch 1 single person from database like in below case person data is fetched by the method in the business Logic layer on the base of person id

Person singlePerson = new personBL().getSinglePerson(personId);

and then

personGridView.DataSource = singlePerson;

personGridView.DataBind(); // on this point exception will be thrown because single Instance of class is assigned to Gridview

it can be resolved, if you have only single object to assign you can simply add it to the list then assign to the DataSource like

 

Person singlePerson = new personBL().getSinglePerson(personId);

List <Person> personList = new List<Person>();

personList.Add(singlePerson );

personGridView.DataSource = personList;

personGridView.DataBind();

or

simply assign a collection instead of on single instance of object to resolve it

it just simple is that.

thank you

Convert String to GUID in C#

string can be converted to GUID just by

Guid stringToGuid = new Guid(“GUID value in the form of String”);

for example

Guid stringToGuid = new Guid(“91a61ee7-48bb-4371-ad33-72oI67c9b3e”); 

it will give same Guid in return but in the form of GUID

 

Solution for an Exception in WCF “HTTP could not register URL http://+:8000/EssentialWCF/. Your process does not have access rights to this namespace”

Hi,
All you just need to do is run your Visual Studio by an Administrator, by right click on visual studio and select “Run as Administrator”

Thank you