C# Insert statement

tmcmaster
tmcmaster used Ask the Experts™
on
I'm having a terrible time attempting to get an INSERT statement to work in ASP.NET usinc C#. I have look everywhere for the answer to no avail.

Ted
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
this tutorial is then what you probably need:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx

apart from that: without getting more precise status of what you have and what you get...

Author

Commented:
I should have given more information.

I am using the OleDB connections and trying to talk Insert into a Foxpro database
.
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
the principle is the same.
use OleDbConnection instead of SqlConnection, and OleDbCommand instead of SqlCommand etc.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
I have done that, but I still get different types of errors . What I would like if possible the syntax for the INSERT string.  If they are identical then I can reseach SQL syntax.

Thanks,

Ted
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
the INSERT syntax purely depends on the database you are using.
if you posted what you tried with the error you got, it would be easier to help with concrete corrections.
I have found that the errors I am getting are because of incorrect Date translations and bool translations.
I'm not sure about how to do them into Foxpro.  I can use DateTime to get the date and the time but I do not know how to convert it to   date ='20090718" is what I need. The errors say incorrect format or invalid field so I have been hacking away and have isolated my problem to those two (2) items.  I can insert into db as long as I exclude the date, time and bool types. I have many manuals in from of me and nowhere in the manuals do I see the conversion.

Thanks,

Ted
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
the best method to avoid data type translations problems is to use bound parameters.
check out OleDbParameter for your OleDbCommand:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/OleDbParameterExample.htm

the relevant part is here:
// specify the sql statement with a place holder for the parameter (?):
      string selstr = "select firstName from Employee where lastname = ?";
 
// fill in the parameter specification: name, type, size:
      OleDbParameter name = cmd.Parameters.Add( "@name", OleDbType.VarChar, 15 );
// and the value
      name.Value = "Tang";

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
note: as you can see, the code has no quotes for the string value. OleDbCommand + OleDbParameter will bind the value. no need for the quote. the same will be true for date values.

Author

Commented:
How does this relate to the INSERT function?

Author

Commented:
the situation now.

have a string date 7/18/2009 and need to convert it to a foxpro date field.

have a string time 12:00 PM and need to convert it to a time field.

In other words I need to figure out how to do these conversions the simplest way.

Thanks

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
SELECT or INSERT does not matter, in regards to the parameter handling...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial