0

I have this table called WeeklySales with 2 columns, DateandTime and Sales. Now, I have 3 textbox on my form. I wanted to get the latest value that was added on the table so I have this string.

    string sql = "SELECT Sales FROM database.weeklysales ORDER BY DateandTime DESC LIMIT 3";

Now, I have this database(lets say that month is the date),

 DateandTime | Sales
 March       | $300
 February    | $500
 January     | $400

and get this result with that string:

 Sales
 $300
 $500
 $400

Now, I wanted to put the first row into first textbox, then second row to second textbox and so on... Now, I do not know what to out in inside the Datareader...

        try
        {
            con.Open();
            using (reader = cmd.ExecuteReader())
            {
               first.Text = ?
               second.Text = ?
               third.Text = ?
            }
        }
        finally
        {
            con.Close();
        }

I have searched but they only get the first row unfortunately.

3
  • 1
    There are plenty of places that you can learn how to use a data reader properly. I suggest that you do that. Commented Feb 16, 2018 at 9:20
  • 1
    I've search thoroughly but can't get the right result. If I've known what to put there, I wouldn't ask here. Commented Feb 16, 2018 at 9:21
  • stackoverflow.com/questions/4018114/… it can helps Commented Feb 16, 2018 at 9:24

4 Answers 4

2

Since you only have 3 text boxes to fill - no loop just advance the reader manually.

MySqlDataReader dr = cmd.ExecuteReader();
dr.Read();
first.Text = dr.GetValue(0).ToString();
dr.Read();
second.Text = dr.GetValue(0).ToString();
dr.Read();
third.Text = dr.GetValue(0).ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I solved the problem. But what if I want to put all the three into one richtextbox (with every data comes into one line)? I know I would use loop but what should I replace in dr.GetValue(0).ToString();
@Mamayg I think you should ask a new question showing what you have tried and how it didn't work.
2

The SqlDataReader class has a Read() method, which returns a bool as long as there are more rows to read. You can use it to read multiple rows using a while loop for example.

using (SqlDataReader reader = cmd.ExecuteReader()
{
   while (reader.Read())
   {
      //Do your stuff here
   }
}

See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read%28v=vs.110%29.aspx for further information

1 Comment

Thanks. I forgot to include that... But the thing I need the most is what should I put in first.Text = ?.... I always to reader.GetValue(0).ToString(); but it is for rows, no?
1

Following code will be helpful to you,

using (reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {   
       int i = 1;                    
       while (reader.Read())
       {
          switch (i)
          {
             case 1:
             first.Text = reader["Sales"].ToString();
             break;
             case 2:
             second.Text = reader["Sales"].ToString();
             break;
             default:
             third.Text = reader["Sales"].ToString();
             break;             
          } 
          i += 1;         
       }
     }
}

1 Comment

Thank you but it still only shows the last (rather than the first) instead of all the values. Anyways, I solved my problem through the answer above. Thank you again,
0

Avoiding repetitions or in case of multiple objects

public void Example(MySqlDataReader dr)
        {
            TextBox a = new TextBox();
            TextBox b = new TextBox();
            TextBox c = new TextBox();

            foreach(TextBox current in new List<TextBox> { a, b, c })
            {
                dr.Read();
                current.Text = dr.GetValue(0).ToString();
            }
        }

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.