Click here to Skip to main content
13,002,399 members (77,060 online)
Rate this:
Please Sign up or sign in to vote.
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.
public int getdayforstaffattandence1(DateTime date)
                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;
                if (Dob.con != null)

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

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 6-Oct-12 2:19am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That's because you haven't read any rows yet!
Try this:
if (dr.Read())
    timeid = Convert.ToInt32( dr[0].ToString());
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.

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 | Mobile
Web02 | 2.8.170626.1 | Last Updated 6 Oct 2012
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