1

I have a projest has Database And Store Variables.I want to store all values and columns in database and access again , but when i try to access im giving only last value in database.How can i fix this ?

public class Variables
{
    public static readonly List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
    public static Dictionary<string, string> column;
    public static String Name = null;
    public static String Surname = null;
    public static String Gsm = null;
    public static String QRCodeID = null;
    public static String Email = null;
    public static String indirimOrani = null;

}

public class SelectSave()
{
    var cmdSelectSave = new SqlCommand
    {
        CommandText =
            "SELECT Name,Surname,Gsm,QRCodeID,Email,indirimOrani FROM elmacustomers WHERE OnayID=1",
        CommandType = CommandType.Text,
        Connection = sqlConElmaCafePlus
    };

    SqlDataReader reader = null;
    reader = cmdSelectSave.ExecuteReader();
    while (reader.Read())
    {
        Variables.column = new Dictionary<string, string>
        {
            ["Name"] = reader["Name"].ToString(),
            ["Surname"] = reader["Surname"].ToString(),
            ["Gsm"] = reader["Gsm"].ToString(),
            ["QRCodeID"] = reader["QRCodeID"].ToString(),
            ["Email"] = reader["Email"].ToString(),
            ["indirimOrani"] = reader["indirimOrani"].ToString()
        };
        Variables.rows.Add(Variables.column);
    }
}

public void DataReceive()
{
    foreach (Dictionary<string, string> column in Variables.rows)
    {
        MessageBox.Show(Variables.column["Name"]);//That's giving only last Name value in database. I want to scan all names in database.
    }
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Ozan Manav
  • 429
  • 4
  • 18
  • 5
    can you confirm more than one rows are being returned from database? I'm skeptical about this condition: `WHERE OnayID=1` – Mridul Kashyap Aug 16 '16 at 07:36
  • 1
    When You debug this and but a break point at the end of SelectSave, does Variables.rows have more columns than one? – Whencesoever Aug 16 '16 at 07:36
  • Yes , when i insert a breakpoint every value stored in Variables.rows but how can i access this discrete? Photo Here : http://i.hizliresim.com/mErk4y.jpg – Ozan Manav Aug 16 '16 at 08:22

1 Answers1

1

In the DataReceive() function, while iterating over the rows use some different name for the iterator like datarow and then use this:

foreach (Dictionary<string, string> column in Variables.rows.ToList())
{
    MessageBox.Show(column["Name"]);
}
Richa Garg
  • 1,888
  • 12
  • 23