0

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" />
10
  • 1
    When is this code started? With pressing a button? Or load event,... (or other) Commented Jun 9, 2011 at 11:37
  • And are the url's from the images correct? Commented Jun 9, 2011 at 11:42
  • Yes, they are correct. maybe the whole code is wrong (i don't get errors). what i'm trying to do is to write IF statement on sql table on c# codebhind.. but i dont know how to do that, and if my code is the right way. Commented Jun 9, 2011 at 11:43
  • They are 2 different tables on the same database. Commented Jun 9, 2011 at 11:44
  • 1
    "SELECT Minbuy FROM items", did u forgot adding a where clause?? did u try executing the query in sql mgmt studio? I believe the query should return only one record as you are using Executescalar, Cheers! Commented Jun 9, 2011 at 11:44

3 Answers 3

1

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;
    }
Sign up to request clarification or add additional context in comments.

8 Comments

I wrote this code without trully knowladge. as you can see i'm trying to write IF staetment on SQL tables at c#... and i don't know how to do that...
I will be glad if you can give me an example for if statement on sql tables (2 different tables on the same database... let's call for one table : bla and for the second bla2 ... i need to chek one column at bla against one column at bla2.
You dont think that i need to assign this method at the image:asp ? maybe ondatatbinding event?
Is there more than one item in each table?
Also try to perfect your queries in SQL Management Studio's "New Query" window. It will help you get the SQL syntax correct, and show you what results are being returned.
|
0

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

2 Comments

I've triyed to do int a = 5 int b = 6 ... if (a<b) imag3.Visble = true ... and yep, the image display. so the problem is on the sql command... the question how to fix that...
put a breakpoint on start of function and check variables value step by step
0

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

4 Comments

Hi, thank you. what i need to write on the ex ? i got error on the VWD : The variable 'ex' is decleard but never used...
anyway... i wrote what you gave me here and i got on the page error: Compiler Error Message: CS0161: 'DMasterPage.bla()': not all code paths return a value
In catch block write: Console.WriteLine(ex.Message); return 0;
I've edited the code. It's just because not all paths return a value.

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.