Accessing SQL data in a Chart Object Windows Forms VS 2012 C#

Hello Experts,

I am trying to populate a Chart with SQL Data and cannot get it to work. I am using Visual Studio 2012 with C# and SQL Server 2008.

Below is the code on my Windows form.

SqlConnection con = new SqlConnection("server= Localhost;uid=sa;pwd=12345;database=MyDB;");
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", con);
SqlDataReader mydatareader;
try
{
con.Open();
      mydatareader = cmd.ExecuteReader();
      while (mydatareader.Read())
            {
this.chart1.Series["Lot"].Points.AddXY(mydatareader.GetInt32("Lot"), mydatareader.GetDouble("Weight"));
                              
            }
}//end try
catch {}

The build messages I get are:

Error      1      The best overloaded method match for 'System.Data.Common.DbDataReader.GetInt32(int)' has some invalid arguments      D:\DataHarvestSystems\EHarvest 2016\OPI_Quota_Project\APP\EHarvestOPI_2015\EHarvestOPI_2015\ChartsForm_SF_Harvest.cs      65      49      EHarvestOPI_2015

Error      2      Argument 1: cannot convert from 'string' to 'int'      D:\DataHarvestSystems\EHarvest 2016\OPI_Quota_Project\APP\EHarvestOPI_2015\EHarvestOPI_2015\ChartsForm_SF_Harvest.cs      65      71      EHarvestOPI_2015

Error      3      The best overloaded method match for 'System.Data.Common.DbDataReader.GetDouble(int)' has some invalid arguments      D:\DataHarvestSystems\EHarvest 2016\OPI_Quota_Project\APP\EHarvestOPI_2015\EHarvestOPI_2015\ChartsForm_SF_Harvest.cs      65      86      EHarvestOPI_2015

Error      4      Argument 1: cannot convert from 'string' to 'int'      D:\DataHarvestSystems\EHarvest 2016\OPI_Quota_Project\APP\EHarvestOPI_2015\EHarvestOPI_2015\ChartsForm_SF_Harvest.cs      65      109      EHarvestOPI_2015


Can anyone see what is going on here?

Thanks!
SaxitalisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
The Get() methods all take an ordinal, rather than a column name.
If you want to use the column name you would need to use a nested call to GetOrdinal() to get the ID from the name, like:
mydatareader.GetInt32(mydatareader.GetOrdinal("Lot"))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SaxitalisAuthor Commented:
That was it - Thank you sir
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.