Help Needed

Help Needed

  • NA
  • 13
  • 22.9k

Connectionstring and Sqlreader

Jan 26 2014 8:17 AM
Hi
 
 I am trying to populate data from table using WCF.My connection string is "ABCD" in App.config file.
 
when I was debugging the application in "LOCALS" window I see the con value null.Basically it is not able to read the connectionstring. I keep preesing f10 and rdr is giving error "Index out of range exception" .It could be basically since it is not able to read connection string from app.config file.I added reference to configuaration file and also added using system.configuaration. Unless I get the solution I can not learn further. Please give me the solution ASAP.Here is my CODE:
 
 
Thanks
 
 public EmployeeWcf GetEmployee(int id)
        {
           EmployeeWcf employeeWcf = new EmployeeWcf();
           string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
            using(SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spGetEmployeeWcf",con);
               cmd.CommandType = CommandType.StoredProcedure;
               SqlParameter paramId = new SqlParameter();
               paramId.ParameterName = "@Id";
               paramId.Value = id;
                cmd.Parameters.Add(paramId);
                con.Open();
               SqlDataReader rdr = cmd.ExecuteReader();
                   while(rdr.Read())
                   {
                       employeeWcf.Id = Convert.ToInt32(rdr["Id"]);
                       employeeWcf.Name = rdr["Name"].ToString();
                       employeeWcf.Gender = rdr["Gender"].ToString();
                       employeeWcf.DateOfBirth = Convert.ToDateTime(rdr["DateOfBirth"]);
                   }
                   return employeeWcf;

Answers (2)