Monday, February 12, 2018

c# - Unknown column 'count' in 'field list'




I try to make a login form in C# with MySQL database but i get this error:
Unknown column 'count' in 'field list'. What can I do?



Image with error:



enter image description here



Here is my code:



using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ScoalaIT

{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void pictureBox1_Click(object sender, EventArgs e)
{


}

private void button1_Click(object sender, EventArgs e)
{
string myConnection = "server=localhost;database=test;uid=root;password=123456";
MySqlConnection connection = new MySqlConnection(myConnection);


//string StrQuery1 = "Select count from users where user='" + txtUser.Text + "' and parola='" + txtParola.Text + "'";

MySqlCommand myCommand = new MySqlCommand("Select count from users where user='" + txtUser.Text + "' and parola='" + txtParola.Text + "'", connection);
//MySqlDataAdapter MyAdapter = new MySqlDataAdapter(myCommand);
MySqlDataReader myReader;
connection.Open();
myReader = myCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count++;
}

if (count == 1)
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("Nu ok");
}
connection.Close();
}

}
}


Thanks!!!


Answer



Change this...



"Select count from 



to this...



"Select count(*) from


and then read this: http://en.wikipedia.org/wiki/SQL_injection becuase you're sure to suffer from it.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...