Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends
This is my c# code for getting workingdaytype. for example type 101 means half day, 100 means fullday for school project.
C#
public int getdayforstaffattandence1(DateTime date)
        {
            try
            {
                Dob.open();
                cm = new SqlCommand();
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "sp_getdayforstaffattandence";
                cm.Parameters.Add(new SqlParameter("@date", date));
                cm.Connection = Dob.con;
                dr = cm.ExecuteReader();
                int timeid=0;
                if (dr.HasRows)
                {

                    timeid =Convert.ToInt32( dr[0].ToString());
                }
                return timeid;
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                if (Dob.con != null)
                {
                    Dob.close();
                }
            }


And this is my stored procedure
SQL
alter procedure sp_getdayforstaffattandence(@date datetime)
as
set nocount on
begin
declare @dates date
set @dates=CONVERT(date,@date,111)
select Time_id from Workingday_details where Working_date=@dates
end


Actually when i run merely storedprocedure it return correct value with the given date. the date format has been copy from c# code. but run with c# code the error is
Invalid attempt to read when no data is present.

please solve this issue. thanks
Posted

1 solution

That's because you haven't read any rows yet!
Try this:
C#
if (dr.Read())
{
    timeid = Convert.ToInt32( dr[0].ToString());
}
 
Share this answer
 
Comments
FarazLoloei 7-Oct-12 4:07am    
Hi, when you want to convert one type to another and when u using convert you do not need to convert to string first.

timeid = convert.ToInt32(dr[0]);
OriginalGriff 7-Oct-12 4:19am    
Depends on the data type of the Time_id in the database which he doesn't specify. Since I don't know, it's safer to give him code which copies his (presumably successful) way of accessing it.
[no name] 7-Oct-12 10:01am    
exactly!
+5

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