C# .NET - error in code

Asked By rehu on 20-Oct-10 12:19 AM
String querychart = "select * from table where Id="+id+'";
      //Response.Write("querychart" + querychart);
      SqlCommand cmd = new SqlCommand(querychart, con);
      con.Open();
      rd = cmd.ExecuteReader();
      Chart1.DataSource = cmd;
      Chart1.Series["1"].XValueMember = "average1";
     Chart1.Series["2"].XValueMember = "average2";
      Chart1.Series["3"].XValueMember = "average3";
     Chart1.Series["4"].XValueMember = "average4";
    
      Chart1.Series["4"].YValueMembers = "State";
     
      con.Close();
Jatin Prajapati replied to rehu on 20-Oct-10 12:23 AM
I think you can't directly set the cmd as the datasource to the char object.
Try to get the data in datatable to dataset and then set it as DataSource to the char control.


Let me know the resutls.
Reena Jain replied to rehu on 20-Oct-10 02:10 AM
hi rehu

try this

String querychart = "select * from table where Id="+id+'";
    //Response.Write("querychart" + querychart);
    SqlCommand cmd = new SqlCommand(querychart, con);
    SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataTable dt=New DataTable();
    da.Fill(dt);
   Chart1.DataSource = dt;


Hope this will help you
Sagar P replied to rehu on 20-Oct-10 02:34 AM
Use datareader as datasource instead of cmd like;

String querychart = "select * from table where Id="+id+'";
    //Response.Write("querychart" + querychart);
    SqlCommand cmd = new SqlCommand(querychart, con);
    con.Open();
    rd = cmd.ExecuteReader();
    Chart1.DataSource = rd;
    Chart1.Series["1"].XValueMember = "average1";
   Chart1.Series["2"].XValueMember = "average2";
    Chart1.Series["3"].XValueMember = "average3";
   Chart1.Series["4"].XValueMember = "average4";
    
    Chart1.Series["4"].YValueMembers = "State";
   
    con.Close();

This will give you proper output.....