15.1.13

Error in chart control in C#


Error:
A chart element with the name 'Default' could not be found in the 'SeriesCollection'.

Solution: 

You need to create a series element for a chart before accessing it.

          Series series1 = this.chart3.Series.Add("Default");
            chart3.Series["Default"].Points.DataBindY(myReader1, "OrderQty");
         

Complete coding to draw a chart in C#


  string myConnectionString = @"Data Source=XXXX;Integrated Security=true;Initial Catalog=DBName";

            // Define the database query    
            string mySelectQuery = "SELECT Top 5 OrderQty,ProductID,SpecialOfferID, UnitPrice   FROM [SalesOrderDetail]";

            // Create a database connection object using the connection string    
            SqlConnection myConnection = new SqlConnection(myConnectionString);

            // Create a database command on the connection using query    
            SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);

            // Open the connection    
            myCommand.Connection.Open();

            // Create a database reader    
            SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            Series series = this.chart1.Series.Add("Default");

            chart1.Series["Default"].Points.DataBind(myReader, "OrderQty", "ProductID", "Tooltip=SpecialOfferID, Label=UnitPrice{C2}");
            chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

            // Close the reader and the connection

Cheers,
Venkatesan Prabu. J
Head, KaaShiv InfoTech, Chennai.


No comments:

Post a Comment