Fetch row and Save in Variable

261 views Asked by At

I am writing a WPF C# application and I want to save fetched row in a variable.

SqlDataAdapter connAdp = new SqlDataAdapter("SELECT  * FROM users WHERE (username = '"+uname+"') AND (password = '"+pass+"');",conn);
SqlDataReader fetchInfo = null;
fetchInfo = conn.ExecuteReader();
fetchInfo.Read();

But when I try to build my application it gives error

ExecuteReader not defined

I tried this code also:

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow row = dt.Rows["username"];

But it gives error too. I am new to C# and WPF, so can anyone tell how I fetch data from SQL Server and store in a variable or array?

2

There are 2 answers

1
Burhan aka SLIM SHADY On

I sort it out myself

SqlCommand cmd = new SqlCommand("SELECT  * FROM users WHERE (username = '" + uname + "') AND (password = '" + pass + "');", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        SqlDataReader dr = cmd.ExecuteReader();

Here it is guys, i hope it help noobs. You can fetch it with dr.Read()

1
KARAN On

It is always SqlCommand.ExecuteReader not Sqlconnection.ExecuteReader.

    SqlCommand cmd= new SqlCommand("SELECT  * FROM users WHERE (username = '"+uname+"') AND (password = '"+pass+"');",conn);
conn.open();
            SqlDatareader dr = cmd.ExecuteReader();
While(dr.Read())
{
//Fecth your values from reader and store it in your variable
}
conn.close();