Click here to Skip to main content
13,447,603 members (51,201 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello I just need another help based on my last question

but the problem is little different
SqlCommand commanduser = new SqlCommand("SELECT * from Users  WHERE Username = @Username", cs);
            commanduser.Parameters.Add("@Username", SqlDbType.NVarChar);
            commanduser.Parameters["@Username"].Value = username.Text;
            SqlDataAdapter dataAdapteruser = new SqlDataAdapter(commanduser);
            DataTable dataTableuser = new DataTable();
            userid.username = (String)dataTableuser.Rows[0]["Username"];

on this bolded line I get There is no row at position 0,

since userid comes from the class which I made like this
public class User
        public  User()
        private string username;

        public string Username

                return username;
            set { username = value; }

I am guessing I have to write some code in case I have no value to return, but hehe don't know how
Posted 18-Aug-13 23:33pm
Updated 18-Aug-13 23:37pm
Jameel M 19-Aug-13 5:37am
Please run the query in sql server directly and check whether it's return any records.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

if (dataTableuser.Rows.Count <= 0)
   // there are no rows to play with...
shonezi 19-Aug-13 5:40am
THANK YOU GRIFF!!! just to ask, is there a possibility to place if question in get set property of the class that I have of the username, just wondering
OriginalGriff 19-Aug-13 6:03am
No - the User class does not know where it is being used: it has no access to the caller variables!

That's a bit like asking "Can my phone please talk to my mother because I don't have an hour to spare" :laugh:
CodeBlack 19-Aug-13 6:04am
instead of you can use
// Do your work

this will reduce the compiler execution time. becase for "Count" compiler will check for all the rows and get the count and for "Any" compiler will check for the first row only and return true or false based on that.
shonezi 19-Aug-13 6:13am
It says Rows don't hold definiton for action Any
shonezi 19-Aug-13 6:14am
:) guess I asked to much ::) :) :) thanks Griff
CodeBlack 19-Aug-13 6:19am
ohhh i forgot that you are using DataTable.
OriginalGriff 19-Aug-13 6:22am
Are you sure?
Have you tried?
Did you know that Any is a Linq method, and has an appreciable overhead?
Try it:
List<string> s = new List<string>();
for (int i = 0; i < 100000; i++)
Stopwatch s1 = new Stopwatch();
int x1 = 0;
for (int x = 0; x < 1000000; x++)
if (s.Count > 0)
Stopwatch s2 = new Stopwatch();
int x2 = 0;
for (int x = 0; x < 1000000; x++)
if (s.Any())
Console.WriteLine("{0}:{1}", s1.ElapsedMilliseconds, s2.ElapsedMilliseconds);
You will find that the Any method is about 10 times slower - probably because an IEnumerable maintains an items count with it adds and subtracts when you call it's methods...
OriginalGriff 19-Aug-13 6:25am
You're welcome!
CodeBlack 19-Aug-13 6:37am
Nice example OriginalGriff. That makes me more clear and improve my knowledge where i was wrong for ANY and COUNT. Thanks for the explanation. :)
OriginalGriff 19-Aug-13 6:42am
You're welcome!
shonezi 19-Aug-13 6:42am
since I am amateur, I will just say thank you both :) :) :) :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

no you do not need to write more code.
its clearing saying that your datatable has no row means you have no record into datatable.

check the query in sql first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180318.3 | Last Updated 19 Aug 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100