简体   繁体   中英

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" />

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM