0

HI I am creating WCF service for which I have Created three (Questions,Answers and Categories) classes by creating a class library project and used their DLL for creating the service as in the below code

public Answers InsertAnswer(Answers answer)
{
    try
    {
        answer_ = new Answers();
        cs = ConfigurationManager.ConnectionStrings[csName].ConnectionString;
        using (con = new SqlConnection(cs))
        {
            cmd = new SqlCommand("InsertAnswer", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@answer", answer.Answer);
            cmd.Parameters.AddWithValue("@questionId", answer.QuestionId);
            con.Open();
            cmd.ExecuteNonQuery();
            answer_ = GetAnswer(answer);
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }
    return answer_;
}

answer_ is Global object of class Answers which is under

using QuestionsAnswers;

namespace Now I am using the same namespace in my client and used the same service for calling the method but gives me error Below is the code on my client side

public void InserQuestionAnswer(QA qaParam)
{
    try
    {
        Answers answer = new Answers();
        answer.QuestionId = 1;
        answer.Answer = qaParam.Answer.ToString();
        QuestionsAnswersService.QuestionAnswerServiceClient questionAnswerService = new QuestionsAnswersService.QuestionAnswerServiceClient("BasicHttpBinding_IQuestionAnswerService");
        questionAnswerService.InsertAnswer(answer);
    }
    catch (Exception ex)
    {

    }
}

I am using the Same reference

using QuestionsAnswers;

I am getting the below error

Severity    Code    Description Project File    Line    Suppression State
Error   CS0120  An object reference is required for the non-static field, 
method, or property 'QAaspx.InserQuestionAnswer(QA)'    QASamapleUI E:\Rohit 
Gupta\PracticeProjects\WebApp\QASamapleUI\QASamapleUI\QAaspx.aspx.cs    25  
Active

Can anybody please help me

giokoguashvili
  • 2,013
  • 3
  • 18
  • 37
Rohit Gupta
  • 455
  • 4
  • 16
  • Possible duplicate of [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – Devin L. Aug 28 '17 at 18:27

1 Answers1

0

You are getting this message because the method you're calling with QAaspx.InserQuestionAnswer(QA) is not static, and evidently you don't have an object reference to its class when you're calling it.

You can solve this by creating an instance of that class and invoking the method. You don't show what class it is in in your code, but let's assume the class is Foo. Then you could do this:

Foo myFoo = new Foo();
myFoo.InserQuestionAnswer(QA);

Alternatively, if that method doesn't need a reference to any other properties of its class, you can just make it static .

public static void InserQuestionAnswer(QA qaParam) {} 
luxdvie
  • 902
  • 8
  • 16