Solved

c# coding help

Posted on 2015-02-05
6
97 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is about my first experience with programming Arduino.
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 …

867 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

17 Experts available now in Live!

Get 1:1 Help Now