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.