Solved

c# coding help

Posted on 2015-02-05
6
96 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 33

Expert Comment

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

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now