Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I just need another help based on my last question

but the problem is little different
C#
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
C#
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
Updated 18-Aug-13 23:37pm
v3
Comments
Jameel VM 19-Aug-13 5:37am    
Please run the query in sql server directly and check whether it's return any records.

C#
if (dataTableuser.Rows.Count <= 0)
   {
   // there are no rows to play with...
   }
 
Share this answer
 
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.
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900