?
Solved

c# coding help

Posted on 2015-02-05
6
Medium Priority
?
105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
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 1500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
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…
Suggested Courses

800 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