Solved

c# coding help

Posted on 2015-02-05
6
100 Views
Last Modified: 2015-02-18
I need some help.
Can someone please take a look at the attached and please tell me what I need to adjust?
please & thank you.

This bit of code goes throught two access tables (inner joint) and returns the total number of hours worked by a mechanic
Capture.JPG
0
Comment
Question by:MrMay
6 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40591569
I removed my bad post. I had no way of deleting it.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40591595
Hi MrMay;

Try and placing everything after (int) in its own set of parentheses.

newNum1 = (int)( ... );
0
 

Author Comment

by:MrMay
ID: 40591616
I tried Fernando... no difference.
This code works if I replace SUM with COUNT in the sql query.  But I don't want a count in this situation but a SUM.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40591618
An index operation (i.e. the square brackets) has higher precedence than a cast (i.e. the (int) part), so you shouldn't need the additional parentheses that Fernando suggests...though adding them won't hurt anything. Are you certain that the think inside the column is actually of type int? Does the error go away if you change the code to use the Convert class instead?

e.g.

newNum1 = Convert.ToInt32(ds.Tables["TaskLabor"].Rows[ds.Tables["TaskLabor"].Rows.Count - 1][0]);

Open in new window

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40591975
As you are wanting just one value to be returned you might want to consider the ExecuteScalar
https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar%28v=vs.110%29.aspx
approach which should use rather less resources than DataAdaptor/Table method you currently use.

There is an example on that link
public void CreateMyOleDbCommand(string queryString, 
    OleDbConnection connection) 
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    command.Connection.Open();
    command.ExecuteScalar();
    connection.Close();
}

Open in new window

where the queryString would be your SQL with the SUM

eg something like
...
    OleDbCommand command = new OleDbCommand(queryString, connection);
    command.Connection.Open();
    newNUm1 = command.ExecuteScalar();
    connection.Close();
return newNum1
}
0
 

Author Comment

by:MrMay
ID: 40592021
thanks for the post kaufmed that worked. The only problem that I'm running into now is if there is a return of null (zero) I still get the same error msg.
So lets say hrs of Mechanic1 is 5.. i get that return. but lets say mechanicB has worked no hrs.. it crashes on me.
How do i adjust that so if there is a return of null (nothing) it returns a zero.
thank u all.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question