return a datafield from a database [Resolved]

Posted by Jopito under ASP.NET on 12/4/2013 | Points: 10 | Views : 1309 | Status : [Member] | Replies : 31
Hi pals,have got a method to return a field "country" from a sql database.The method doesnt return the lists from the database and am stuck how to figure it out,
Here is my method am using

public Nationality Getcountries(string countries)
{
Nationality country = db.Countries.SingleOrDefault(xz => xz.countries == countries);
return country;
}
where am i getting lost in this method?#Thanks

Mark as answer if satisfied


Responses

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
May be you are asking for LIKE search?

public string GetNationalities(string nationals)
{
var query = (from countries in db.Registrations
where countries.Nationalities.Contains(nationals)
select countries.Nationalities).FirstOrDefault();
string returnValue = Convert.ToString(query);
return returnValue;
}

NOTE: use Contains(), StartsWith(), or EndsWith() to mimick the LIKE operator of SQL Server.

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
1). Please check whether your db.Countries have any records or not?
2). Are you getting any error in your code. Please check it too.


Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You can change your code as

IQueryable<Nationality> countryquery =
from country in db.Countries
where country.countries == countries
select country.SingleOrDefault();

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Here what is the type of Nationality ?


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
Nationality is a string datatype


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 12/4/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Put debug point and check whether ur getting records from db or not.
Try with FirstOrDefaukt instead of SingleOrDefault


Mark as answer if satisfied....

Regards,
Shree M.


Kavya Shree Mandapalli

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
am getting the error
Operator '==' cannot be applied to operands of type 'string' and 'System.Linq.IQueryable' after using the code
public Nationality GetNationality()
{
IQueryable<Nationality> countryquery =
from country in db.Countries
where country.countries == countries
select country.FirstOrDefault()

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public Nationality Getcountries(string countries) 
{
Nationality country = db.Countries.FirstOrDefault(xz => xz.countries == countries).ToString();
return country;
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public Nationality GetNationality()
{
IQueryable<Nationality> countryquery = (
from country in db.Countries
where country.countries == countries
select country)
.FirstOrDefault()

retrun countryquery
}

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 12/4/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Try with the following code:

Nationality country = (from country in db.Countries
where country.countries == countries
select country.FirstOrDault).ToString();

Mark as answer if satisfied....


Regards,
Shree M.

Kavya Shree Mandapalli

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 12/4/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Nationality country = (from country in db.Countries
where country.countries == countries
select country).ToString();


Mark as answer if satisfied....

Regards,
Shree M.

Kavya Shree Mandapalli

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public Nationality Getcountries(string countries)  
{
var cnty = (from country in db.Countries
where country.countries == countries
select country).FirstOrDefault();
string returnValue = Convert.ToString(cnty);
return returnValue ;
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 12/4/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Forgot to tell you,
You cannot perform direct conversion from IQuerable to string.
You first need to assign it to a variable and then perform conversion.

var result = (...);
string value = Convert.ToString(result);
return value.


Mark as answer if satisfied.....


Regards,
Shree M.

Kavya Shree Mandapalli

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,am using this code but it cannot return anything and i got record in my databse
public Nationality Getcountries(string countries)
{
var cnty = (from country in db.Countries
where country.countries == countries
select country).FirstOrDefault();
string returnValue = Convert.ToString(cnty);
return returnValue ;


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
The table name is Registrations and has a column called Nationality.I dont want to return the whole contents but only the column Nationality in my method .I hope this makes ya get my intention pals


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
what is the value in returnValue ?
Are you getting value to returnValue ?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down

Hi,am using this code but it cannot return anything and i got record in my databse
public string  Getcountries(string countries) 

{
var cnty = (from country in db.Countries
where country.countries == countries
select country.Nationality ).FirstOrDefault();
string returnValue = Convert.ToString(cnty);
return returnValue ;

}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
The value is cnty as per the method what av used really

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
What are columns available in Registrations table?
public string  Getcountries(string countries)  
{
var cnty = (from country in db.Registrations

where country.countries == countries

select country.Nationality ).FirstOrDefault();

string returnValue = Convert.ToString(cnty);
return returnValue ;
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
The columns are Username,Age,Password,Email and Nationality in my registrations table

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Have you checked above solution?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public string  Getcountries(string usrName)   
{
var cnty = (from country in db.Registrations

where country.UserName == usrName
......


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,have tried all this solutions but cannot solve please

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The columns are Username,Age,Password,Email and Nationality in my registrations table

public string  Getcountries(string countries)   
{
var cnty = (from country in db.Registrations

where country.countries == countries // Place your condition

select country.Nationality ).FirstOrDefault();

string returnValue = Convert.ToString(cnty);
return returnValue ;
}


what is the problem with this code?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
can the "List"method accomplish this?

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Am not getting what is the problem with above code...

Support link
http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx
http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
It works using the method
public List<Registrations> GetNationalities(string nationals)
{

IList<Registration>getnationalities = db.Registrations.Where(nationals =>nationals.Nationalities!=nationals).Distinct().ToList();

The issue is that it repeats the list of nationalities,thought i could use "distinct" for it to filter the list being returned it somehow....??
Thanks
return getSources.ToList();

}


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
You are checking for NOT EQUAL to condition... .
Where(nationals =>nationals.Nationalities != nationals) ?

Is it intentional or typo?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
i checked that because it would mean in my previous code above that i would pass the value as a query string of type,you notice in my code this part ((public string Getcountries(string countries))) would mean that i enter the value to get ,i,e (countries) in my code,after i checked that it brought all the items since in the code behind i passed a value which is not in the database

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Means that you want to get the all records which doesn't satisfy the given nationality?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
yeah,in my case,have onlyb predefined 5 records for nationalities,The five nationalities are the one s to appear in the dropdown,its jus a trial which i tried to check if it will work out

Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response