0

I have a check button that will fetch the month and year in combo box:

private void cmdSend_Click(object sender, System.EventArgs e)
    {
        List<string>[] list;
        list = dbConnect.Select(month_list.SelectedItem.ToString(), year_list.SelectedItem.ToString());

        printer_info.Rows.Clear();
        for (int i = 0; i < list[0].Count; i++)
        {
            int number = printer_info.Rows.Add();
            printer_info.Rows[number].Cells[0].Value = list[0][i];
            printer_info.Rows[number].Cells[1].Value = list[1][i];
            printer_info.Rows[number].Cells[2].Value = list[2][i];
            printer_info.Rows[number].Cells[3].Value = list[3][i];
        }
    }  

enter image description here

The check button then pass the month and year to the select statement function:

public List<string>[] Select(string month,string year)
    {
        string query = "SELECT * FROM page_counter WHERE month = '@month' AND year = @year;";

        //Create a list to store the result
        List<string>[] list = new List<string>[4];
        list[0] = new List<string>();
        list[1] = new List<string>();
        list[2] = new List<string>();
        list[3] = new List<string>();

        //Open connection
        if (this.OpenConnection() == true)
        {
            //Create Command
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.Add("@month",MySqlDbType.VarChar);
            cmd.Parameters.Add("@year", MySqlDbType.Year);
            cmd.Parameters["@month"].Value = month;
            cmd.Parameters["@year"].Value = year;

            //Create a data reader and Execute the command
            MySqlDataReader dataReader = cmd.ExecuteReader();

            //Read the data and store them in the list
            while (dataReader.Read())
            {
                list[0].Add(dataReader["id"].ToString() + "");
                list[1].Add(dataReader["month"].ToString() + "");
                list[2].Add(dataReader["year"].ToString() + "");
                list[3].Add(dataReader["page_count"].ToString() + "");
            }

            //close Data Reader
            dataReader.Close();

            //close Connection
            this.CloseConnection();

            //return list to be displayed
            return list;
        }  

the data will then display on the gridview where all column are specified by default in page designer:
enter image description here

When I run the code, it doesnt have any error, but theres no value display on the gridview. Is there any mistake I make? Im newbie in c# winform,please advise.

2
  • As far as I can see, you don't set the DataSource of dataGridView.And why you are using a List<string>[] ? use a class with 4 properties instead. Commented Mar 11, 2014 at 0:35
  • @Selman22 Sorry, can you guide me please? Im new in c#. Commented Mar 11, 2014 at 0:36

1 Answer 1

1

I think you have two mistakes.First you should remove the single-quotes from query string:

string query = "SELECT * FROM page_counter WHERE month = @month AND year = @year;"

Because when you use single-quotes your parameter names treated as actual value.Secondly, I would highly recommend you to use a class for your item instead of a List<string>[].The class would look like this:

public class Data
{
    public int Id { get; set; }
    public string Month { get; set; }
    public string Year { get; set; }
    public int PageCount { get; set; }
}

Then create a List<Data> and populate it like this:

 var dataList = new List<Data>();
 while (dataReader.Read())
 {
    var item = new Data();
    item.Id = Convert.Toınt32(dataReader["id"]);
    item.Month = dataReader["month"].ToString();
    item.Year = dataReader["year"].ToString();
    item.PageCount = Convert.ToInt32(dataReader["page_count"]);
    dataList.Add(item);
}
return dataList;

Then ofcourse change the returning type of your method:

public List<Data> Select(string month,string year)

Then all you need to do is set the DataSource property:

var list = dbConnect.Select(month_list.SelectedItem, year_list.SelectedItem);
printer_info.DataSource = list;
Sign up to request clarification or add additional context in comments.

1 Comment

Where should I create the List<Data>? Is it in the public class Data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.