Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, can you help me with sql query. I try to sum column "Cas" with 2 conditions. Records in sql exist but nothing found.

thank you

What I have tried:

Dim conn As New SqlConnection(connstring)
        Dim cnn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim time As DateTime = DateTime.Now
        Dim dates As String = "yyyy-MM-dd"

        Try
            conn.Open()
        Catch
            MsgBox("Připojení k databázi se nezdařilo.", , "Chyba")
        End Try

        Dim COMMAND2 As String = (("SELECT case SUM(Cas) FROM nvsp_bar_automaticky_tisk_zamestnanci WHERE CisloZam = '" & TextBox1.Text & "'  AND (CONVERT(DATETIME,CONVERT(INT,Datum))) = '" & time.ToString(dates) & "'"))
        
        Dim DA2 As New SqlDataAdapter(CMD2)
        Dim TABLE2 As New DataTable
        If TABLE2.Rows.Count > 0 Then
            'SLOUPEC Z TABULKY 3
            Refresh()
            DA2.Fill(TABLE2)
            Label2.Text = TABLE2.Rows(0)(0).ToString()
        Else
            MsgBox("Číslo karty nenalezeno",, "Chyba")
        End If
        conn.Close()

    End Sub
Posted
Updated 13-May-19 1:31am
Comments
Kornfeld Eliyahu Peter 13-May-19 6:56am    
SELECT
CASE
SUM(CAS)
FROM
NVSP_BAR_AUTOMATICKY_TISK_ZAMESTNANCI
WHERE
CISLOZAM = '...'
AND (CONVERT(DATETIME,CONVERT(INT, DATUM))) = '...'

This is your SQL - check it again (it has basic syntax errors)...
Maciej Los 13-May-19 7:06am    
Are you sure it's a VB.NET? It looks to me that it's VB6 or VBA.
CHill60 13-May-19 7:59am    
I agree!
Richard Deeming 14-May-19 12:05pm    
SqlConnection, SqlDataAdapter, DataTable, DateTime.Now, and Try..Catch blocks didn't exist in either of those languages. :)
Maciej Los 15-May-19 17:11pm    
;)

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

When you've fixed that through your whole app, you can start to fix your problem - which is that you don't need the word "CASE" in there, unless you are trying to use an actual CASE WHEN statement. SQL CASE Statement[^]
 
Share this answer
 
Advice: print the resulting SQL query string to check that it is what you expect, or use the debugger.
VB
COMMAND2 = (("SELECT case SUM(Cas) FROM nvsp_bar_automaticky_tisk_zamestnanci WHERE CisloZam = '" & TextBox1.Text & "'  AND (CONVERT(DATETIME,CONVERT(INT,Datum))) = '" & time.ToString(dates) & "'"))

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900