Search code examples
c#stored-proceduressql-server-expressteechart

Populate PieChart based on SQL Server stored procedure


I have the following stored procedure that I have to get data from:

   EXECUTE [dbo].[StationHealthStatusSummary2]
            @LineId varchar(100),        // 5,9,10
            @MeasurementDt datetime,    //2012/06/06
            @Ntotal Int output,
            @N0 int output,
            @N1 int output,
            @N2 int output,
            @N3 int output,
            @N4 int output,
            @N5 int output,
            @N6 int output,
            @N7 int output,
            @N8 int output,
            @N9 int output,
            @N10 int output,
            @N11 int output,
            @N12 int output,
            @N13 int output,
            @N14 int output,
            @N15 int output,
            @N16 int output
       GO

Now I can send parameters to LineID and Measurement date as follows:

SqlConnection sql = new SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=iComs;Persist Security Info=True;User ID=sa;Password=Password);

SqlCommand getData = new SqlCommand("StationHealthStatusSummary2", sql);

SqlDataAdapter da = new SqlDataAdapter(getData);

getData.CommandType = CommandType.StoredProcedure;
getData.Parameters.Add(new SqlParameter("@LineId", Lines));
getData.Parameters.Add(new SqlParameter("@MeasurementDt", date1));

SqlParameter ParamaterNtotal = new SqlParameter();
ParamaterNtotal.ParameterName = "@Ntotal";
ParamaterNtotal.SqlDbType = SqlDbType.Int;
ParamaterNtotal.Direction = ParameterDirection.Output;
getData.Parameters.Add(ParamaterNtotal);

sql.Open();

getData.ExecuteNonQuery(); 

Now I can get the value of NTotal and assign it to a Teechart (PieSlice), but how do I get the value for @N0..@N16?

I've got some code that might give you some idea of what I'm trying to accomplish.

int NTotal = int.Parse(getData.Parameters["@Ntotal"].Value.ToString());

if (GetVariantVariableI(getData.Parameters[0].Value) = 0)
{               
    for (c = 1; c <= 18; c++)
    { 
        Nvl = GetVariantVariableI(getData.Parameters[2+c].Value);

        switch(c)
        {
            case 1:
                NTotal = Nvl;
                break;

            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:                           
            case 12:                            
            case 13:                           
            case 14:                           
            case 15:                            
            case 16:                           
            case 17:
            case 18:
            case 19:
            case 20:
                cstatus = c-2;
                NPieValues[cstatus] = Nvl;
                break;
   }   

   string SliceName;
   int NUsed;

   NUsed = 0;

   for(i=0;i<=16;i++)
   {
      NUsed = NUsed + NPieValues[i];
   }

   if (NUsed < NTotal)
   {
       Chart1.Series[0].Add(NTotal - NUsed);
       slice1.Title = "Not Connected";
       slice1.Add();
       slice1.Color = System.Drawing.Color.Silver;
   }

   if (NUsed > NTotal)
   {
      NPieValues[7] = NPieValues[7]-(NUsed-NTotal);
   }

   for (i=0;i<=16;i++)
   {
     if (NPieValues[i]>0)
     {
         switch(i)
         {
            case 0: SliceName ="Green"; 
                    break;
            case 1: SliceName ="Yellow";
                    break;
            case 2: SliceName ="Orange";
                    break;
            case 3: SliceName ="Red";
                    break;
            case 4: SliceName ="Broken Rail";
                    break;
            case 5: SliceName ="Buckling Rail";
                    break;
            case 6: SliceName ="Maintenance required";
                    break;
            case 7: SliceName ="Station(s) Off";
                    break;
            case 8: SliceName ="Rail Differential kN";
                    break;
            case 9: SliceName ="Left Rail Sensor Faulty";
                    break;
            case 10: SliceName ="Right Rail Sensor Faulty";
                     break;
            case 11: SliceName ="Temperature Rail Sensor Faulty";
                     break;
            case 12: SliceName ="Calibration Required";
                     break;
            case 13: SliceName ="Station Vandalised";
                     break;
            case 14: SliceName ="Station uninstalled";
                     break;
            case 15: SliceName ="Gauges removed for Maintenance";
                     break;
            case 16: SliceName ="No GSM Coverage";
                     break;
            default:
                     SliceName ="?";
         }
         switch(i)
         {
          case 0: clr = "System.Drawing.Color.Green";
                 break;
          case 1:clr = "System.Drawing.Color.Yellow";
                 break;
          case 2:clr = "System.Drawing.Color.Orrange";
                 break;
          case 3:clr = "System.Drawing.Color.Red";
                 break;
          case 4:
          case 5:
          case 8:clr = "System.Drawing.Color.Purple";
                 break;
          case 6:clr = "System.Drawing.Color.Black";
                 break;
          case 7:clr = "System.Drawing.Color.Gray";
                 break;;
          case 9:clr = "System.Drawing.ColorTranslator.FromHtml('#E0671F')";
                 break;
          case 10:clr = "System.Drawing.ColorTranslator.FromHtml('#BA4EC2')";
                 break;
          case  11:clr = "System.Drawing.ColorTranslator.FromHtml('#FF8000')";
                 break;
          case  12:clr =  "System.Drawing.ColorTranslator.FromHtml('#BF4093')";
                 break;
          case  13:clr = "System.Drawing.Color.SkyBlue";
                 break;
          case 14:clr = "System.Drawing.Color.Aqua";
                 break;
          case 15:clr = "System.Drawing.ColorTranslator.FromHtml('#BFBFFF')";
                 break;                 
          case 16:clr = "System.Drawing.Color.MedGray";
                 break;
             default : clr = "System.Drawing.Color.White";
                 break;              
         }
      slice1.Add(NPieValues[i],SliceName,clr);
     }

 }
            }
        }

    }

Now after it got all those values it has to populate the piechart.

Please, any help will be highly appreciated and please tell me if I'm being too vague.

Thanks


Solution

  • You have added @Ntotal as an Output parameter, why didn't you add all the others?

    SqlParameter n = new SqlParameter();
    n.ParameterName = "@N0";
    n.SqlDbType = SqlDbType.Int;
    n.Direction = ParameterDirection.Output;
    getData.Parameters.Add(n); 
    

    Then you can retrieve the output values the same way you do with @Ntotal. Also, you can retrieve the values by name instead of looping through them, if you prefer.