-1

I want to retrieve value /miejsca/, but I don't how it works. For example please show me how selecting value define as variable or how to show it in textbox.

At this point I recieve "System.Data.SqlClient.SqlDataReader" in textbox.

        SqlDataReader reader;
        cn.Open();
        cmd.CommandText = ("SELECT miejsca FROM oferty WHERE oferty.idoferty = @rezerw");
        cmd.Parameters.AddWithValue("@rezerw", rezerw);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = cn;
        reader = cmd.ExecuteReader().Read
        string rs = Convert.ToString(reader);
        TextBox1.Text = rs;//at this point i recieve "System.Data.SqlClient.SqlDataReader" in textbox
        cn.Close();
  • This is pretty basic and well described in countless resources. Try googling with your question title, you'll get what you need. – Szymon Apr 24 '14 at 10:16
  • Have you tried erading a tutorial or - ah - the documentation? I mean "hello, staring at code, i hate documentation" is not good when you are on a level of "tutorial 3: getting value out of a data reader". – TomTom Apr 24 '14 at 10:17
  • http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx – jeroenh Apr 24 '14 at 10:19
  • thx, I don't remember sql class ;) – user3561881 Apr 24 '14 at 10:51

1 Answers1

2

If you are reading a single row and a single column: just use ExecuteScalar():

string rs = (string)cmd.ExecuteScalar();

But to answer your question, the normal usage is:

using(var reader = cmd.ExecuteReader())
{
    while(reader.Read())
    {
        // read a row, for example:
        string foo = reader.GetString(0);
        Console.WriteLine(foo);
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900