-5

I am making an application to get data from an xml url/file, and then save the data onto a database. All my other code seems fine, but on my insert I keep getting an error.

Here is the error

Error 1 An object reference is required for the non-static field, method, or property 'DataSet1TableAdapters.vehiclesTableAdapter.InsertCars(string, string, string, string, string, decimal, decimal, string, string, string, string, string, string)'

I tried converting to decimal at first, when that didn't work I tried the below.

vehiclesTableAdapter.InsertCars(dr["REGNO"].ToString(), dr["STOCKNO"].ToString(), dr
["COLOUR"].ToString(), dr["NEWUSED"].ToString(), dr["MODELYEAR"].ToString(), Convert.ToDecimal
(Convert.ToInt16(dr["MILEAGE"].ToString())), Convert.ToDecimal(Convert.ToInt16(dr
["RETAILPRICE"].ToString())), dr["MMCODE"].ToString(), dr["IMAGE_URL1"].ToString(), dr
["IMAGE_URL2"].ToString(), dr["IMAGE_URL3"].ToString(), dr["IMAGE_URL4"].ToString(), dr
["DESCRIPTION"].ToString());

This is for MILEAGE and RETAILPRICE

And I still get the same error. May I kindly have some assistance in this regard.

EDIT:

Button click event code:

DataSet cars = new DataSet(); 
cars.ReadXml("example.com/example.xml");
progressBar1.Maximum = cars.Tables[2].Rows.Count; 

foreach (DataRow dr in cars.Tables[2].Rows) 
{ 
  vehiclesTableAdapter.InsertCars(SameAsAbove); 
  progressBar1.Value++; 
}
Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
Siyanda
  • 11
  • 2

1 Answers1

0

From the error message, I assume that the InsertCars method is not static, and you are not using an instance of vehiclesTableAdapter to access it.

Either that, or the method is static but you are trying to call from it a non static method, which is not allowed in C#.

Static methods are those who do not need info from an instance that contains them, they can be executed without any change in states.

If the InsertCars doesn't need to modify your adapter, and you don't want to have an adapter instance, then add static in the declaration of this method and the compile time error should dissapear.

By default in C#, methods are instance methods and they receive the implicit "self" argument. By making them static, no such argument is needed (nor available), and the method must then of course refrain from accessing any instance (non-static) objects or methods of the class.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
  • Yes, well I've done this before, and I eventually had to change the decimal in my database to a string. This time I cannot alter any data. So I believe that this error is because of that one line of code in the above segment. I tried it in Php as well but it did not allow wrappers and I would've had to parse the xml manually. I checked and double checked the insertCars, it seems in order. And I declared the vehiclesTableAdapter. using xmlFeedAutoSLM.cars4ipq_cars4saDataSet1TableAdapters; I'll check up on what you have posted though. Thank you – Siyanda Aug 08 '14 at 09:28
  • There are two errors then, inserting the data itself and calling the method. The call to the method is the one you are mentioning with the exception, not the other one. In order to help you with that you will need to give more info on that :) – Nahuel Ianni Aug 08 '14 at 09:32
  • My code seems too long to add here. I will add it as an answer? – Siyanda Aug 08 '14 at 09:47
  • Add only the relevant part on your question: where you call the method, where you instanciate the object and the method itself. – Nahuel Ianni Aug 08 '14 at 10:00
  • This code is in my button click event 'DataSet cars = new DataSet(); cars.ReadXml("http://www.example.com/example.xml");progressBar1.Maximum = cars.Tables[2].Rows.Count; foreach (DataRow dr in cars.Tables[2].Rows) { vehiclesTableAdapter.InsertCars(SameAsAbove); progressBar1.Value++; }' – Siyanda Aug 08 '14 at 10:44
  • But where does "vehiclesTableAdapter" come from? And the InsertCars signature? – Nahuel Ianni Aug 08 '14 at 15:01
  • This is in a dataset, I have my database connected on the app. So I used the dataset wizard to connect, which is on my data source. So there was no coding done there, it's just a matter of calling the sql query. – Siyanda Aug 11 '14 at 06:23