Mamayg
Mamayg

Reputation: 45

Get values of a single column in C# mysql and put it in textbox

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.

Upvotes: 0

Views: 7025

Answers (4)

PurpleZool
PurpleZool

Reputation: 1

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

Upvotes: 0

Abhilash Ravindran C K
Abhilash Ravindran C K

Reputation: 1856

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;         
       }
     }
}

Upvotes: 1

Mary
Mary

Reputation: 15081

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

Upvotes: 2

Oerk
Oerk

Reputation: 158

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

Upvotes: 2

Related Questions