Solved

Error updating SQL server table with vb.net

Posted on 2016-07-22
12
33 Views
Last Modified: 2016-07-22
I get a syntax error near dtTo. Not sure if dates are handled differently or if it is something else. The select statement is hitting the Case False. Code below...

 Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        Try
            con.ConnectionString = sqlComm
            con.Open()
            cmd.Connection = con
            Select Case ysnNew
                Case False
                    cmd.CommandText = "Update tblLeads " & vbCrLf &
                       "SET strType  = @strType," & vbCrLf &
                            "numClient  = @numClient," & vbCrLf &
                            "strLast = @strLast," & vbCrLf &
                            "strFirst = @strFirst" & vbCrLf &
                            "dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
                            "tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
                            "dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
                            "tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
                            "strNotes = @strNotes" & vbCrLf &
                            "strResponse = @strResponse" & vbCrLf &
                            "strLeadType = @strLeadType" & vbCrLf &
                            "ysnBad = @ysnBad" & vbCrLf &
                            "ysnPhone = @ysnPhone" & vbCrLf &
                       "Where numLeadID = @numLeadID"
                Case Else
                    cmd.CommandText = "Insert Into tblLeads " & vbCrLf &
                            "strType  = @strType," & vbCrLf &
                            "numClient  = @numClient," & vbCrLf &
                            "strLast = @strLast," & vbCrLf &
                            "strFirst = @strFirst" & vbCrLf &
                            "dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
                            "tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
                            "dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
                            "tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
                            "strNotes = @strNotes" & vbCrLf &
                            "strResponse = @strResponse" & vbCrLf &
                            "strLeadType = @strLeadType" & vbCrLf &
                            "ysnBad = @ysnBad" & vbCrLf &
                            "ysnPhone = @ysnPhone" & vbCrLf & ""
            End Select
            cmd.Parameters.AddWithValue("@strType", Me.txtType.Text)
            cmd.Parameters.AddWithValue("@numClient", Me.txtClient.Text)
            cmd.Parameters.AddWithValue("@strLast", Me.txtlast.Text)
            cmd.Parameters.AddWithValue("@strFirst", Me.txtFirst.Text)
            cmd.Parameters.AddWithValue("@dtTo", Me.dtTO.Text)
            cmd.Parameters.AddWithValue("@tmTO", Me.tmTO.Text)
            cmd.Parameters.AddWithValue("@dtCT", Me.dtCT.Text)
            cmd.Parameters.AddWithValue("@tmCT", Me.dtCT.Text)
            cmd.Parameters.AddWithValue("@strNotes", Me.txtNotes.Text)
            cmd.Parameters.AddWithValue("@strResponse", Me.txtEmail.Text)
            cmd.Parameters.AddWithValue("@strLeadType", Me.cboVehicle.Text)
            cmd.Parameters.AddWithValue("@ysnBad", Me.chkBad.Text)
            cmd.Parameters.AddWithValue("@ysnPhone", Me.ysnPhone.Text)
            Select Case ysnNew
                Case False
                    cmd.Parameters.AddWithValue("@numLeadID", Me.txtID.Text)
            End Select
            cmd.ExecuteNonQuery()

        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try
0
Comment
Question by:smm6809
  • 7
  • 5
12 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41724806
one thing for sure, you are missing a comma on this line:

"strFirst = @strFirst," & vbCrLf &

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41724811
also, it is the fist time I see this syntax:

"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &

Open in new window


I would be very surprised if you can specify a datatype like this! and I don't see any trace of if in the sql-bible: https://msdn.microsoft.com/en-us/library/ms177523.aspx
0
 

Author Comment

by:smm6809
ID: 41724851
Took SqlDbTypes.DateTime out of all places I referenced it. Fixed the comma issues and now getting: Error while inserting record... Conversion failed when converting date and/or time from character string.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41724871
your date & time fields are surely incorrectly formatted. Can you try something like this (for all your date & time fields):
cmd.Parameters.AddWithValue("@dtTo", Me.dtTO.Value)

Open in new window


Here I suppose that dtTO is a datetimepicker control.
0
 

Author Comment

by:smm6809
ID: 41724884
My time fields tmTO and tmCT don't like that. It says value is not a member of a textbox. I use a textbox for the user to enter the times (ie. 3:15 PM) because I have to measure how long it took for a task to be completed. Should I be doing it differently?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41724891
and what is the datatype in the database?

You will need to convert your text to a datetime value probably
0
 

Author Comment

by:smm6809
ID: 41724896
In the database those fields are time(7)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41724905
I never tried those. There is no real .Net equivalent. You could at least try to convert from text to datetime:
Dim dtmTime As DateTime = DateTime.Parse(tmTO.Text)
cmd.Parameters.AddWithValue("@tmTO", dtmTime)

Open in new window

0
 

Author Comment

by:smm6809
ID: 41724933
That worked. Just have one question. Is there a way to format date and time to read month/day/year instead of the other way around?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41725059
read/format from where?
0
 

Author Comment

by:smm6809
ID: 41725064
In the sql table the date is formatted 2016-05-09. Does that have to be the format or can it be 05-09-2016?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41725066
it is usually not the job of SQL server to format dates.

but if you really need, you can use Format:
SELECT FORMAT(GETDATE(), 'MM-dd-yyyy')

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

820 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