SQL Server Express and C# : Database Charting
Asked By: mcoliver88
Originally Asked On: 2014-01-02 11:27:07
Asked Via: stackoverflow
I am trying to link my SQL Server Express database to a chart in a c# application. I am trying to extract the age of some employees to display them in a bar char. Here is my code
string constring = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Michael\Downloads\Employee\Employee\Employee\EmployeeDetails.mdf;Integrated Security=True;User Instance=True;Initial Catalog=Employee"; SqlConnection conDatabase = new SqlConnection(constring); SqlCommand cmdDatabase = new SqlCommand("Select * from Employee;", conDatabase); SqlDataReader myReader; try{ conDatabase.Open(); myReader=cmdDatabase.ExecuteReader(); while(myReader.Read()) { this.chart1.Series["Age"].Points.AddXY(myReader.GetString("FirstName"), myReader.GetInt32("Age")); } }
When I compile my program, I get 4 error messages, which relate to this line:
(myReader.GetString("FirstName"), myReader.GetInt32("Age"))
The error messages are:
The best overloaded method match for ‘System.Data.Common.DbDataReader.GetString(int)’ has some invalid arguments
Argument 1: cannot convert from ‘string’ to ‘int’
The best overloaded method match for ‘System.Data.Common.DbDataReader.GetInt32(int)’ has some invalid arguments
Argument 1: cannot convert from ‘string’ to ‘int’The SQL Server Express database uses
VARCHAR
forFirstName
andINT
forAge
what have i done wrong?
He received 4 answers
eventually accepting:
StuartLC’s answer to
SQL Server Express and C# : Database Charting
IDataReader
‘s.Get*
methods only take the index of the column to be accessed, not the column name.So instead of
myReader.GetString("FirstName")
You’ll need something like
myReader["FirstName"].ToString()
and
Convert.ToInt32(myReader.GetInt32("Age")))
If you are doing this in a tight loop, then you can cache the name : index mappings.
The answer with the highest score with 2 points was:
Soner Gönül’s answer to
SQL Server Express and C# : Database Charting
Error 1 The best overloaded method match for
‘System.Data.Common.DbDataReader.GetString(int)’ has some invalid
arguments
DbDataReader.GetString
method takes argument as anint
, notstring
.Gets the value of the specified column as an instance of String.
public abstract string GetString( int ordinal )
Use this method with what is the zero-based column ordinal of your
FirstName
andAge
columns.Error 2 Argument 1: cannot convert from ‘string’ to ‘int’
There is no implicit conversation from
string
toint
. You get this error actually becausemyReader.GetString("FirstName")
method returnsstring
but yourAddXY()
method probably exceptint
as a first parameter.
If the selected answer did not help you out, the other answers might!
All Answers For: SQL Server Express and C# : Database Charting
StuartLC’s answer to
SQL Server Express and C# : Database Charting
IDataReader
‘s.Get*
methods only take the index of the column to be accessed, not the column name.So instead of
myReader.GetString("FirstName")
You’ll need something like
myReader["FirstName"].ToString()
and
Convert.ToInt32(myReader.GetInt32("Age")))
If you are doing this in a tight loop, then you can cache the name : index mappings.
ayilmaz’s answer to
SQL Server Express and C# : Database Charting
you should use GetString(0) or GetString(1) or GetString(2)
Soner Gönül’s answer to
SQL Server Express and C# : Database Charting
Error 1 The best overloaded method match for
‘System.Data.Common.DbDataReader.GetString(int)’ has some invalid
arguments
DbDataReader.GetString
method takes argument as anint
, notstring
.Gets the value of the specified column as an instance of String.
public abstract string GetString( int ordinal )
Use this method with what is the zero-based column ordinal of your
FirstName
andAge
columns.Error 2 Argument 1: cannot convert from ‘string’ to ‘int’
There is no implicit conversation from
string
toint
. You get this error actually becausemyReader.GetString("FirstName")
method returnsstring
but yourAddXY()
method probably exceptint
as a first parameter.
user3093781’s answer to
SQL Server Express and C# : Database Charting
Please try this code.
try{ conDatabase.Open(); myReader=cmdDatabase.ExecuteReader(); chart1.DataSource = myReader; chart1.Series["Age"].XValueMember = "FirstName"; chart1.Series["Age"].YValueMembers = "Age"; chart1.DataBind(); }
Of course, you should really check out the original question.
The post SQL Server Express and C# : Database Charting [ANSWERED] appeared first on Tech ABC to XYZ.