Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
i have a store proc in sql server 2008
SQL
ALTER PROCEDURE dbo.Number	
AS
	select count(*) as total from site
	RETURN

it works fine, but when i want to read the total amount in SqlDataReader in bump into an errro,here it is my code in c#:
C#
SqlCommand storedProcCommand = new SqlCommand("Number", con);
storedProcCommand.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = storedProcCommand.ExecuteReader();
if (dr.HasRows)
    {
      object[] values = new object[dr.FieldCount];
      Int32 ob = dr.GetInt32(0);
     ob = dr.GetInt32(0);
               }


and the error says:

Invalid attempt to read when no data is present.

what's wrong with my code?and can you give me some links so i can't learn more about sql programming?like learning how to call a store proc. in another store proc,and conditions and loops in sql etc.

Thanks
Posted
Comments
[no name] 19-Sep-12 8:25am    
You need to call Read() on your reader before you can read anything.
Arash Khangaldi 19-Sep-12 8:30am    
tanx
[no name] 19-Sep-12 8:28am    
Waht's the point of "RETURN" at the end of your procedure?
Arash Khangaldi 19-Sep-12 8:31am    
that's pre-added sql coded by sql server,that's why i asked for some links to understand and read more about sql and programming in sql,i'm new to sql

change your code by tis way:

C#
if (dr.Read())
{
    object[] values = new object[dr.FieldCount];
    Int32 ob = dr.GetInt32(0);
    ob = dr.GetInt32(0)
}
 
Share this answer
 
Comments
Arash Khangaldi 19-Sep-12 8:30am    
ممنون عزیز
[no name] 19-Sep-12 8:32am    
You're welcome!
C#
if (dr.HasRows)
{
    while (dr.Read())
    {
        object[] values = new object[dr.FieldCount];
       Int32 ob = dr.GetInt32(0);
      ob = dr.GetInt32(0);
    }
}
dr.Close();


Try in this way
 
Share this answer
 
v3

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