I've tried a lot of things - I'm having difficulty trying to get any modifications or deletions made in the datagridview1 control to stick. As a result my code might look a bit messy but it's all here. For reference, button3 is supposed to be the save button, button2 is the refresh button, and button1 is the submit button. This is Windows Forms in C# and I am editing on MS VS 2013. I have several tabs here, two are relevant. On the audit tab is a BindingNavigator, the DataGridView, button2 (Refresh) and button 3 (Save). On the Add Entry tab, there are several field box controls, a datetime control, and button1 (Submit entry). Of all of the functions, the ones mapped to button2 and button1 work flawlessly. You can refresh, you can add new rows. You cannot, however, save any changes you make in DataGridView1, and that's my problem. Here's the code, please help me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace LicenseInventoryManager
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds;
SqlConnection con;
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
private void MainWindow_Load(object sender, EventArgs e)
{
this.lIMTableTableAdapter.Fill(this.licenseInventoryMgrDataSet.LIMTable);
dataGridView1.DataSource = lIMTableBindingSource;
GetData("SELECT * FROM LIMTable");
}
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=data0;Initial Catalog=LicenseInventoryMgr;Integrated Security=True;Connect Timeout=0;Trusted_Connection=Yes");
da = new SqlDataAdapter("insert into LIMTable(Software,Host,AssetTag,ActivationDate,LicenseNumber)values('"+comboBox1.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "','" + textBox3.Text + "')", con);
ds = new DataSet();
da.Fill(ds);
MessageBox.Show("Entry was successfully submitted.");
}
private void button2_Click(object sender, EventArgs e)
{
GetData(dataAdapter.SelectCommand.CommandText);
}
private void lIMTableBindingSource_CurrentChanged(object sender, EventArgs e)
{
DataRow ThisDataRow =
((DataRowView)((BindingSource)sender).Current).Row;
if (ThisDataRow.RowState == DataRowState.Modified)
{
lIMTableTableAdapter.Update(ThisDataRow);
}
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=data0;Initial Catalog=LicenseInventoryMgr;Integrated Security=True");
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from LIMTable", con);
SqlCommandBuilder build = new SqlCommandBuilder(adp);
adp.Update(ds.Tables[0]);
con.Close();
}
private void GetData(string selectCommand)
{
try
{
String connectionString =
"Integrated Security=SSPI;" +
"Initial Catalog= LicenseInventoryMgr ;Data Source=data0";
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
lIMTableBindingSource.DataSource = table;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}