Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column in where clause in C# app

Tags:

c#

mysql

I am trying to develop C# app where I want to have login form connected to remote server. I connected to the server but when I try to login, the line : MySqlDataReader reader = cmd.ExecuteReader(); is giving me an error: Unknown column "admin" in where clause Do you have any idea from where can come the problem? 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 ECBSRecruitmentAgencySoftware
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }  

        public bool tryLogin(string username , string password)
        {
             MySqlConnection con = new MySqlConnection("host=aaaaaaaa.baaadsg;user=saaaaaak;password=2333333336;database=soaaaaaaaa2;");
             MySqlCommand cmd = new MySqlCommand("Select * FROM niki WHERE user_name = `" + username + "` AND user_password = `" + password + "`;");
             cmd.Connection = con;
             con.Open();
             MySqlDataReader reader = cmd.ExecuteReader();
             if (reader.Read() != false)
             {
                 if (reader.IsDBNull(0) == true)
                 {
                     cmd.Connection.Close();
                     reader.Dispose();
                     cmd.Dispose();
                     return false;
                 }
                 else
                 {
                     cmd.Connection.Close();
                     reader.Dispose();
                     cmd.Dispose();
                     return true;
                  }
             }
             else 
             {
                 return false;
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tryLogin(user.Text, pass.Text) == true)
            {
                MainScreen F2 = new MainScreen();
                F2.Show();
                this.Hide();
            }            
            else 
                MessageBox.Show("Wrong details!");             
        } 
    }
 }
like image 826
Nikolay Dyankov Avatar asked Feb 20 '23 11:02

Nikolay Dyankov


1 Answers

In your query quote string using ' instead of ( ` )

Select * FROM niki WHERE user_name = '" + username + "' AND user_password = '" + password + "'
like image 78
bitoshi.n Avatar answered Mar 03 '23 07:03

bitoshi.n