0

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?

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by pikkas
0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.