Hi, I have ben having problems making an instance of sqldatareader. I keep getting a squigly line under my datareader object with a message saying that unreachable code detected. Here is my code:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Albergo.Classes
{
    class Login
    {
        String username;
        String password;
        String sql;
        Connect connect;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader datareader;

        public Login(String username, String password)
        {
            this.username = username;
            this.password = password;
            Connect connect = new Connect();
            con = new SqlConnection(connect.connectionString);
            sql = "SELECT * FROm users WHERE username = '" + username + "' and password = '" + password;
        }

        public int authenticate()
        {
          connect = new Connect();
          datareader = cmd.ExecuteReader();
            try
            {
                cmd = new SqlCommand(sql, con);
                connect.OpenCon();
                datareader = cmd.ExecuteReader();
                datareader.Read();
                if (datareader.HasRows)
                {
                    return 1;
                    datareader.Close();
                    connect.CloseCon();
                }
                else
                {
                    return 0;
                    datareader.Close();
                    connect.CloseCon();
                }

                datareader.Close();
                connect.CloseCon();
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem while trying to Connect: " + ex.Message);
                return 0;
            }

        }
    }
}

Line 42 you return a value. The method stops then, the lines after it never get executed, they are unreachable code. You do this in several places.

Once you fix that, you'll get a "not all paths return a value".

Edited 3 Years Ago by Momerath

This question has already been answered. Start a new discussion instead.