0

I am not very confident with the capabilities of SqlDataReader and how it works, would it be possible for me to create something that for each StudentID found corresponding to the SQL query, it displays every ID in a different label? And if so could you please help me out in how I would achieve this.

This is my code that I have so far with the SQL query:

private void SetTestsForm_Load(object sender, EventArgs e)
{
    string y = GlobalVariableClass.Signedinteacher;

    string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
    SqlConnection connect = new SqlConnection(connectionString);
    connect.Open();

    SqlCommand command20 = new SqlCommand(@"SELECT [StudentID] FROM StudentDetails WHERE ([TeacherID] = @signedinteacher)", connect);

    command20.Parameters.AddWithValue("@signedinteacher", y);

    SqlDataReader reader = command20.ExecuteReader(); 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mot375
  • 99
  • 1
  • 13
  • If you find "partial class Employee" (an answer) on this question (url at the end of this comment)....you will find a more "layered" approach to solving your situation. I would google "Layered Application" as you design your software. http://stackoverflow.com/questions/13650443/why-is-datatable-faster-than-datareader – granadaCoder Apr 24 '15 at 13:28

2 Answers2

1

You can use Properties

private int StudentID { get; set; }

And then in your reader

using (SqlConnection con = new SqlConnection("Your Connection String"))
    {
        using (SqlCommand cmd = new SqlCommand("Your Query", con))
        {
            con.Open();
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    StudentID = Convert.ToInt32(reader["Field you want to read here"]);
                    YourLabelId.Text += StudentID;
                }
            }
        }

This is on top of my head so do test it but it should give you a good understanding

Izzy
  • 6,740
  • 7
  • 40
  • 84
  • Okay thank you very much for the help, it works! Now I've come up with an error in a different part of my code :@ – mot375 Apr 24 '15 at 14:18
  • You're welcome! Maybe consider asking another question if the error is not relating to this issue – Izzy Apr 24 '15 at 14:25
0

You can try this.

SqlDataReader oReader = new SqlDataReader();
            while (oReader.Read())
            {
                int oColumIndex = oReader.GetOrdinal("StudentID"); //Retrive the colunm index to get the data

                Label oLabel = new Label() { Text = oReader.GetString(oColumIndex) };//Get the data with the retrived colum index and set as Text of my
                                                                                     //label instance, the you can add this label in any container
            }

You can loan the reader in a DataTable and round the rows of the table retrieving the data of the needed fields.

Regards

  • Seeing UI objects (Label) and a IDataReader in the same code block hurts my eyes. – granadaCoder Apr 24 '15 at 13:25
  • Why ? i undertond tha he want create a label per each row , and I am explainig how read the reader, I'm not making an App I'm just explaining broh. – Francisco Mercedes Apr 24 '15 at 13:29
  • I understand your point. You're giving him a quick-demo. But that was not noted. So the UI mixing with the DB layer code will probably make it into his final solution........because the concept of "layers" was not pointed out to him...and that is the quick-hit-example he/she was given. – granadaCoder Apr 24 '15 at 13:36
  • Hehehe, yes he/she must know that mix those layers is not a good practice. thanks for the comment. – Francisco Mercedes Apr 24 '15 at 13:44