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;
            }

        }
    }
}

I should remove line 33. It is on line 38.

Strictly speaking you should put it in a using statement too :)

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".

The statement will not be executed when i remove line 38

You should do what ddanbe says and remove line 33 not line 38...

Followed Momerath's advice n it worked... Thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.