Oshrib Oshrib - 7 months ago 17
SQL Question

Return multiple values from sql to label

I have the label:

<asp:Label ID="lbl1" runat="server"></asp:Label>


Code behind:

protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text = ImageCheck().ToString();
}


And:

protected int ImageCheck()
{
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string CommandText2 = "SELECT * FROM Machreta WHERE noImage = 1";
SqlCommand command2 = new SqlCommand(CommandText2, connection);
connection.Open();
int check = (int)command2.ExecuteScalar();
connection.Close();

return check;
}


How can i return multiple values? That label display only single value but there are 6 more in the table.

Answer

try this:

    protected string ImageCheck()
    {

      var result = new StringBuilder();

    using(var connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
    {
        string CommandText2 = "SELECT * FROM Machreta WHERE noImage = 1";
        SqlCommand command2 = new SqlCommand(CommandText2, connection);
        connection.Open();

      using(var reader = command2.ExecuteReader())
      {
        while (reader.Read())
        {
          result.Append(reader.GetString(0));
        }
      }

      return result.ToString();

    }
 }

of course is only an example and not fully solving your issue but should be a starting point :)

Comments