0

I have a form that contains buttons that refer to tables and the buttons represent tables. The default button image is free and I want the button image to change according to value in database table "free" or "busy" and I know there's a mistake in my code.

How to get this to work?

EDIT :

I have replaced the custom buttons with default stock buttons now it gives nothing and I checked the result of the query and it's correct but nothing changes and no errors.

My table is as follows:

| tablenum | tabestatusint |
|----------|---------------|
| 1        | 0             |
| 2        | 1             |
| 3        | 1             |

So if tabestatusint is 0 it should change the image

Here's what I have tried :

public void checkSuites()
{
  Dictionary<int, Control> btnList = new Dictionary<int, Control>();
  btnList.Add(1, Button1);
  btnList.Add(2, Button2);

  SqlCommand checkSuite = new SqlCommand(
                          "SELECT tablestatusint FROM tablesstatustbl", cn);
  SqlDataReader readSuite = checkSuite.ExecuteReader();
  while (readSuite.Read())
  {
    int suiteIndex = Convert.ToInt32(readSuite["tblstatusint"]);
    string suitePath = "tblstatusint" + suiteIndex;
    foreach (Button key in btnList.Values)
    {
      if (key.Name == suitePath)
      {
        key.Image = My_Café_Manger.Properties.Resources.tablesbusy;
      }
    }
  }
}
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
Ahmed Zoeil
  • 47
  • 1
  • 8

1 Answers1

1

Firstly you don't need to iterate the dictionary to access the correct button. That is the point of a dictionary. Secondly calling your current element 'key' is confusing as it implies it is the dictionary key which it isn't. Thirdly you are comparing the name of your button (which I can't see that you ever set) against the string "tblstatusintX" where X is the value from the database. Fourthly suiteIndex is badly named as I think it's the true/false status value rather than an index. Fifthly I think you have typo in your 2nd column name which I reckon should be 'tablestatusint'. Note the missing letter 'l' in your question. Finally you are also missing the tablenum column from your SQL query.

I think you need something like this:

SqlCommand checkSuite = new SqlCommand("SELECT tablenum, tablestatusint FROM tablesstatustbl", cn);
SqlDataReader readSuite = checkSuite.ExecuteReader();
while (readSuite.Read())
{
  int status = Convert.ToInt32(readSuite["tblstatusint"]);
  if (status == 1)
  {
    // I have separated the dictionary access and image assignment into two lines for readability
    int buttonNum = ConvertToInt32(readSuite["tableNum"]);
    btnList[buttonNum].Image = My_Café_Manger.Properties.Resources.tablesbusy;
  }
}

This is very much "aircode" but it should get you going again. You might also like to review this question.

Community
  • 1
  • 1
Caltor
  • 2,538
  • 1
  • 27
  • 55
  • first of all thank u so much for your answer and your time i used dictionary for better performance as i might have many buttons .. i still have red underline under : readSuite["tableNum"] says : cannot convert from object to int – Ahmed Zoeil Apr 26 '17 at 15:55
  • @Ahmed_Zoeil yes I realised afterwards why you're using dictionary and revised my answer. Try 'readSuite["tableNum"].Value' – Caltor Apr 26 '17 at 17:59
  • i stil get red underline error under .'.value' says : object does not contain a definition for value and no extension for method value accepting a first argument of object could be found – Ahmed Zoeil Apr 26 '17 at 22:39
  • @AhmedZoeil You aren't including the tableNum column in your SQL query. You need to change it to `SELECT tableNum, tablestatusint FROM tablesstatustbl` or `SELECT * FROM tablesstatustbl`. – Caltor Apr 27 '17 at 07:55
  • @AhmedZoeil I've rewritten the whole snippet for you now so hopefully it should work and be easier to read. – Caltor Apr 27 '17 at 08:05
  • @AhmedZoeil forget `.Value`. You just need to `ConvertToInt32` or cast the column to the correct data type. I've updated the answer accordingly. If you ever need to read a string you can use `readSuite["columnName"].ToString()` though. – Caltor Apr 27 '17 at 08:12