-1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

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()

  • You should use the [using statement](http://www.hanselman.com/blog/WhyTheUsingStatementIsBetterThanASharpStickInTheEyeAndASqlConnectionRefactoringExample.aspx). – BCdotWEB Nov 18 '14 at 15:16
0

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();
KARAN
  • 1
  • 4