Data type Conversion error when running a select query with a SUM() function agaignst database.

ALTER PROCEDURE dbo.GetSupportHoursByCompanyID
@CompanyID int
AS
      
      SELECT     SUM(ticket_support_time_used) AS SupportUsed
      FROM         tbl_ticket
      WHERE     (company_ID = @CompanyID)
      
      RETURN

After running the above Stored Procedure  i get the following error message from ASP.NET:


"Error converting data type nvarchar to int."


My Database code looks like this:

public List<Ticket> GetSupportTimeforCompany( int companyID)
        {
            // Create Product collection
            List<Ticket> colTickets = new List<Ticket>();

            //Create an instance of the DataManager Class to get the connection string.
            DataManager ConnectString = new DataManager();

            // Create connection
            SqlConnection con = new SqlConnection(ConnectString.GetConnectString());

            // Create command
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetSupportHoursByCompanyID";

            //Pass the parameters sent in (companyName, companyLocation) for the SqlQuery
           
            cmd.Parameters.AddWithValue("@CompanyID", "companyID");


            // Execute command
            using (con)
            {
                try
                {
                    con.Open();

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Ticket theTicket = new Ticket();


                        theTicket.SupportTimeUsed = (decimal)reader["SupportUsed"];
                       


                        colTickets.Add(theTicket);
                    }




                    reader.Close();
                    con.Close();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message + " There was a problem getting the support hours used from the Database.");

                }

            }
            return colTickets;

        }



Any insight would be greatly appreciated.

Thanks in Advance.

appbuilderAsked:
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.

DhaestCommented:
Do you get any error when you run the sql directly on your database ?
On which line do you get the error ?
0
Pratima PharandeCommented:
  // try like
        cmd.Parameters.Add("@CompanyID", SqlDbType.Int).Value = 1;   // here 1 is harcoded
        //In your case
        cmd.Parameters.AddWithValue("@CompanyID", "companyID");  //"companyID" is string , send it as integer
        cmd.Parameters.AddWithValue("@CompanyID", 1);
0
appbuilderAuthor Commented:
I get the error when i try to view the page in which the  sql query is called. I have an objectdatasouce control what populates a repeater control. The objectdatasource control ofcourse if responsible for calling the select method described above.

Also to answer your other question, i get the error message on line 146 which is where the  try catch block is.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Guy Hengel [angelIII / a3]Billing EngineerCommented:

            cmd.Parameters.AddWithValue("@CompanyID", "companyID");

should be:


            cmd.Parameters.AddWithValue("@CompanyID", companyID);
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
LimbeckCommented:
hi, the data in the field ticket_support_time_used; what does it consists of? can you post a few values of this field?

maybe this will work

     SELECT     SUM(cast(ticket_support_time_used as float)) AS SupportUsed
      FROM         tbl_ticket
      WHERE     (company_ID = @CompanyID)

or

     SELECT     Cast(SUM(cast(ticket_support_time_used as float)) as float)) AS SupportUsed
      FROM         tbl_ticket
      WHERE     (company_ID = @CompanyID)


0
appbuilderAuthor Commented:
Great work Angellll !
0
Pratima PharandeCommented:
:(  ... mine also right answer.....

ok , glad to see your problem in solved
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
C#

From novice to tech pro — start learning today.