Join 136,423 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,338 people online right now. Registration is fast and FREE... Join Now!
An object reference is required for the nonstatic field, method, or property 'MPCS.TicketsQueries.InsertTicket(System.DateTime, string, int, int, decimal, decimal, decimal, string, decimal)'
Can someone help me with what I am doing wrong, there are other functions in that class file that I am calling and they are working correctly...
Since you class is not a static class you must create an instance of the class before calling any of the methods. Look at this example
csharp
//create an instance of your TicketsQueries class TicketsQueries tickets = new TicketsQueries();
//now you can call your InsertTicket method using //the instance you created in the first line tickets.InsertTicket(ticketDateTimePicker.Value, ticketDescriptionTextBox.Text, TID, CID, billedTime, billedRate, ticketTotal, ticketPaidComboBox.Text, ticketProfit);
You could also make your class static (and all methods inside it would have to be static as well), like
csharp
//make your class static public static class TicketsQueries { //now since your class is static so do the methods have to be static public static void InsertTicket(DateTime TicketDate, string TicketDescription, int TherapistID, int CustomerID, decimal TicketBillableTime, decimal TicketBillableRate, decimal TicketTotal, string TicketPaid, decimal TicketProfit) { string sSQL = "InsertTicket"; SqlConnection cnInsertRecord = new SqlConnection(DataAccess.GetConnectionString("MPCS.Properties.Settings.MassageProConnectionString")); SqlCommand cmdInsertRecord = new SqlCommand(); cmdInsertRecord.Parameters.Clear(); cmdInsertRecord.CommandText = sSQL; cmdInsertRecord.CommandType = CommandType.StoredProcedure; cmdInsertRecord.Parameters.AddWithValue("@TicketDate", TicketDate); cmdInsertRecord.Parameters.AddWithValue("@TicketDescription", TicketDescription); cmdInsertRecord.Parameters.AddWithValue("@TherapistID", TherapistID); cmdInsertRecord.Parameters.AddWithValue("@CustomerID", CustomerID); cmdInsertRecord.Parameters.AddWithValue("@TicketBillableTime", TicketBillableTime); cmdInsertRecord.Parameters.AddWithValue("@TicketBillableRate", TicketBillableRate); cmdInsertRecord.Parameters.AddWithValue("@TicketTotal", TicketTotal); cmdInsertRecord.Parameters.AddWithValue("@TicketPaid", TicketPaid); cmdInsertRecord.Parameters.AddWithValue("@TicketProfit", TicketProfit); cmdInsertRecord.Connection = cnInsertRecord; DataAccess.HandleConnection(cnInsertRecord); cmdInsertRecord.ExecuteNonQuery(); } }
Now that it's static you cal use it the way you have it now
Given the fact that you are passing in all of the parameters (i.e., They aren't already properties of the object) you could also have made just the method static, and still had the class as normal.
This is extremely handy when there is a function of the class that is not dependent on an instantiation of the class, but which can be called by anyone anywhere so long as the right information is provided (like this method).
Another question on the same topic... On the MainForm, I have a DataGrid being populated inside of a function, so that it can be called and populated/refreshed without having to repeat the entire code, it works fine if it is called from the MainForm, but how can I call it from another form, so that upon closing of the other form, it will refresh the DataGrid with the new entries that were inserted from the other form?
The MainForm...
CODE
namespace MPCS { public partial class MainForm : Form { private void loadTicketsDataGrid(int CID) { ticketsDataGridView.DataSource = TicketsQueries.GetTicketsByCustomerID(CID);
namespace MPCS { public partial class AddTicketForm : Form { private void newTicketButton_Click(object sender, EventArgs e) { int errorTracker = 0; if (ticketDescriptionTextBox.Text == "") { MessageBox.Show("You must enter a description."); errorTracker += 1; } if (ticketBillableTimeTextBox.Text == "") { MessageBox.Show("You must enter the billable time."); errorTracker += 1; } if (ticketBillableRateTextBox.Text == "") { MessageBox.Show("You must enter the billable rate."); errorTracker += 1; } if (ticketPaidComboBox.Text == "") { MessageBox.Show("You must select whether or not the ticket was paid."); errorTracker += 1; } if (errorTracker == 0) { decimal ticketProfit = (ticketTotal * TherapistPercentage) / 100; TicketsQueries.InsertTicket(ticketDateTimePicker.Value, ticketDescriptionTextBox.Text, TID, CID, billedTime, billedRate, ticketTotal, ticketPaidComboBox.Text, ticketProfit); MainForm mForm = new MainForm(); mForm.loadTicketsDataGrid(CID); this.Close(); } } } }
Notice on the other form that I use "mForm.loadTicketsDataGrid(CID);" to try and refresh the DataGrid prior to the closing of this form...
Now on the MainForm, I tried to use public static void for the function, but it gave a arm's length of errors... any ideas on how to accomplish this task?
Just to help your understanding, I'll give you a quick explanation of why you did one static and the other not.
With the first example you posted, this was for the method TicketQueries.InsertTicket(DateTime, string, int, int, decimal, decimal, decimal, string, decimal). Now, in this instance, the method was within the class TicketQueries, which was not being instantiated (i.e., You were not creating an instance of the class before calling the method). As it was not being instantiated, you were generating the error because the method was not static. As you found out, labelling the method with the "static" keyword allowed you to use it, and this is because the static keyword makes the method seperate from any instantiation (and makes it so that you can't even call the method on an instantiated object).
Your second problem needed to be non static because it is the other way around. You have an instance of MainForm (specifically, the form being shown on screen) and it is the value of this specific instantiation that you want to modify (there is no value in modifying a non-instantiated form's display values/datagrid details). As such, you needed to make the method non-static so that it is a method of the object itself rather than being something that can be called without instantiation.
If you don't understand the word instantiation or instances of classes then the above will not have made too much sense, but http://searchcio-midmarket.techtarget.com/...212355,00.html# appears to be a reasonable reference, if a bit wordy. To simplify, you would have a class named car, but your car is actually an instance of that car class, with it's own make, model, datagrid , completely seperate to my car instance which has it's own make, model, etc.
Effectively, static is similar to function libraries of old languages where you just call the method once. Most of the Math class is a good example, there would be no value of instantiating the Math class just to run the Sqrt method; instead you just call Math.Sqrt. As such, Sqrt is defined as a static method within the Math class.
Hope that helps to explain/clarify the issue with static methods.