Click here to Skip to main content
15,888,454 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Error in IE11 for website automation using vb6 Pin
Chris Quinn3-Apr-16 21:56
Chris Quinn3-Apr-16 21:56 
AnswerRe: Error in IE11 for website automation using vb6 Pin
Mycroft Holmes4-Apr-16 14:20
professionalMycroft Holmes4-Apr-16 14:20 
AnswerRe: Error in IE11 for website automation using vb6 Pin
CHill604-Apr-16 22:40
mveCHill604-Apr-16 22:40 
QuestionCalculate networkdays in vb.net, can someone help me? Pin
Real Corks27-Mar-16 11:57
Real Corks27-Mar-16 11:57 
GeneralRe: Calculate networkdays in vb.net, can someone help me? Pin
Sascha Lefèvre27-Mar-16 12:29
professionalSascha Lefèvre27-Mar-16 12:29 
AnswerRe: Calculate networkdays in vb.net, can someone help me? Pin
CHill6027-Mar-16 12:44
mveCHill6027-Mar-16 12:44 
Questionsum a field in database, can someone help me? Pin
Real Corks27-Mar-16 4:21
Real Corks27-Mar-16 4:21 
AnswerRe: sum a field in database, can someone help me? Pin
Sascha Lefèvre27-Mar-16 4:58
professionalSascha Lefèvre27-Mar-16 4:58 
I'm pretty sure you intend the "date-range" to be a WHERE-predicate, right? I don't know how exactly MySQL interpretes the statement that you have there currently; apparently it's no syntax error but it most certainly won't be interpreted as a WHERE-predicate.

Also you should use SQL-parameters (MySqlParameter in this case) instead of concantenating your SQL-statement. Concatenating values into the statement has several(!) disadvantages, the most prominent one being the risk of SQL-injection, potentially leading to inadvertant or purposeful destruction of your database (Google SQL-injection).

Also you should use using-statements for your MySql****-objects like the connection, command, datareader, etc., which will ensure those are properly disposed in any case.

Also I recommend to not keep any of those objects as class variables but only as local variables.

And since you only want one single value, you don't need a DataReader; instead you can execute your command with ExecuteScalar().

VB
Dim startDate As DateTime
Dim endDate As DateTime

If Not DateTime.TryParse(dtpstart.Text, startDate) Then
   ' error indication and return
End If

If Not DateTime.TryParse(dtpend.Text, endDate) Then
   ' error indication and return
End If

Dim sql As String = "SELECT SUM(late) AS SumTotalLate FROM tbl_attendance WHERE employeeNumber=@empNo AND dates BETWEEN @startDate AND @endDate;"

Try
   Using conn As New MySqlConnection(ConnectionString) ' ConnectionString from somewhere central
      Using cmd As New MySqlCommand(sql, conn)

         cmd.Parameters.AddWithValue("@empNo", employeeNumber)
         cmd.Parameters.AddWithValue("@startDate", startDate)
         cmd.Parameters.AddWithValue("@endDate", endDate)

         conn.Open()

         TextLateDeduct.Text = cmd.ExecuteScalar().ToString()
         
      End Using
   End Using
Catch ex As MySqlException
   MessageBox.Show(ex.Message)
End Try


(I'm no VB.NET 'guy', maybe there's a small syntax error in there somewhere.)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson


modified 27-Mar-16 11:34am.

GeneralRe: sum a field in database, can someone help me? Pin
Real Corks27-Mar-16 9:05
Real Corks27-Mar-16 9:05 
GeneralRe: sum a field in database, can someone help me? Pin
Sascha Lefèvre27-Mar-16 9:42
professionalSascha Lefèvre27-Mar-16 9:42 
GeneralRe: sum a field in database, can someone help me? Pin
Real Corks27-Mar-16 9:47
Real Corks27-Mar-16 9:47 
GeneralRe: sum a field in database, can someone help me? Pin
Sascha Lefèvre27-Mar-16 10:12
professionalSascha Lefèvre27-Mar-16 10:12 
GeneralRe: sum a field in database, can someone help me? Pin
Real Corks27-Mar-16 11:29
Real Corks27-Mar-16 11:29 
Questioncan someone help me to fix this? because i cannot make another xml file only the employee information Pin
Real Corks17-Mar-16 17:47
Real Corks17-Mar-16 17:47 
AnswerRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Patrice T17-Mar-16 20:49
mvePatrice T17-Mar-16 20:49 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Real Corks20-Mar-16 21:43
Real Corks20-Mar-16 21:43 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
CHill6020-Mar-16 22:41
mveCHill6020-Mar-16 22:41 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Real Corks27-Mar-16 4:18
Real Corks27-Mar-16 4:18 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
CHill6027-Mar-16 5:29
mveCHill6027-Mar-16 5:29 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Richard MacCutchan21-Mar-16 1:10
mveRichard MacCutchan21-Mar-16 1:10 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Dave Kreskowiak21-Mar-16 2:47
mveDave Kreskowiak21-Mar-16 2:47 
GeneralRe: can someone help me to fix this? because i cannot make another xml file only the employee information Pin
Richard MacCutchan21-Mar-16 2:55
mveRichard MacCutchan21-Mar-16 2:55 
Questionvb.net 2010 has sql error Pin
dcof16-Mar-16 12:28
dcof16-Mar-16 12:28 
AnswerRe: vb.net 2010 has sql error Pin
Mycroft Holmes16-Mar-16 23:08
professionalMycroft Holmes16-Mar-16 23:08 
AnswerRe: vb.net 2010 has sql error Pin
dcof17-Mar-16 8:43
dcof17-Mar-16 8:43 

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.