2

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.

4
  • command2.ExecuteReader() and gether info in the reader. Commented Sep 8, 2011 at 9:55
  • 1
    I think at first you need to read some basic articles how to work with c# and sql server...article from quick search Commented Sep 8, 2011 at 10:05
  • What do you want to display? ExecuteScalar's description explicitly states that it returns the first column of the first row of the result set. Commented Sep 8, 2011 at 12:36
  • Possible duplicate of How to populate more than one column using executescalar? Commented Nov 23, 2015 at 23:23

2 Answers 2

3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

2

Here is the explanation of ExecuteScalar() method. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.71%29.aspx

"Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored."

Also, SELECT * will fetch all the columns. You probably want to display multiple values for single column. Then select the column name in select statement.

SELECT xyzColumn FROM Machreta WHERE noImage = 1

Lastly, you can assign only one string to label.text. So, you will have to concatenate all these strings (multiple values for single column) and then assign it to label text. Use a reader and ExecuteReader() method instead of ExuecuteScalar().

Comments

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.