Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
namespace BusinessLayer
{
   public class BusinessLogic
    {
       public IEnumerable<employee> Employees
       {
           get
           {
               string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
               List<employee> employees = new List<employee>();
               using (SqlConnection con = new SqlConnection(cs))
               {
                   SqlCommand cmd = new SqlCommand("SPGETEMPLOYEE", con);
                   cmd.CommandType = CommandType.StoredProcedure;
                   con.Open();
                   SqlDataReader dr = cmd.ExecuteReader();
                   while (dr.Read())
                   {
                       Employee emp = new Employee();
                       emp.Id = Convert.ToInt32(dr["Id"]);
                       emp.name = dr["Name"].ToString();
                       emp.gender = dr["Gender"].ToString();
                       emp.mobile = dr["Mobile"].ToString();
                       emp.doj = Convert.ToDateTime(dr["doj"]);
                       employees.Add(emp);
                   }


               }


               return Employees;
           }

       }
    }
}

getting the exception while reading the data from database. why can any one help me
Here
C#
emp.Id = Convert.ToInt32(dr["Id"]);

thanks in advance
Posted
Updated 8-Feb-15 22:33pm
v3
Comments
nagendrathecoder 9-Feb-15 4:44am    
Check executing SPGETEMPLOYEE stored procedure in SQL server mgmt studio and verify the result. Check is the result showing all columns you are trying to fetch properly or not.
raxhemanth 9-Feb-15 4:47am    
okey let me see
CREATE PROCEDURE [dbo].[SPGETEMPLOYEE]
AS
BEGIN
select EmpId,Name,Gender,MobileNumber,Dateofjoining,[Address] from tblEmployee
End
GO
this is the procedure i have written
nagendrathecoder 9-Feb-15 4:53am    
There you go.
You have EmpId field in SP and you are fetching Id field.
nagendrathecoder 9-Feb-15 5:05am    
Match all column names and use same column names as defined in stored procedure.

Use this:
C#
emp.Id = Convert.ToInt32(dr["EmpId"]);
 
Share this answer
 
Comments
raxhemanth 9-Feb-15 5:03am    
notworking same error tharowning
nagendrathecoder 9-Feb-15 5:05am    
Match all column names and use same column names as defined in stored procedure.
You need to match the names of your columns with the names you access they with from the DataReader:
SQL
select EmpId,Name,Gender,MobileNumber,Dateofjoining,[Address] from tblEmployee
Does not match the column names with the code:
C#
emp.Id = Convert.ToInt32(dr["Id"]);
emp.name = dr["Name"].ToString();
emp.gender = dr["Gender"].ToString();
emp.mobile = dr["Mobile"].ToString();
emp.doj = Convert.ToDateTime(dr["doj"]);
So try:
C#
emp.Id = Convert.ToInt32(dr["EmpId"]);
emp.name = dr["Name"].ToString();
emp.gender = dr["Gender"].ToString();
emp.mobile = dr["MobileNumber"].ToString();
emp.doj = Convert.ToDateTime(dr["DateofJoining"]);


And by preference, don't use Convert or ToString: cast it instead:
C#
emp.Id = (int) dr["EmpId"];
emp.name = (string) dr["Name"];
emp.gender = (string) dr["Gender"];
emp.mobile = (string) dr["MobileNumber"];
emp.doj = (DateTime) dr["DateofJoining"];

Using Convert and ToString implies that you have stored the values in your DB as strings instead of the "proper" datatypes - which would give you enormous problems later on.
 
Share this answer
 
Comments
raxhemanth 9-Feb-15 5:09am    
Thankyou so much
OriginalGriff 9-Feb-15 5:15am    
You're welcome!

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