Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a query which brings picture from database based on id which looks like this

var selectphotos = "Select * from ItemPhotos where ItemID= @0";

I want to hide image div if that query has 0 results or no pictures. I tried

if(selectphotos.Count()  > 0 ){
<div> with pics </div>
}else{
<p>just msg </p>
}

It didn't work pls help

share|improve this question
2  
"It didn't work" is not an error. What do you expect to happen and what does happen? – CodeCaster 53 mins ago

1 Answer

up vote 2 down vote accepted

selectphotos is a string in what you have shown containing the SQL query. You should do the test with Count() > 0 on the results of this query:

if (resultsOfYourSQLQuery.Count() > 0) {
    <div> with pics </div>
} else {
    <p>just msg </p>
}

If you are using WebMatrix you could execute the query like that:

@{
    var db = Database.Open("YOUR_CONNECTION_STRING_NAME");
    var sql = "SELECT * FROM ItemPhotos WHERE ItemID=@0";
    int itemId = 123; // you should probably fetch this from the request or something
    var results = db.Query(sql, itemId);
}

if (results.Count() > 0) {
    <div> with pics </div>
} else {
    <p>just msg </p>
}
share|improve this answer
Or rather, Any(). – CodeCaster 52 mins ago
could you pls give me rought syntax for that ? – ktm 52 mins ago
2  
Rough syntax for what? Executing a SQL query? There are gazillions of ways and syntaxes to execute a SQL query in .NET. Depending on your specific scenario, the data access engine you are using and whether you are using an ORM or not there might be so many different ways. – Darin Dimitrov 51 mins ago
1  
Rough syntax for how to do a basic query? Any beginners tutorial should cover that. – SwissCoder 49 mins ago
Thanks for the solution – ktm 41 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.