Page view counter

class method select

Last post 08-29-2008 2:36 PM by dotnetkode. 1 replies.

Sort Posts:

  • class method select

    08-29-2008, 12:56 PM
    • Loading...
    • bthumber
    • Joined on 12-25-2006, 12:09 PM
    • Posts 333
    • Points 145

    I wrote a general class to insert, update and delete records in ado.net SQL db. I used the following method to select (populate my gridview) but a dictionary is not a good why to do what I need:

    private Dictionary<string,string> SelectAll(string ConnectString)

        {
         Dictionary<string,string> coolPeople = new Dictionary<string,string>();

          SqlConnection cn = new SqlConnection(ConnectString);

          cn.Open();

          SqlCommand cmd;


          string sql = “Select * From CoolPeople“;

          try

          {

            cmd = new SqlCommand(sql, cn);
        SqlDataReader reader = cmd.ExecuteReader();
         While(reader.Read())
        {
            coolPeople.Add(reader["firstname"].ToString(),reader["lastname"].ToString());
        }

        reader.Close();
          
          }

          catch (SqlException sqlexception)

          {

            MessageBox.Show(sqlexception.Message, “Oh Crap.”

              , MessageBoxButtons.OK, MessageBoxIcon.Error);

          }

          catch (Exception ex)

          {

            MessageBox.Show(ex.Message, “Oh Crap.”

              , MessageBoxButtons.OK, MessageBoxIcon.Error);

          }

          finally

          {

            cn.Close();

          }
    return coolPeople;

        }

    I need to display three columns, but the dictionary only return two.

  • Re: class method select

    08-29-2008, 2:36 PM
    Answer
    • Loading...
    • dotnetkode
    • Joined on 01-22-2008, 4:47 PM
    • Herndon, VA
    • Posts 113
    • Points 682

    Why don't you have class called Person with three properties.

    then have something like this.

    List<Person> coolPeople=new List<Person>();

     

    then in your while loop

    Person p=new Person();

    //set the person properties

     //add the person to the list

     

    return the list

     

     

     

    ~ Remember To Mark The Posts Which Helped You As The ANSWER ~
Page 1 of 1 (2 items)