Question Multiple SQL Commands in one Action

vb8web

New member
Joined
Jun 9, 2012
Messages
3
Programming Experience
1-3
VB.NET:
Private Sub Woodstock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connection As New SqlClient.SqlConnection("Data Source=server;Initial Catalog=umrdashboard;Persist Security Info=True;User ID=userid;Password=password")
        Dim command As New SqlClient.SqlCommand
        Dim adapter As New SqlClient.SqlDataAdapter
        Dim dataset As New DataSet
        command.CommandText = "SELECT * FROM dateprolog WHERE Office='Woodstock' AND Date='" & Date.Now & "'"
        connection.Open()
        command.Connection = connection
        adapter.SelectCommand = command
        adapter.Fill(dataset, "0")
        Dim count = dataset.Tables(0).Rows.Count
        If count > 0 Then
            MsgBox("A report has already been started for today. Please select the End Of Day tab to view what is left.", vbOKOnly)
            connection.Close()
        Else
            MsgBox("There is not a Report started for today! I have started one for you.", vbOKOnly, "New Day Opened")
            command.Connection = connection
            command.CommandText = "INSERT INTO dateprolog (Office, Date, PC, UF, EMS, PT, EMP, UNITS, FS, EOD) VALUES ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')"
            Try
                connection.Open()
                command.ExecuteNonQuery()
                connection.Close()
            Catch ex As Exception
                MsgBox("Error")
            End Try
        End If
    End Sub
The above code does not work. I need for this to check to see if there is a database entry with the office name and today's date. Then if not it needs to tell the user that it is going to create a new entry for todays date. Then the code is trying to enter a new row in the database but it is like i can't use command.commandtext = in two spots. Both SQL commands work if separated but not together in this code. Any help would be greatly appreciated. Obviously the abcdefghij are just test entries but it works if it is separated from the other sql command above. Also of course i have changed the datasource above to hide mine. Thank you
I not getting a program error code but it is not inserting the new row into dateprolog database.
 
First of all, you should not be using a DataAdapter to Fill a DataTable in the first case. You don't actually want any of that data so why retrieve it? Your query should be getting just the count of the matching records, not the records themselves. You then call ExecuteScalar to get that count. Either it's zero or it's not zero. That's all you care about.

As for the issue, if an exception is not thrown then it must be working. What value does ExecuteNonQuery return?
 
Back
Top