Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir/Madam

I have created a C# Project with couple of Forms inside this project. In one particular form (lets say Form1) I have 10 Buttons. I also created a Access 2007 DataBase and inside this database I have a table with 10 records.

Now my question is:

Is there a possibility to call these 10 database records dynamically from Form1 (but not one-by-one). For instance I know how to call a db record when clicking (lets say Button1) e.g.

C#
Button1_Click(object sender, EventArgs e)
{
    //to do database stuff.....
}


but this is not the case I want.

Basically what I want is, when Form1 Loads (where inside this form are 10 buttons) to create an SQL Query, get those 10 records from database, and each button I click I want that db record to match the corresponding button and display that record on my list view.


Kind Regards


Agron
Posted
Updated 9-Dec-10 14:57pm
v3
Comments
Henry Minute 10-Dec-10 7:01am    
If you follow the link on 'parameterized query' you will see a code example.

1 solution

One way is to load the data into a DataTable/DataSet (you say you know how to do this) in the Form.Load event.

Then, still in Form.Load, get the value you want to use, ID say or Name, from each record and use that value as the Text of each button. Then dispose of the DataTable/DataSet.

Then when a button is clicked use a parameterized query[^] (scroll down till you hit the Select section.)

The main difference in your situation is that you would use
C#
cmd.Parameters.AddWithValue("ID", Button.Text);


Hope this helps. :)
 
Share this answer
 
v2
Comments
LAPEC 9-Dec-10 21:31pm    
Dear Henry

Thanks for the answer to my question, but I think you lost me with the parameterized query, could you please explain me with a bit of sample code if possible...

thanks in advance

Agron

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900