Click here to Skip to main content
15,068,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am not a coder, I'm a teacher. My sole aim is to code a simple educational application for my students. I was into programming for like 20 years ago but it's all so foggy now. So with this example, vb.net is throwing an exception only when I use some punctuations while recording a text to sql database.
VB
con.Open()

cmd = con.CreateCommand
cmd.CommandType = CommandType.Text
cmd = New SqlCommand("Select * from Dictionary where Id = " & intIdCheck & "", con)


Dim result As DialogResult = MessageBox.Show("This word already exists in the Dictionary. Do you want to edit this entry?", "Entry Exists!", MessageBoxButtons.YesNo)

If result = vbYes Then

    cmd.CommandText = "Update Dictionary
    SET English = '" & TextBox1.Text & "',
    Turkish1 = '" & TextBox2.Text & "',
    TUrkish2 = '" & TextBox3.Text & "',
    Turkish3 = '" & TextBox4.Text & "',
    Turkish4 = '" & TextBox5.Text & "',
    Turkish5 = '" & TextBox6.Text & "',
    Turkish6 = '" & TextBox7.Text & "',
    Noun = '" & CheckBox1.CheckState & "',
    Verb = '" & CheckBox2.CheckState & "',
    Adjective = '" & CheckBox3.CheckState & "',
    Adverb = '" & CheckBox4.CheckState & "',
    NounSnt1 = '" & RichTextBox1.Text & "',
    NounSnt2 = '" & RichTextBox2.Text & "',
    VerbSnt1 = '" & RichTextBox3.Text & "',
    VerbSnt2 = '" & RichTextBox4.Text & "',
    AdjAdvSnt1 = '" & RichTextBox5.Text & "',
    AdjAdvSnt2 = '" & RichTextBox6.Text & "'
    where Id = " & intIdCheck & ""

    cmd.ExecuteNonQuery()

    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    TextBox5.Text = ""
    TextBox6.Text = ""
    TextBox7.Text = ""
    CheckBox1.Checked = CheckState.Unchecked
    CheckBox2.Checked = CheckState.Unchecked
    CheckBox3.Checked = CheckState.Unchecked
    CheckBox4.Checked = CheckState.Unchecked
    RichTextBox1.Text = ""
    RichTextBox2.Text = ""
    RichTextBox3.Text = ""
    RichTextBox4.Text = ""
    RichTextBox5.Text = ""
    RichTextBox6.Text = ""
    intIdCheck = 0

Else

    Exit Sub

End If

So, the richtextbox controls are for sentences and textboxes are for words. Exception is thrown only when I update the database table with a sentence which includes certain punctuations, like an apostorophe '. Does anyone know what this is about and may they also offer me an easy and simple solution, as I am not so pro at coding.

Thanks in advance.

P.S: I am using visual studio 2019 and it says T-SQL on the interface.

What I have tried:

I haven't tried anything yet because I honestly have no idea about this and it's a difficult search to perform on google. Did you mean "vb.net throwing an exception while trying to insert punctuations into the database" nope... :)
Posted
Updated 19-Dec-20 8:30am
v3

Using parameterized queries is recommended (also for security reasons), see: how to escape single quotes in a dynamic SQL clause?[^]
   
Simple: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And in your case, it's causing problems for the same reason without SQL Injection - fix that through your whole app - and teach your students that as well - and the problem you have noticed will go away as well...

And two other things you - and students - should learn early:
1) Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...

2) Learn to use the debugger - it will save you huge amounts of time and effort - it's the best friend you have as a developer, and you will spend most of your time using it (instead of a lot more of your time tearing your hair out in frustration!)
   
Comments
salim çataloglu 19-Dec-20 14:36pm
   
@RickZeeland Thank you very much for your help. It has been quite useful for me. Now I understood what went wrong there. Although, I am sorry but I can't teach that to my students as I am an English teacher. This application is just for helping them with some vocabulary work out since we are on online education schedule and I can't do classroom exercises. This will probably be my last application ever, but it will be very very useful. Thanks again :)
Quote:
So with this example, vb.net is throwing an exception only when I use some punctuations while recording a text to sql database.
VB
cmd.CommandText = "Update Dictionary
SET English = '" & TextBox1.Text & "',
Turkish1 = '" & TextBox2.Text & "',
TUrkish2 = '" & TextBox3.Text & "',
Turkish3 = '" & TextBox4.Text & "',
Turkish4 = '" & TextBox5.Text & "',
Turkish5 = '" & TextBox6.Text & "',
Turkish6 = '" & TextBox7.Text & "',
Noun = '" & CheckBox1.CheckState & "',
Verb = '" & CheckBox2.CheckState & "',
Adjective = '" & CheckBox3.CheckState & "',
Adverb = '" & CheckBox4.CheckState & "',
NounSnt1 = '" & RichTextBox1.Text & "',
NounSnt2 = '" & RichTextBox2.Text & "',
VerbSnt1 = '" & RichTextBox3.Text & "',
VerbSnt2 = '" & RichTextBox4.Text & "',
AdjAdvSnt1 = '" & RichTextBox5.Text & "',
AdjAdvSnt2 = '" & RichTextBox6.Text & "'
where Id = " & intIdCheck & ""


Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900