Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,716 members, 7,802,164 topics. Date: Friday, 19 April 2024 at 10:15 AM

Help With Construction Of Troublesome Sql Statement In Vb.net! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With Construction Of Troublesome Sql Statement In Vb.net! (1179 Views)

Help...my If Statement In Php Is Not Echoing / Fingerprint Authentication In Vb.net Application / How To Deploy Application With Access Database In Vb.net 2008 (2) (3) (4)

(1) (Reply) (Go Down)

Help With Construction Of Troublesome Sql Statement In Vb.net! by davidt(m): 9:04am On Jun 02, 2008
I am working on a small project and I have run into an itchy problem with an SQL statement. The construction of the SQL statement is as below:

Me.StudInsertCommand.CommandText = _
"INSERT INTO Students (FirstName, LastName, MidName, SessionAdmitted, Level, " & _
"MatricNumber, DeptID, ProgID, FacultyID) VALUES ('" & firstTxt.Text & "', " & _
"'" & lastTxt.Text & "', " & _
"'" & midTxt.Text & "', " & _
"'" & sessAdTxt.Text & "', " & _
levelBox.Text & ", " & _
"'" & matricTxt.Text & "', " & _
deptBox.SelectedValue & ", " & _
progBox.SelectedValue & ", " & _
facultyBox.SelectedValue & "wink"

Me.StudInsertCommand.ExecuteNonQuery()


Now the above statement is in the Click event of a button. After all the necessary information has been entered and the add button is clicked, the VB.NET compiler/IDE indicates that there is an SQL syntax error. My database is implemented in MS Access and I am using an OleDbConnection and MS Jet. The resultant SQL statement from the above concatenation is below:

"INSERT INTO Students (FirstName, LastName, MidName, SessionAdmitted, Level, MatricNumber, DeptID, ProgID, FacultyID) VALUES ('Gill', 'Bates', 'Michael', '2006/2007', 300, '06BC0987', 19, 27, 5)"

As stated earlier, all this results in an SQL syntax error. Below is the StackTrace. Database programming really annoys me. Please help! I have been staring at this thing since last week and annoyingly, it may be a simple problem.

Unhandled Exception: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()


Can anyone please help me take a look, because my eye cannot see anything. I am quite sure of the data types. Thanks a lot guys!
Re: Help With Construction Of Troublesome Sql Statement In Vb.net! by kanirip: 10:37am On Jun 02, 2008
Nothing seems to be wrong with the code, although I must confess that I am a Java Programmer tongue

Anyways is the SQL string that u displayed the actual one that is generated by the code or what YOU EXPECT to be generated from the code.

If it was actually generated from the code , then you can simply copy and paste the code into your SQL Manager and see if it executes on its own. If it doesn't execute then you really are in the shit! From what I can see the only possible source of error is the spaces between the commas in the insert values. But that shouldn't be a big deal!

If that fails I'm sure it would have to do with the structure of your table but I seriously doubt it reaches that far!
Re: Help With Construction Of Troublesome Sql Statement In Vb.net! by davidt(m): 10:59am On Jun 02, 2008
The SQL statement is what was generated. Someone gave me an idea as to what might be wrong, I'll try that and keep the thread updated.

Kanirip, thanks for checking it.
Re: Help With Construction Of Troublesome Sql Statement In Vb.net! by luckyCO(m): 11:59am On Jun 02, 2008
Try this and let me know if it works.

INSERT INTO STUDENTS set FirstName=’Gill’,lastname=’Bates’,Midname=’Micheal’,sessionadmited=’2006/2007’,Level=300,MatricNumber='06BC0987',
DeptID=19,ProgID=27, FacultyID=5


Use Val keyword or integer.trypass or Directcast to cast string to Integer.

Val(deptBox.SelectedValue) & ", " & _
Val(progBox.SelectedValue) & ", " & _
val(facultyBox.SelectedValue )& ")"



2k care

(1) (Reply)

What Android Phone Wud You Recommend For A Programmer? / Microfinance Bank Application Software / Referral Code Network

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.