0

I have a windows form chart that retrieve the x value in mysql database and plot it on a bar graph. This is the the first time I'm doing this so I don't really have an idea why I am getting this error.

public void loadChart()
    {
        string conn = "server=localhost;user=root;password='';database=cashieringdb;";
        string cmdstring = "SELECT sum FROM salessum";
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmdstring, conn);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        chart1.DataSource = dt;
        this.chart1.Palette = ChartColorPalette.SeaGreen;
        this.chart1.Titles.Add("Daily Record");
        int[] pointsArray = a.mysqlSelect("select sum from salessum"); // error in this line
        string[] seriesArray = { "Mon", "Tue", "Wed", "Th", "Fri" };

        for (int i = 0; i < seriesArray.Length; i++)
        {
            Series series =this.chart1.Series.Add(seriesArray[i]);
            series.Points.Add(pointsArray[i]);
        }

Here's the error shows: No matter how much time searching for solution on the internet, but still I ca'nt get this working. Or is there any easy way to retrieve data from database and plot it on the windows form chart?

Error   2   Cannot implicitly convert type 'System.Collections.Generic.List<string>[]' to 'int[]'

EDIT

I change my code to this:

 List<string>[] pointsArray = a.mysqlSelect("select sum from salessum"); //no error

Error in this line:

 series.Points.Add(pointsArray[i]); 
user2262382
  • 335
  • 2
  • 5
  • 13
  • Where u are getting this exception in code – Rajeev Kumar Apr 12 '13 at 06:49
  • look at the code I post thre is a comment line there – user2262382 Apr 12 '13 at 06:50
  • 1
    Well, what *is* `a.mysqlSelect("select sum from salessum")` really? Because as the error suggests, it clearly isn't an `int[]`. Try `var obj = a.mysqlSelect("select sum from salessum");` and see of what type `obj` is. And maybe you can find a way to get an `int[]` from whatever `obj` is. – Corak Apr 12 '13 at 06:50
  • Does `chart1.Series.Add(...)` take a `string[]` ? shouldn't it be a collection of points/koordinates in the chart? – Jens Kloster Apr 12 '13 at 06:50
  • @Corak yes it is a string – user2262382 Apr 12 '13 at 06:52
  • @Jens Kloster the value from database is a varchar not an int but I thought that varcahr holds both string and int values isn't it? – user2262382 Apr 12 '13 at 06:53
  • 1
    Yes it can hold both string and int value but int [] can't in c# – Rajeev Kumar Apr 12 '13 at 06:54
  • @user2262382 well there you have it. There is no implicit cast from `string` to `int[]` so you have to make one yourself. – Corak Apr 12 '13 at 06:54
  • a.mysqlSelect("select sum from salessum") returns List or List[] ? – Özgür Kaplan Apr 12 '13 at 06:55
  • but all of my values from my database column sum is just pure numbers and never be inputed a letters. can I just change its type to int? – user2262382 Apr 12 '13 at 06:57
  • @RajeevKumar - Varchar can hold int? As an integer? Are you sure about that? – Tim Apr 12 '13 at 06:57
  • If you only ever store `int`s in that field of your table then yes, by all means change that fieldtype to `INTEGER`. – Corak Apr 12 '13 at 06:59
  • I will try changing it and I will let you know if that will work. – user2262382 Apr 12 '13 at 07:02
  • Btw. if `salessum` is any indication, you will probably want to work with numbers with decimal places (if not now, than maybe at some point in the future). So you might want to consider using `DECIMAL`. (For financial stuff, stay far away from `FLOAT` or `DOUBLE`.) – Corak Apr 12 '13 at 07:10

1 Answers1

0

This error occurs because you are converting a List to int[]. You can not implicitly do this, you have to do this explicitly. you can do this easily using Linq.

I assume that a.mysqlSelect("select sum from salessum"); is a method call. and out put a list of strings(each string representing a number)

Something Like this:

int[] pointsArray = a.mysqlSelect("select sum from salessum").Select(i => int.Parse(i)).ToArray();

This link will be helpful to you as well.

Edited: Take a look at the LINK I have added earlier and get an idea on what you are going to accomplish. :)

in this code,

series.Points.Add(pointsArray[i]);

Points.Add method do not expect a string it expects a int. What you have tried to do earlier is correct. So point Array has to be a int[]. I mean this code:

int[] pointsArray = a.mysqlSelect("select sum from salessum");

I think I have found the exact same thing that you want to accomplish. Please take a look at this LINK. This will be helpful to you.

Community
  • 1
  • 1
diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • if you look at the error message ' a.mysqlSelect("select sum from salessum") ' is a List[] not a List so your code won't work. – Özgür Kaplan Apr 12 '13 at 07:04
  • Hi I try your code but I have an error in this line `int.Parse(i))` – user2262382 Apr 12 '13 at 07:16
  • no it is different and I'm browsing your given link, thanks to that anyway. – user2262382 Apr 12 '13 at 07:29
  • @Diode yes at the link you post that is what exactly I am trying to do the difference is just the values from pintsArray are retrieve from database which is I am now trying to fix. – user2262382 Apr 12 '13 at 07:50
  • @Diode please see this http://i495.photobucket.com/albums/rr312/flame25dragon/Untitled_zpsac8bb86e.png I am the one who made this question I already have an output but only show different result. – user2262382 Apr 12 '13 at 07:52