Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys I am trying to fill data from sql and show it on button text I don't see what I did wrong can you take a look

What I have tried:

SqlConnection CON = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=.mdf;Integrated Security=True;Connect Timeout=30");
            DataTable DTA = new DataTable();
            DataSet ds = new DataSet();
            SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM '"+ button1.Text +"'", CON);
            SDA.Fill(DTA);

            ds.Tables.Add(DTA);
            foreach (DataRow dr in DTA.Rows)
            {
                panel1.Dock = DockStyle.Left;
                Button btn = new Button();
                btn.Name = dr[1].ToString();
                btn.Padding = new Padding(20, 3, 20, 3);
                btn.FlatStyle = FlatStyle.Flat;
                btn.ForeColor = Color.White;
                btn.BackColor = Color.RoyalBlue;
                btn.Text = dr[1].ToString();
                btn.Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold);
                btn.Size = new Size(200, 85);
                btn.Location = new Point(516, 68);
                panel1.Controls.Add(btn);
        }
Posted
Updated 1-Apr-21 5:54am
Comments
Richard MacCutchan 1-Apr-21 7:39am    
Where is the line that produced the error message, and what is the text of button1?
Kleo Rogers 1-Apr-21 7:44am    
Text of the button is earth + $exception {"Incorrect syntax near 'earth'."} System.Data.SqlClient.SqlException
Richard MacCutchan 1-Apr-21 7:53am    
Well some part of the generated SQL statement (near the word "earth") is invalid. Use your debugger to find out exactly what part it is.

Also, pay particular attention to all the comments in the post below from OriginalGriff.
Maciej Los 1-Apr-21 8:01am    
Do you want to:
- get data from 'earth' table
or
- get data from a table - 'earth' column?
Kleo Rogers 1-Apr-21 11:17am    
Earth table

Don't do it like that! 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?

Fix that through your whole app - miss one, and you will suffer later - and you will find your problem has gone at the same time.

And BTW: 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...
 
Share this answer
 
Quote:
Incorrect syntax near 'earth'.

Since 'earth' is not part of your code, this means that the user input is promoted to code, that is SQL Injection, and it is bad.
C#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM '"+ button1.Text +"'", CON);

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[^]
 
Share this answer
 
In amongst all this talk of SQL injection I can't see that anyone has pointed out that you are trying to SELECT FROM a-text-value because of the single quotes in your code ...
C#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM '"+ button1.Text +"'", CON);
becomes
SQL
SELECT * FROM 'earth'
So the simplest solution is just get rid of those single quotes
C#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM "+ button1.Text, CON);
Of course if you had used a parameterised query you probably would not have come across that problem in the first place (which is another reason why parameterised queries should be used!)
 
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