fristomer
Member
- Joined
- Mar 15, 2025
- Messages
- 10
- Programming Experience
- 3-5
I am struggling to get the Database to update after inserting a new row into the table. My question is, what exactly do the two items in the title do? Which do I need to work with to update the database?
(I know this excerpt is very limiting. If anyone wants the full section of code for testing purposes, please let me know.)
(I know this excerpt is very limiting. If anyone wants the full section of code for testing purposes, please let me know.)
Block Sample:
public class Connector {
private SqlConnection Con { get; } = new SqlConnection("Connection String");
private SqlDataAdapters[] Adapters;
private DataSet Data;
public Connector() {
// Build SqlDataAdapters and Data
// Note: All SqlDataAdapters are built with the "SELECT" command.
// Does this matter? Do I need to modify them in the Update call?
}
public void UpdateDatabase(int tableId = 0) {
// Note: This code works great when adding rows to tables in
// another section of the code, but fails during the section in question.
SqlCommandBuilder cb = new SqlCommandBuilder(Adapters[tableId]);
cb.DataAdapter.Update(Data.Tables[tableId]);
}
}
public partial class wdoCompany : Form {
Connector Companies { get; set; }
private int[] ComboArray {get; } = new int[] { 17, 18, 19, 20 }
private void btnSave_Click(object sender, EventArgs e) {
if (RequirementsMet()) {
DataRow row = Companies.Data.Tables[0].NewRow();
row["Company"] = txtCompanyName.Text;
row["Website"] = txtWebsite.Text;
row["CompanyType"] = ComboArray[cboCompanyType.SelectedIndex];
Companies.UpdateDatabase();
}
}
}