Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Feature Forums
  3. - Uncategorised posts -
  4. .net

.net

Scheduled Pinned Locked Moved - Uncategorised posts -
csharp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11409298
    wrote on last edited by
    #1

    while (rdr.Read())
    {
    // emp = new EmployeeBll(rdr["ID"].ToString(), rdr["Name"].ToString(), rdr["Gender"].ToString(), rdr["Email"].ToString(), rdr["Roles"].ToString());

                  emp.ID = rdr\["ID"\].ToString();
                  emp.Name = rdr\["Name"\].ToString();
                  emp.Gender = rdr\["Gender"\].ToString();
                  emp.Email = rdr\["Email"\].ToString();
                  emp.Roles = rdr\["Roles"\].ToString();
    
    
    
              }
    

    hear I am passing rdr["ID"].ToString()..... line by line i want this to be executed in a single line is it posible Thanks Gopi Krishna

    Richard DeemingR 1 Reply Last reply
    0
    • U User 11409298

      while (rdr.Read())
      {
      // emp = new EmployeeBll(rdr["ID"].ToString(), rdr["Name"].ToString(), rdr["Gender"].ToString(), rdr["Email"].ToString(), rdr["Roles"].ToString());

                    emp.ID = rdr\["ID"\].ToString();
                    emp.Name = rdr\["Name"\].ToString();
                    emp.Gender = rdr\["Gender"\].ToString();
                    emp.Email = rdr\["Email"\].ToString();
                    emp.Roles = rdr\["Roles"\].ToString();
      
      
      
                }
      

      hear I am passing rdr["ID"].ToString()..... line by line i want this to be executed in a single line is it posible Thanks Gopi Krishna

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      If you have a constructor which accepts the property values, then your commented-out line will work. If you don't, an object initializer might help:

      emp = new EmployeeBLL
      {
      ID = Convert.ToString(rdr["ID"]),
      Name = Convert.ToString(rdr["Name"]),
      Gender = Convert.ToString(rdr["Gender"]),
      Email = Convert.ToString(rdr["Email"]),
      Roles = Convert.ToString(rdr["Roles"]),
      };

      You might also want to look at Dapper[^]:

      using (var connection = new SqlConnection("..."))
      {
      List<EmployeeBLL> employees = connection.Query<EmployeeBLL>(
      "SELECT ID, Name, Gender, Email, Roles FROM YourTable WHERE SomeCondition = @SomeParameter",
      new { SomeParameter = "some value" })
      .ToList();
      ...
      }

      Or AutoMapper[^] with the AutoMapper.Data[^] extension:

      using (var connection = new SqlConnection("..."))
      using (var command = new SqlCommand("SELECT ID, Name, Gender, Email, Roles FROM YourTable WHERE SomeCondition = @SomeParameter", connection))
      {
      command.Parameters.AddWithValue("@SomeParameter", "some value");

      connection.Open();
      using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
      {
          if (reader.HasRows)
          {
              List<EmployeeBLL> employees = Mapper.DynamicMap<IDataReader, List<EmployeeBLL>>(reader);
              ...
          }
      }
      

      }

      (Code based on this StackOverflow answer[^].)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups