simple ado question

  • Thread starter Thread starter kirk g
  • Start date Start date
K

kirk g

When I run the following code:

OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\moneymakin.mdb");

String stmt = "INSERT INTO Person (FirstName,
LastName, Email, LoginID, Password) VALUES (" +
"'" + this.m_perFirstName + "', '" +
this.m_perLastName +
"', '" + this.m_emailAddress + "', '" +
this.m_loginID +
"', '" + this.m_password + "')";

MessageBox.Show(stmt);

//conn.Open();
OleDbCommand perCommand = new OleDbCommand
(stmt, conn);
perCommand.Connection.Open();
perCommand.ExecuteNonQuery();

//OleDbDataReader perReader =
perCommand.ExecuteNonQuery();
//perReader.Close();

I get the following error no matter what:

************** Exception Text **************
System.Data.OleDb.OleDbException: Syntax error in INSERT
INTO statement.
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHand
ling(Int32 hr)
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingle
Result(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText
(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand
(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal
(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at WindowsSample.Person.saveNewData() in c:\Documents
and Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\DataItems\Person.cs:line 192
at WindowsSample.PersonalInfo.btnPersonalSave_Click
(Object sender, EventArgs e) in c:\Documents and
Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\Forms\PersonalInfo.cs:line 326
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp
(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Line 192 refers to the executenonquery() method. I have
tested the query by itself in Access, and it inserts the
value just fine. Does anyone have any idea what could be
wrong? I have tracked it down to the loginID or Password
field, as the query works fine without those two in
there, but I am not sure what the problem is. Any help
would be much appreciated!
Kirk
 
Password is a reserved word in Access and is the most likely culprit It
will run ok within access, but via ADO.NET, reserved words cause all kinds
of problems. The bandaid is to wrap it in bracekts [Password] but you or a
co-worker will invariably forget this sometime in the future. IMHO, get rid
of it b/c it's not worth the maintenance and headaches it will likely cause.

HTH,

Bill
 
That was it...thank you very much for your help. I am
just getting started with .NET, C#, and ADO by doing some
stuff at home, and have been banging my head against a
wall on this one.
Much appreciated!
Kirk

-----Original Message-----
Password is a reserved word in Access and is the most likely culprit It
will run ok within access, but via ADO.NET, reserved words cause all kinds
of problems. The bandaid is to wrap it in bracekts [Password] but you or a
co-worker will invariably forget this sometime in the future. IMHO, get rid
of it b/c it's not worth the maintenance and headaches it will likely cause.

HTH,

Bill
kirk g said:
When I run the following code:

OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\moneymakin.mdb");

String stmt = "INSERT INTO Person (FirstName,
LastName, Email, LoginID, Password) VALUES (" +
"'" + this.m_perFirstName + "', '" +
this.m_perLastName +
"', '" + this.m_emailAddress + "', '" +
this.m_loginID +
"', '" + this.m_password + "')";

MessageBox.Show(stmt);

//conn.Open();
OleDbCommand perCommand = new OleDbCommand
(stmt, conn);
perCommand.Connection.Open();
perCommand.ExecuteNonQuery();

//OleDbDataReader perReader =
perCommand.ExecuteNonQuery();
//perReader.Close();

I get the following error no matter what:

************** Exception Text **************
System.Data.OleDb.OleDbException: Syntax error in INSERT
INTO statement.
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHand
ling(Int32 hr)
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingle
Result(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText
(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand
(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal
(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at WindowsSample.Person.saveNewData() in c:\Documents
and Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\DataItems\Person.cs:line 192
at WindowsSample.PersonalInfo.btnPersonalSave_Click
(Object sender, EventArgs e) in c:\Documents and
Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\Forms\PersonalInfo.cs:line 326
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp
(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Line 192 refers to the executenonquery() method. I have
tested the query by itself in Access, and it inserts the
value just fine. Does anyone have any idea what could be
wrong? I have tracked it down to the loginID or Password
field, as the query works fine without those two in
there, but I am not sure what the problem is. Any help
would be much appreciated!
Kirk


.
 
My pleasure. Keep on trucking though, ADO.NET is a bit confusing at first,
but you'll quickly learn to love it.

Bill
Kirk Gray said:
That was it...thank you very much for your help. I am
just getting started with .NET, C#, and ADO by doing some
stuff at home, and have been banging my head against a
wall on this one.
Much appreciated!
Kirk

-----Original Message-----
Password is a reserved word in Access and is the most likely culprit It
will run ok within access, but via ADO.NET, reserved words cause all kinds
of problems. The bandaid is to wrap it in bracekts [Password] but you or a
co-worker will invariably forget this sometime in the future. IMHO, get rid
of it b/c it's not worth the maintenance and headaches it will likely cause.

HTH,

Bill
kirk g said:
When I run the following code:

OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\moneymakin.mdb");

String stmt = "INSERT INTO Person (FirstName,
LastName, Email, LoginID, Password) VALUES (" +
"'" + this.m_perFirstName + "', '" +
this.m_perLastName +
"', '" + this.m_emailAddress + "', '" +
this.m_loginID +
"', '" + this.m_password + "')";

MessageBox.Show(stmt);

//conn.Open();
OleDbCommand perCommand = new OleDbCommand
(stmt, conn);
perCommand.Connection.Open();
perCommand.ExecuteNonQuery();

//OleDbDataReader perReader =
perCommand.ExecuteNonQuery();
//perReader.Close();

I get the following error no matter what:

************** Exception Text **************
System.Data.OleDb.OleDbException: Syntax error in INSERT
INTO statement.
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHand
ling(Int32 hr)
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingle
Result(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText
(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand
(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal
(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at WindowsSample.Person.saveNewData() in c:\Documents
and Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\DataItems\Person.cs:line 192
at WindowsSample.PersonalInfo.btnPersonalSave_Click
(Object sender, EventArgs e) in c:\Documents and
Settings\Kirk Gray\My Documents\Borland Studio
Projects\WindowsSample\Forms\PersonalInfo.cs:line 326
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp
(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Line 192 refers to the executenonquery() method. I have
tested the query by itself in Access, and it inserts the
value just fine. Does anyone have any idea what could be
wrong? I have tracked it down to the loginID or Password
field, as the query works fine without those two in
there, but I am not sure what the problem is. Any help
would be much appreciated!
Kirk


.
 
Back
Top