Friday, February 24, 2012

Logic statement using select query

I'd like to make a logic statement, that would take as arguments result of the sql select query. In more details: I would like to create a local Bool variable that would be false if some value is NULL in the table (or select query).

Query example:

select taskID from Users where Login=@.username

Which classes/methods should i use to solve this problem? I use SqlDataSource to get access to database and i think i should use something like SqlDataSource.UpdateCommand and SqlDataSource.UpdateParameters but dont know how to build from this a logic statement.

Thanks in advance

If you are using the SqlDataSource, in its Selected event, you can check the RowsAffected property. If it is > 0, set your bool to true:

bool Check = False;

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
int RecordCount = e.AffectedRows;

if (RecordCount > 0)
{ Check = True; }
}


|||

Actually I tried to make it in the Page_Load(object sender, EventArgs e) event. Does it change anything?

I'm wondering if there is a way to get to sql query from C# code. Something like:

[for each row from select query]

bool present;

if ( [row.taskID] == null )

{

present = false;

}

|||You will not get rows containing null from the query you showed in your example. You will only get rows that contain the TaskID that belong to the user specified. If there are no TaskIDs for that user, you will get no rows at all.|||

I think i will get a row of some user even if he has NULL in taskID field. I checked this and i got the row that i wanted with empty value of the taskID field.

But even if there would be e.g. -1 the problem still exists and i have no idea how to solve it or even where to look for solution.

|||I beg your pardon. You are right. I didn't read your query correctly. You can access values from a datasource as described here:http://www.mikesdotnetting.com/Article.aspx?ArticleID=45, and check each value, setting your bool accordingly|||

Thaks again, it works fine now. You are gosu ;)

|||

DCVer:

You are gosu ;)

Is that a good thing?Confused

|||

Yes, you are master, the king etcSmile

No comments:

Post a Comment