Database Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the error.

Any help really appreciated.

Thanks

Greg
 
Greg said:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


Chris Hyde said:
Greg said:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
Greg said:
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks

What is the datatype of the Number column?
 
What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


Chris Hyde said:
Greg said:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
Dear All,

The datatype in the table is long int - which is fine. The error is at
runtime: Syntax error in INSERT INTO statement.

Thanks

Greg

John Wood said:
What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


Chris Hyde said:
Greg Horwood wrote:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
Then you need to look at the InsertCommand you have assigned to the adapter.
Are you using the command builder? Can you show us that code too?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Dear All,

The datatype in the table is long int - which is fine. The error is at
runtime: Syntax error in INSERT INTO statement.

Thanks

Greg

John Wood said:
What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


:

Greg Horwood wrote:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get
the
error.
Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
John,

I've taken the following connection strings straight from a self-help book.
(I'm new to C#)

OleDbConnection m_cnADONetConnection = new OleDbConnection();
OleDbDataAdapter m_daDataAdapter = newOleDbDataAdapter();
DataTable m_table = new DataTable();

The connection is made successfully. I can scroll through the datatable -
just can't add anything with this error.

Thanks

Greg

John Wood said:
Then you need to look at the InsertCommand you have assigned to the adapter.
Are you using the command builder? Can you show us that code too?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Dear All,

The datatype in the table is long int - which is fine. The error is at
runtime: Syntax error in INSERT INTO statement.

Thanks

Greg

John Wood said:
What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


:

Greg Horwood wrote:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get the
error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype, this
should work.

HTH...

Chris
 
Greg,
Most of the time when I've seen this error in OleDb programs it resolves
to having a database field name that is a reserved keyword that needs to be
quoted.
You can quote this with '[' and ']' surrounding the field name if you
are hand generating it or by setting the QuotePrefix and QuoteSuffix
properties of a CommandBuilder if you are using one.

Ron Allen

Greg Horwood said:
Dear All,

The datatype in the table is long int - which is fine. The error is at
runtime: Syntax error in INSERT INTO statement.

Thanks

Greg

John Wood said:
What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/


Greg Horwood said:
Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


:

Greg Horwood wrote:
Dear All,

Can anyone suggest why the following code returns an error:

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = "222";
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

If the column "Number" is converted to a text field, I don't get
the error.

Any help really appreciated.

Thanks

Greg

You are trying to assign a string to what appears to be an int (or
numeric) field. that's why when you change the column datatype to
string, it works.

Replace your line with this:

drNewRow["Number"] = 222;

As long as the Number column is an int (or other numeric) datatype,
this
should work.

HTH...

Chris
 
Greg,

Ron is correct, it looks like you're using a reserved word, "number" for
the field name.

It is best to avoid reserved words for names of tables and columns in
databases as it makes maintenance and troubleshooting that much more
difficult.

Ron said:
Greg,
Most of the time when I've seen this error in OleDb programs it resolves
to having a database field name that is a reserved keyword that needs to be
quoted.
You can quote this with '[' and ']' surrounding the field name if you
are hand generating it or by setting the QuotePrefix and QuoteSuffix
properties of a CommandBuilder if you are using one.

Ron Allen

Dear All,

The datatype in the table is long int - which is fine. The error is at
runtime: Syntax error in INSERT INTO statement.

Thanks

Greg

:

What *is* the error you're getting?
Is it a compilation error or runtime exception?
When does it occur?

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/



Sorry, the code snippet should have been

DataRow drNewRow = m_table.NewRow();
drNewRow["Number"] = 222;
m_table.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_table);

I presented a string to determine that the same code would work with a
string and not an int.

Thanks


:


Greg Horwood wrote:
....snip...
 
Back
Top