Click here to Skip to main content
13,192,207 members (36,597 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();
 
            dataAdapteruser.Fill(dataTableuser);
 
            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
        {
            get
            {
 
                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
Code-Hunt7.1K
v3
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 1

if (dataTableuser.Rows.Count <= 0)
   {
   // there are no rows to play with...
   }
  Permalink  
Comments
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:
shonezi 19-Aug-13 6:14am
   
:) guess I asked to much ::) :) :) thanks Griff
OriginalGriff 19-Aug-13 6:25am
   
You're welcome!
CodeBlack 19-Aug-13 6:04am
   
instead of you can use
if(dataTableuser.Rows.Any())
{
// 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
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++)
{
s.Add("Hello");
}
Stopwatch s1 = new Stopwatch();
int x1 = 0;
s1.Start();
for (int x = 0; x < 1000000; x++)
{
if (s.Count > 0)
{
x1++;
}
}
s1.Stop();
Stopwatch s2 = new Stopwatch();
int x2 = 0;
s2.Start();
for (int x = 0; x < 1000000; x++)
{
if (s.Any())
{
x2++;
}
}
s2.Stop();
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...
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
 
good
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.
  Permalink  
v2

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.171017.2 | Last Updated 19 Aug 2013
Copyright © CodeProject, 1999-2017
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