Solved

executenonquery() Syntax Error

Posted on 2010-09-21
6
394 Views
Last Modified: 2013-11-07
I'm getting a "Syntax error converting datetime from character to string".
The returned error says it is located on this line of code: cmd.executenonquery()

Please see code below. All and any help is greatly appreciated. Thanks!
Public Sub btnSubmit_Click (byval s as object, byval e as system.eventargs)
	Dim con as sqlConnection
	dim cmd as sqlcommand
	Dim dtr as Sqldatareader
	Dim str as string
	Dim strAccount as string
	dim BreakTestID as integer
	
	con = new sqlconnection ("Server=sqlserver;UID=sa;PWD=#####;Database=BreakTest")
	
	str = "INSERT tblBreakTest (BreakTestNO, InstallDate, CustomerNO, LoginID,BreaktestDate,Division,Application, TypeofService, Comments, Location, ProdDesc, NoofLines, ProductNO, ReturnReason, Status, SplicerName, SplicerRegion, SplicerCountry, SplicerPhone, SplicerPostalCode, SplicerState) VALUES "
	str = str & "(@BreakTestNo, @InstallDate, @Customer, @Login, @BreakTestDate, @Division, @Application, @TypeofService, @Comments, @Location, @ProdDesc, @NoofLines, @Product, @ReturnReason, @Status, @SplicerName, @Region, @Country, @SplicerPhone, @PostalCode, @State )"
	cmd = New sqlcommand (str, con)
	cmd.parameters.add ("@BreakTestNo", lblBreakTestID.Text)
	cmd.parameters.add ("@InstallDate", txtinstallDate.text)
	cmd.parameters.add ("@Customer", lblCompanyNo.text)
	cmd.parameters.add ("@Login", label2.text)
	cmd.parameters.add ("@BreakTestDate", txtDate.text)
	cmd.parameters.add ("@Division", cboDivision.text)
	cmd.parameters.add ("@Application", cboApplication.text)
	cmd.parameters.add ("@TypeofService", cboService.text)
	cmd.parameters.add ("@Comments", txtComments.text)
	cmd.parameters.add ("@Location", cboLocation.text)
	cmd.parameters.add ("@ProdDesc", txtProdDesc.text)
	cmd.parameters.add ("@NoofLines", txtNoLines.text)
	cmd.parameters.add ("@Product", txtProductNo.text)
	cmd.parameters.add ("@ReturnReason", cboReturnReason.text)
	cmd.parameters.add ("@SplicerName", txtSplicerName.text)
	cmd.parameters.add ("@Region", txtRegion.text)
	cmd.parameters.add ("@Country", txtCountry.text)
	cmd.parameters.add ("@SplicerPhone", txtPhoneNumber.text)
	cmd.parameters.add ("@PostalCode", txtPostalCode.text)
	cmd.parameters.add ("@State", txtState.text)
	
	strAccount = request.cookies("AccountCookie").value
	
	Select Case strAccount
	Case "Regional Sales Manager"
		cmd.parameters.add ("@Status", "Pending-GSM Approval")
	Case "General Sales Manager"
		cmd.parameters.add ("@Status","Pending-Technical Manager Approval")
	Case "R & D Personnel"
		cmd.parameters.add ("@Status", "Approved-Pending Receipt of Rope")
	Case Else
		cmd.parameters.add("@Status", "Initiated")
	End Select
	
	con.open()
	cmd.executenonquery()
	con.close()

Open in new window

0
Comment
Question by:dhaverstraw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Expert Comment

by:timexist
ID: 33729667
error is from this:
cmd.parameters.add ("@InstallDate", txtinstallDate.text)
try to change to this:
cmd.parameters.add ("@InstallDate",Cdate(txtinstallDate.text))

Of course you need to user's input first.
0
 

Author Comment

by:dhaverstraw
ID: 33729781
Changed the code. User now recieves this error: Conversion from string "tbd" to type 'Date' is not valid.
Error is on cmd.parameters.add ("@InstallDate", Cdate(txtinstallDate.text))
0
 
LVL 4

Accepted Solution

by:
timexist earned 105 total points
ID: 33729821
you need to check this txtinstallDate.text first.
before you pass this string in, you should put an validation on this field.
something likes a mask.
All Input Data Is Evil.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 29

Assisted Solution

by:Kumaraswamy R
Kumaraswamy R earned 20 total points
ID: 33731632


try to change like this:
if (txtinstallDate.text != "")
 cmd.parameters.add ("@InstallDate",Cdate(txtinstallDate.text))
else
 cmd.parameters.add ("@InstallDate",datetime.minvalue)
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33731909
In the simplest terms how many date columns does the table contain and what are the values you're passing through the command?
0
 
LVL 5

Expert Comment

by:soujanya_g
ID: 33763202
Hi

InstallDate as taken as text parameter insted of date time convert it into date time using cDate and eecute your application
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change this SQL to get all nodes 3 54
Adjust the codes 3 63
SQL DATE Past due to current wek 4 22
.NET universe documentation poster 2 19
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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