hi, i m trying to make something like a login page with C# using VS 2010 and SQL server database. I am using the following code to get the password from the appropriate username but i get an error of Object reference not set to an instance of an object.
. here is the code:

String username = txtusername.Text;
        String password = txtpass.Text;

        String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.Connection = conn;
        cmd.CommandText = "Select password from Details WHERE username=' " + username + "'";
       
        String pass=cmd.ExecuteScalar().ToString();
        Console.WriteLine(pass);
        conn.Close();

Any help please?

I THINK U HAV TO CHANGE THE CODE AS FOLLOWS
FROM 5TH LINE

SqlConnection conn=new SqlConnection(connStr);
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandText="select password from Details WHERE username=' " + username + " ' ";
conn.Open();
String pass=cmd.ExecuteScalar().ToString();
Console.WriteLine(pass);
conn.Close();

please enclose the full files (including project ), then it will be helpful to find the error where it is

i get the same error

I THINK U HAV TO CHANGE THE CODE AS FOLLOWS
FROM 5TH LINE

SqlConnection conn=new SqlConnection(connStr);
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandText="select password from Details WHERE username=' " + username + " ' ";
conn.Open();
String pass=cmd.ExecuteScalar().ToString();
Console.WriteLine(pass);
conn.Close();

please enclose the full files (including project ), then it will be helpful to find the error where it is

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.