Reputation: 157
I have 2 buttons that add to a CheckListBox
. The first button adds a Delivery with the customer name, address and arrival Time.
The second button adds a pickup with Delivery Name and Delivery address
I also have a database table called customers with the following columns: ID, Description, Customer Name, Customer Address, Arrival Time, Delivery Name, Deliver Address
I have about 10 records stored in the database at the moment
My question - how do I code it so that when my program starts it loads the records stored in my database into the CheckListBox
and when I add a new delivery or a new pick up it saves it in the Customer table in my database? Also, if I edit or delete within the CheckListBox
I want it to update my database table accordingly.
Upvotes: 0
Views: 3961
Reputation: 1726
From the looks of the video, you're using SQL Server. You'll need to do a few things in order to get your program where you want it. I'll try my best to get you there, with the information provided (this assumes you are learning and will keep things basic):
"How do I code it so that when my program starts it loads the records stored in my database into the CheckListBox"
You'll need to add this using statement at the top of your windows form class:
using System.Data.SqlClient;
Then, in the form_Load event, connect to your database and retrieve the rows from the customer table (not tested):
private void Form1_Load(object sender, EventArgs e)
{
//Setup connection to your database.
SqlConnection myConnection = new SqlConnection("user id=sql_userID;" +
"password=password;server=server_url;" +
"Trusted_Connection=yes;" +
"database=databaseName; " +
"connection timeout=30");
//Open connection.
myConnection.Open();
//Create dataset to store information.
DataSet ds = new DataSet();
//Create command object and adapter to retrieve information.
SqlCommand myCommand = new SqlCommand("SELECT * FROM Customers", myConnection);
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
adapter.Fill(ds);
//Loop through each row and display whichever column you wish to show in the CheckListBox.
foreach (DataRow row in ds.Tables)
checkedListBox1.Items.Add(row["ColumnNameToShow"]);
}
The rest of your question is a bit vague because you don't explain how you are going to save a "new" record (with a button, what data is required, what data is the user actually inputting, input types, etc.) or how you are "deleting" a record. This should get you on the right path though and help get you started.
Upvotes: 2