Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
QuestionSession ended event not properly working Pin
Member 1085025313-Mar-15 11:58
Member 1085025313-Mar-15 11:58 
AnswerRe: Session ended event not properly working Pin
Eddy Vluggen13-Mar-15 12:06
professionalEddy Vluggen13-Mar-15 12:06 
GeneralRe: Session ended event not properly working Pin
Member 1085025313-Mar-15 12:19
Member 1085025313-Mar-15 12:19 
GeneralRe: Session ended event not properly working Pin
Richard Deeming16-Mar-15 2:32
mveRichard Deeming16-Mar-15 2:32 
GeneralRe: Session ended event not properly working Pin
Member 1085025313-Mar-15 12:51
Member 1085025313-Mar-15 12:51 
GeneralRe: Session ended event not properly working Pin
Eddy Vluggen14-Mar-15 3:47
professionalEddy Vluggen14-Mar-15 3:47 
QuestionError When Table has No Records ... Pin
smh139213-Mar-15 9:27
smh139213-Mar-15 9:27 
AnswerRe: Error When Table has No Records ... Pin
phil.o13-Mar-15 11:13
professionalphil.o13-Mar-15 11:13 
There is so much to say about these few lines of code.

First, this does not compile, due to the third line.

Basically you are:
- reading a double value from your database.
- make a string out of this value if found, and set the Text property of a TextBox from the result ; if not found, you put a "0" string in another TextBox.
- then you convert it back to a double from the TextBox that can only be "0".

Moreover, as your WHERE clause restricts the result to one line, you should be using ExecuteScalar instead of ExecuteReader.

When you want to work with numbers, then keep up with the numbers. The only moment when you have to make a string from your number is when you want to present it to the user. Every design moving back and forth from number to string is a terrible practice and should be avoided at any time.

Also, when dealing with Disposable objects (OleDbCOnnection and OleDbCommand), you should use a using block to make sure you release all resources when you do not need them anymore.

At the end of your method, you return a double. This function should only deal with computing the result. Displaying it in a TextBox should be done elsewhere.

A more appropriate implementation could be:
C#
double result = 0;
string strSum = "SELECT SUM(Val1 + Val2 + Val3) AS Total FROM myTable WHERE year=@year";
int year;
// User inputs should never be trusted and always tested against validity
if (!int.TryParse(txtyear.Text, out year)) {
   throw new ArgumentException("Entered text is not convertible to a valid integer.");
}
using (OleDbConnection cnn = new OleDbConnection(strcn)) {
   cnn.Open();
   using (OleDbCommand cmd = new OleDbCommand(strSum, cnn)) {
      cmd.Parameters.AddWithValue("@year", year);
      try {
         result = (double)cmd.ExecuteScalar();
      }
      catch (SqlException ex) {
         MessageBox.Show(string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace));
      }
   }
}
return result;

There are two kinds of people in the world: those who can extrapolate from incomplete data.

QuestionShow Big Number in TextBox Pin
smh139213-Mar-15 1:01
smh139213-Mar-15 1:01 
AnswerRe: Show Big Number in TextBox Pin
Pete O'Hanlon13-Mar-15 1:31
mvePete O'Hanlon13-Mar-15 1:31 
GeneralRe: Show Big Number in TextBox Pin
smh139213-Mar-15 8:27
smh139213-Mar-15 8:27 
AnswerRe: Show Big Number in TextBox Pin
OriginalGriff13-Mar-15 3:13
mveOriginalGriff13-Mar-15 3:13 
GeneralRe: Show Big Number in TextBox Pin
harold aptroot13-Mar-15 5:01
harold aptroot13-Mar-15 5:01 
QuestionProject Architecture Pin
JammoD8712-Mar-15 21:57
JammoD8712-Mar-15 21:57 
AnswerRe: Project Architecture Pin
Eddy Vluggen13-Mar-15 10:54
professionalEddy Vluggen13-Mar-15 10:54 
GeneralRe: Project Architecture Pin
JammoD8715-Mar-15 4:21
JammoD8715-Mar-15 4:21 
AnswerRe: Project Architecture Pin
Subramanyam Shankar17-Mar-15 1:39
professionalSubramanyam Shankar17-Mar-15 1:39 
QuestionHow to use GIT with .Net Pin
AmbiguousName12-Mar-15 3:59
AmbiguousName12-Mar-15 3:59 
AnswerRe: How to use GIT with .Net Pin
Richard Deeming12-Mar-15 4:09
mveRichard Deeming12-Mar-15 4:09 
Questionc sharp user control Pin
Sarita S12-Mar-15 3:32
Sarita S12-Mar-15 3:32 
QuestionRe: c sharp user control Pin
ZurdoDev12-Mar-15 4:38
professionalZurdoDev12-Mar-15 4:38 
AnswerRe: c sharp user control Pin
Richard Givis24-Mar-15 12:05
Richard Givis24-Mar-15 12:05 
AnswerRe: c sharp user control Pin
Dave Kreskowiak12-Mar-15 5:21
mveDave Kreskowiak12-Mar-15 5:21 
SuggestionRe: c sharp user control Pin
Matt T Heffron12-Mar-15 10:20
professionalMatt T Heffron12-Mar-15 10:20 
AnswerRe: c sharp user control Pin
BillWoodruff13-Mar-15 4:38
professionalBillWoodruff13-Mar-15 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.