开发者

assign text value to buttons from database in .net?

I have 30 buttons with nothing in default text value. What i want to do is get the ordered data from database:

Here is the database format:

ID       value                   cat
1       Button name               a
2       Another name              a
3       something                 a
.           .                     a
.           .                     a
.           .                     a
30      Last button               a
1       Button name               b
2       Another name              b
3       something                 b
.           .                     b
.           .                     b
.           .                     b
18      Last button               b

Now i have created a function to grab ID and category to select the specific name using OLEDB for MS ACCESS database.

here is a function:

 private string getItem(int i, string a) {
     //database connection
    // database query and table slection where ID = i and cat = a
    read the data in the selected column
    return the string form of the column
 }

Now whenever a form loads, I've assigned following function:

 button1.Show();
      ...
      ...
  button30.Show();
  button1.Text = getItem(1, a);
  button2.Text = getItem(2, a); 
       ..
       ...
       ...
  button30.text = getItem(30, a);

again there is a se开发者_开发百科parate button to change the category that will change the text value in the button:

button1.Text = getItem(1, b);
  button2.Text = getItem(2, b);
       .
       .
       .
  button18.Text = getItem(18, b);
  // because I have only 18 items I hide all the remaining buttons.
  button19.Hide();
           ...
  button30.Hide();

It works well and easy, but the problem is, it is too slow. Whenever the form loads it takes time to fill all the data to the buttons and also when I click the button to change the category it happens there too.

Is there any method to do this in array or something like that? The above code requires multiple time access to database resulting slow processing.

Another problem is, if i have only 18 or lesser than 30 items in the database table, the pre made buttons will be visible with default values in it.

In the above code I overcome it by using buttonx.Hide() while x is for all the values that doesn't have any values..

These all are manual work and required mass codding.

I will be grateful to know some good method to overcome this problem.


There is lot of messy code there so do these things:

  • First dont have all the butttons available all the time (this will solve the problem of having stray buttons with default text). Instead generate and add them on form from you your code (Form.Controls.Add()).

  • Instead of doing multiple roundtrips. have a DataTable in you code to keep a cache of the table and instead Fetch this DataTable. (If the data is too much volatile then this might not be of use, still instead of query for eachitem query whole Table at once)

  • You can use LINQ to query DataTable to reduce code size. (And make it more maintainable)


As Jeff M pointed out, the first thing you need to do is load all the records using a single query.

Then, create the buttons while iterating through the records.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜