Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my database table is
SQL
create table tasktodo(Id int identity(1,1) not null, Done bit,Texts nvarchar(max),Dates date)


i wrote a code to get the data from database in c#

C#
public List<TaskToDoList> GetTaskToDo()
        {
            var Obj = DBHelper.GetDBObject();
            reader = Obj.ExecuteReader(CommandType.StoredProcedure, "GetTaskToDoList");
            var tasktodo = new List<TaskToDoList>();
            while(reader.Read())
            {
                tasktodo.Add(
                    new TaskToDoList
                    {
                        
                    Id =GetInteger("Id"),
                    Done=Convert.ToBoolean("Done"),
                    Text=GetString("Text"),
                    Date =reader["Date"] ==DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["Date"])            
                    });
            }
            return tasktodo;
        }


my model

C#
namespace SampleMVCApplication.Models
{
    public class TaskToDoList
    {

        public int Id { get; set; }

        public bool Done { get; set; }

        public string Text { get; set; }

        [DataType(DataType.Date)]
        public DateTime Date { get; set; }
    }
}


it is showing an error "String was not recognized as a valid Boolean"

how to solve this
Posted

1 solution

I got the answer

C#
public List<tasktodolist> GetTaskToDo()
      {
          var Obj = DBHelper.GetDBObject();
          reader = Obj.ExecuteReader(CommandType.StoredProcedure, "GetTaskToDoList");
          var tasktodo = new List<tasktodolist>();
          while(reader.Read())
          {
              tasktodo.Add(
                  new TaskToDoList
                  {

                  Id =GetInteger("Id"),
                  Done=Convert.ToBoolean( reader["Done"]),
                  Text=GetString("Texts"),
                  Date =reader["Dates"] ==DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["Dates"])
                  });
          }
          return tasktodo;
      }</tasktodolist></tasktodolist>
 
Share this answer
 
Comments
PIEBALDconsult 28-Jul-15 23:22pm    
Please don't answer your own question.
And please don't use Convert; you can just cast it
Done=(bool) reader["Done"]
I also wouldn't use the Getxxx methods; again, just cast.

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