简体   繁体   English

通过 SQL 命令在 c#、asp.net 显示图像

[英]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?我需要在 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: aspx 文件中的图像:

     <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) .一个疯狂的猜测是在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.我的第一步是记录每个 sql 语句的 output,如果在try catch finally块中抛出异常,还添加日志记录。

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 Hover over ex(在 Visual Studio 中)查看有关错误的更多信息

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首先使图像可见性为真并检查它是否以正常形式可见,如果它是,则通过硬编码而不是通过 sql 中的 select 使可见性变为真,如果此步骤通过可能是 sql 返回错误值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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