I am getting the following error:
Unhandled Exception: System.Data.SqlClient.SqlException: Incorrect syntax near '500'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at DATACONSOLE.Module1.Main() in G:000000SRIKANTHProjectsVBdatabaseDATACONSOLEDATACONSOLEModule1.vb:line 70
My database table:
CREATE TABLE new
(
[SNO] INT PRIMARY KEY,
[SNAME] NCHAR(12) NULL,
[COURSE] NCHAR(10) NULL,
[TOTALMARKS] INT NULL DEFAULT 700,
[PERCENTAGEMARKS] DECIMAL(9, 2) NULL,
[GRADE] NCHAR(10) NULL
)
My code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Module Module1
Sub Main()
Dim CON As SqlConnection
Dim CMD As SqlCommand
Dim SOURCE As String
Dim COMMAND As String
SOURCE = "Data Source=DESKTOP-20RTV69SQLEXPRESS;Initial Catalog=VBONE;Integrated Security=True;Pooling=False"
CON = New SqlConnection(SOURCE)
CON.Open()
Dim SNO As Integer
Dim NAME As String
Dim COURSE As String
Dim MARKS As Integer
Dim TOTAL As Integer = 700
Dim PERCENTAGE As Double
Dim GRADE As String
Console.Write("ENTER SNO : ")
SNO = Convert.ToInt32(Console.ReadLine())
Console.Write("ENTER NAME : ")
NAME = Console.ReadLine()
Console.Write("ENTER COURSE : ")
COURSE = Console.ReadLine()
Console.Write("ENTER MARKS : ")
MARKS = Convert.ToInt32(Console.ReadLine())
PERCENTAGE = (MARKS / TOTAL) * 100
If (PERCENTAGE > 90) Then
GRADE = "'A'"
ElseIf (PERCENTAGE > 70) Then
GRADE = "'B'"
ElseIf (PERCENTAGE > 60) Then
GRADE = "'C'"
ElseIf (PERCENTAGE > 50) Then
GRADE = "'D'"
Else
GRADE = "'F'"
End If
COMMAND = "INSERT INTO NEW(SNO,NAME,COURSE,MARKSOBTAINED,PERCENTAGEMARKS,GRADE)
VALUES(" & SNO & " , '" & NAME & "' , '" & COURSE & "' " & MARKS & "," & PERCENTAGE & ",'" & GRADE & "')"
CMD = New SqlCommand(COMMAND, CON)
CMD.ExecuteNonQuery()
Console.WriteLine("---RECORD IS INSERTED---")
Console.ReadLine()
End Sub
End Module
Please could you find the solution to my problem and give me some suggestions for not repeating such mistakes.
See Question&Answers more detail:os