I keep getting "The ConnectionStrin g property has not been initialized." on the line da.Update(ds1. "Mail")
I've tried wrapping the section in a open and close (data base connection) and now I get the following error message: Syntax error in INSERT INTO statement. on this segment of code: da.Update(ds1, "Mail");
I put in a break point and checked and the DataSet has been updated with the new information
I'm able to read from the database, just can't add a new row to it...
Using C# 2005
I've tried wrapping the section in a open and close (data base connection) and now I get the following error message: Syntax error in INSERT INTO statement. on this segment of code: da.Update(ds1, "Mail");
I put in a break point and checked and the DataSet has been updated with the new information
I'm able to read from the database, just can't add a new row to it...
Using C# 2005
Code:
System.Data.OleDb.OleDbConnection con;
DataSet ds1;
System.Data.OleDb.OleDbDataAdapter da;
int intMaxRows = 0;
int intCurrentRow = 0;
private void Form1_Load(object sender, EventArgs e)
{
con = new System.Data.OleDb.OleDbConnection();
ds1 = new DataSet();
openConnection();
string sql = "Select * From EmailLog";
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
da.Fill(ds1, "Mail");
NavigateRecords();
intMaxRows = ds1.Tables["Mail"].Rows.Count;
closeConnection();
}
private void NavigateRecords()
{
DataRow dRow = ds1.Tables["Mail"].Rows[intCurrentRow];
txtDate.Text = dRow.ItemArray.GetValue(1).ToString();
txtFrom.Text = dRow.ItemArray.GetValue(2).ToString();
txtSubject.Text = dRow.ItemArray.GetValue(3).ToString();
txtEmailMsg.Text = dRow.ItemArray.GetValue(4).ToString();
txtMessageId.Text = dRow.ItemArray.GetValue(5).ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
openConnection();
System.Data.OleDb.OleDbCommandBuilder cb;
cb = new System.Data.OleDb.OleDbCommandBuilder(da);
DataRow dRow = ds1.Tables["Mail"].NewRow();
dRow[1] = txtDate.Text;
dRow[2] = txtFrom.Text;
dRow[3] = txtSubject.Text;
dRow[4] = txtEmailMsg.Text;
dRow[5] = txtMessageId.Text;
ds1.Tables["Mail"].Rows.Add(dRow);
intMaxRows = intMaxRows + 1;
intCurrentRow = intMaxRows - 1;
da.Update(ds1, "Mail"); //Not Working
MessageBox.Show("New Data Added!");
closeConnection();
}
private void openConnection()
{
con.ConnectionString = ("provider=Microsoft.Jet.OLEDB.4.0;" + "data source = C:\\PaymentFileTracker.mdb");
con.Open();
MessageBox.Show("Connection to Data Base Open");
}
private void closeConnection()
{
con.Close();
MessageBox.Show("Connection to Data Base is Closed");
con.Dispose();
}
Comment