Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After login,i want to change the label.Text to tom jerry,
but the output shows m_fnm_sn the two column names instead of the exact value that i wanted to show.

Thanks for the help, here are the table and code.

m_ac m_pw m_fn m_ln
test test tom jerry


C#
private void Login_Button_Click(object sender, EventArgs e)
             {
MySqlConnection conn = new MySqlConnection(connectionString);
                 conn.Open();

                 MySqlCommand cmd = new MySqlCommand("select 'm_ac','m_pw','m_fn','m_ln' from member where m_ac='" + username_textBox.Text + "' and m_pw='" + password_textBox.Text + "'", conn);
                 MySqlDataReader dr;
                 dr = cmd.ExecuteReader();
                 while(dr.Read())
                 {
                     string fn = dr.GetString("m_fn");
                     string ln = dr.GetString("m_ln");
                     label1.Text = ac+ln;
                 }
}
Posted
Updated 26-Mar-15 21:42pm
Comments
Richard Deeming 26-Mar-15 16:26pm    
As mentioned in Solution #1, your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

You also appear to be storing passwords in plain text. That's an extremely bad idea. You should only ever store a salted hash of the user's password:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

1 solution

In MySQL object names (table names, column names...) are quoted with backticks: `

You're using single quotes, so you're actually selecting string constants instead of columns.

So do this instead:

SQL
select `m_ac`,`m_pw`,`m_fn`,`m_ln`  ...


Additional suggestions:
- Use SQL-Parameters instead of concatenating your query-string
- Use intuitive column names - the times where column names could only be 8 characters or so are over ;)
- Use using-Blocks for the Connection-, Command and DataReader-Objects
- Use either the Interface-versions or the abstract baseclasses instead of the database-specific classes (e.g. IDbConnection or DbConnection instead of MySqlConnection) - which will allow you to easily switch to another database system if required or re-use your code in another project where you'll use another database-system:
C#
using (DbConnection conn = new MySqlConnection(connectionString))
{
    conn.Open();

    using (DbCommand cmd = conn.CreateCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "select ....";

        using (DbDataReader dr = cmd.ExecuteReader())
        {
            // ...
        }
    }
}

In the above example you would only have to make one change to use it for SQL-Server or some other database system. If you would use a DbProviderFactory[^] to create your Db*****-Objects, you wouldn't have to change anything.
Obtaining a DbProviderFactory[^]


Edit after comment:

cmd.CommandText = "select `m_ac`,`m_pw`,`m_fn`,`m_ln` from member where m_ac=`" + username_textBox.Text + "` and m_pw=`" + password_textBox.Text + "`";

An unhandled exception occurred in MySql.Data.dll
Additional information: Unknown column 'test' in 'where clause'


When quoting "things" in a query string you have to differentiate between object names (table names, column names) and string literals (your TextBox-inputs). Object names are quoted with backticks in MySQL, string literals are quoted with single quotes. Object names only have to be quoted if they otherwise would collide with a MySQL-reserved-keyword (e.g. a table or column named "select" or "from"). String literals have to be quoted always.

You quoted your TextBox-inputs with backticks, so MySQL interpreted it as column-names and couldn't find the column named "test".

Your query string-construction can either look like this:
C#
cmd.CommandText = "select `m_ac`, `m_pw`, `m_fn`, `m_ln` from `member` where `m_ac`='" + username_textBox.Text + "' and `m_pw`='" + password_textBox.Text + "'";
or like this:
C#
cmd.CommandText = "select m_ac, m_pw, m_fn, m_ln from member where m_ac='" + username_textBox.Text + "' and m_pw='" + password_textBox.Text + "'";


If you would use SQL-Parameters, it could look like this and you wouldn't have to bother with quoting string literals (on top of other benefits like avoiding the risk of SQL-injection and better readibility):
C#
cmd.CommandText = "select m_ac, m_pw, m_fn, m_ln from member where m_ac=?user and m_pw=?pw";
 
Share this answer
 
v3
Comments
Member 11558504 27-Mar-15 3:49am    
after i changed the ' to a `,
an error pops out.

using (MySqlCommand cmd = conn.CreateCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select `m_ac`,`m_pw`,`m_fn`,`m_ln` from member where m_ac=`" + username_textBox.Text + "` and m_pw=`" + password_textBox.Text + "`";

using (MySqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
string fn = dr.GetString("m_fn");
string sn = dr.GetString("m_ln");
label1.Text = fn + sn;
}


An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: Unknown column 'test' in 'where clause'
Sascha Lefèvre 27-Mar-15 7:39am    
I updated my answer above, please take a look.
Member 11558504 27-Mar-15 12:57pm    
i tried all your answer above, the output is still the two column names m_fnm_ln like that.

I have tried insert sql, it works i can insert data to the database, so i guest the problem is not on the sql.

When im using select sql to do selection of the condition m_ac and m_pw, i can get myself login. But when i tried to use DataReader to get the data from the table, the output is just two column names.

Im using AppServ 2.6.0, so i wondering if it is the problem cause.

Sorry for bothering too much.
Sascha Lefèvre 27-Mar-15 13:12pm    
"When im using select sql to do selection of the condition m_ac and m_pw, i can get myself login"
- What do you mean by that exactly? Please post the code doing that.

- Do you have MySQL Workbench installed? (If not, get it, it's free.) Please take a look at your table and verify that there is *not* a record that contains the values "m_fn" and "m_ln" as *values* in these columns.

- AppServ 2.6.0 won't be the issue.

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