Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am having a problem with SQL Query C#, WPF I am doing a Library Management System so the student can take a maximum of 5 books I have created Book_1, Book_2,... _5 Cells in the student Table So if book1 equals Null then add book if it has a book name then insert into next cell(Book_2) any idea??


What I have tried:

<pre lang="C#">tring mySQL = string.Empty;
            mySQL += "UPDATE StudentsTbl SET Book_1 = CASE WHEN Book_1 = NULL THEN '"+ IDTxt.Text +"' END WHERE ID='"+ UserIDTxt.Text +"'";
            Connection.LibraryServer.executeSQL(mySQL);
            System.Windows.MessageBox.Show("ADD BOOK");
Posted
Updated 13-May-21 6:45am

First off, 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?

Secondly, that won't even try to work because you don't supply a value in all cases - so SQL doesn't know what to do when the CASE WHEN fails.

But why are you doing it like that at all?
If you want to replace all the NULL entries, just use an AND clause on the WHERE:
SQL
UPDATE StudentsTbl SET Book_1 = @ID WHERE ID = @ID AND Book_1 IS NULL
 
Share this answer
 
I see the actual question is already solved, but one note about the table structure you're having.

If I understood correctly you're now having a student table containing 5 columns for different books. With relational databases, when you have something that has one-to-many relationship, like one student has many books, you don't create new columns for the books, instead you create a table. In other words a normal approach would be to have the students table and the a book table with columns like
- StudentId
- Book

Now you'd have a table where you can store as many book as you like for each student. Also this approach makes it easier to both query and maintain the data. When you normalize tables like this you also need to get familiar with the concept of joins. If you want to query student info with all the books he has you'd need to join the tables in your query. There are quite many articles explaining joins, here's one for example Visual Representation of SQL Joins[^]

Not knowing what the goals for your assignment are but this is one thing to consider.
 
Share this answer
 

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