Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to MS Access using C#

Status
Not open for further replies.

liamba

Technical User
Jan 6, 2007
21
IE
hi,
im trying to write to a ms access database using c#,however i keep running in to problems.
One is
A field initializer cannot reference the nonstatic field, method, or property 'Register.objConnection'

the other
A field initializer cannot reference the nonstatic field, method, or property 'Register.objMemberDA'

the code i am using is

OleDbConnection objConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0.....);
OleDbDataAdapter objMemberDA = new OleDbDataAdapter("select * from tblMember", objConnection);
OleDbCommandBuilder objMemberCB = new OleDbCommandBuilder(objMemberDA);
DataSet objDataSet = new DataSet();

protected void btnSubmit_Click(object sender, EventArgs e)
{
DataRow objRow;

objRow=objDataSet.Tables.Add("tblMember").NewRow();

objRow["Name"]= txtMemberName.Text;
objRow["Address1"] = txtAddress1.Text;
objRow["Address2"] = txtAddress2.Text;
objRow["Address3"] = txtAddress3.Text;
objRow["Telephone"] = txtTelephone.Text;
objRow["Mobile"] = txtMobile.Text;
objRow["Password"] = txtPassword.Text;
objRow["ConfirmPassword"] = txtConPassword.Text;

objDataSet.Tables.Add(objRow);
objMemberDA.Update(objDataSet, "tblMember");}

Thank you for your help
 
My work with C# has been somewhat limited, but I think that you might need to specify which table you are adding the row to? I don't have C# on this PC so cannot test at the moment, but
Code:
objDataSet.Tables.Add(objRow);
looks off to me.

Also, to format your code try using these tags [ignore]
Code:
 type some code here
[/ignore] to make your code more readable.

Good Luck,

Alex

Ignorance of certain subjects is a great part of wisdom
 
You do need to specify which table you add a row to, like
Code:
objDataSet.Tables[0].Add(objRow);

also, don't forget to call the AcceptChanges method of the dataset, like
Code:
objDataSet.AcceptChanges();
But I'm not sure if this is required to propagate the changes (added rows etc.) to the dataAdapter.

Cheers,
Marco

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top