Oshrib
Oshrib

Reputation: 1900

Image display by SQL command at c# , asp.net

What is wrong with my code? why the images not shown? i need to assign that method at the asp:image ? if yes - how ?

public int bla()
{
    SqlConnection connection = new SqlConnection("Data Source=tcp:**.****;Initial Catalog=*****;User ID=****;Password=****;Integrated Security=False;");
    string commandtext = "SELECT Minbuy FROM items";
    SqlCommand command = new SqlCommand(commandtext, connection);
    connection.Open();
    int itemsMin = (int)command.ExecuteScalar();

    string commandtext2 = "SELECT purchaseid FROM purchase";
    SqlCommand command2 = new SqlCommand(commandtext2, connection);
    int purchase = (int)command2.ExecuteScalar();

    if (itemsMin >= purchase)
        image3.Visible = true;
    else
        image4.Visible = true;

    connection.Close();



    return itemsMin;




}

the images at the aspx file:

     <asp:Image ID="image3"  runat="server" Visible="false" ImageUrl="~/images/V.png" />

     <asp:Image ID="image4"  runat="server" Visible="false" ImageUrl="~/images/X.png" />

Upvotes: 0

Views: 752

Answers (3)

RubenHerman
RubenHerman

Reputation: 1854

Try this on you're method:

public int bla()
{
   try{
     //your code
   }catch(Exception ex){
      System.Diagnostics.Debugger.Break();
      Console.WriteLine(ex.Message);
      return -1;
   }
}

When the debugger starts, you've got an error in your code. Hover over ex (in visual studio) to see more information about the error

Upvotes: 0

Matt
Matt

Reputation: 498

It's pretty hard to say without knowing what the data is!

A wild guess would be that an exception is thrown before the line if (itemsMin >= purchase).

My first step would be to log the output of each sql statement, and also add logging if an exception is thrown in a try catch finally block.

For example:

    public int bla()
    {
        int itemsMin = -1;
        try
        {
            SqlConnection connection = new SqlConnection("Data Source=tcp:**.****;Initial Catalog=*****;User ID=****;Password=****;Integrated Security=False;");
            string commandtext = "SELECT Minbuy FROM items";
            SqlCommand command = new SqlCommand(commandtext, connection);
            connection.Open();
            itemsMin = (int)command.ExecuteScalar();

            string commandtext2 = "SELECT purchaseid FROM purchase";
            SqlCommand command2 = new SqlCommand(commandtext2, connection);
            int purchase = (int)command2.ExecuteScalar();

            if (itemsMin >= purchase)
                image3.Visible = true;
            else
                image4.Visible = true;

            connection.Close();
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Trace.WriteLine(ex);
        }
        return itemsMin;
    }

Upvotes: 1

DeveloperX
DeveloperX

Reputation: 4683

at firts make images visibility to true and check is it visible in normal form if its is ,make visibility to true by hard coding not by select from sql,and if this step passed may be the sql returns bad value

Upvotes: 0

Related Questions