An object reference is required for the nonstatic field, method, or property

I know you have all seen this one before, for which i am sorry. But I can't figure this out.

I am not sure if what I am trying to do here will even work in the end but this is my first project and I am learning as I go, so please try not to laugh.

I have a combobox of form1 that is bound to a data source and on form3 I have a report viewer that (I hope) will load using the text that is in the combobox on form1.

This is where I have the problem! I have searched many forums but still don’t really understand!!

(it is the WHERE SaveName = '" + Form1.comboBox2.Text + "'";where the error occurs, as follows:

(An object reference is required for the nonstatic field, method, or property 'Stock_Scan.Form1.comboBox2')

Here is the code from form 3.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace Stock_Scan

{

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void reportViewer1_Load(object sender, EventArgs e)

{

SqlConnection conn = new System.Data.SqlClient.SqlConnection("Initial Catalog=StockScan;Data Source=(local);Integrated Security=SSPI");

String ls_sqlstate = "SELECT * FROM ProductHis WHERE SaveName = '" + Form1.comboBox2.Text + "'";

conn.Open();

SqlCommand cmd = new System.Data.SqlClient.SqlCommand(ls_sqlstate, conn);

cmd.ExecuteNonQuery();

conn.Close();

}

}

Any help would be greatly appriciated.



Answer this question

An object reference is required for the nonstatic field, method, or property

  • atirado

    Form1 is the class name, so not an instance of the form

    the thing you shoud do is:

    Form 3:

    private String myParam;

    public Form3(String yourParam)

    {

    InitializeComponent();

    myParam = yourParam

    }

    so it becomes

    String ls_sqlstate = "SELECT * FROM ProductHis WHERE SaveName = '" + myParam+ "'";

    However, you shouldn't be doing this in the gui code!



  • The.B

    Hi, Smegg

    Additionally, I suggest you to use cmd.Parameter instead of directly adding string as param. That is to say use:

    String ls_sqlstate = "SELECT * FROM ProductHis WHERE SaveName = @param";

    cmd.Parameters.AddWithValue("param",value);

    instead

    It is more secure, since it will check the param input.



  • museicon

    You could also try keeping a public string variable!

    Set the string variable when the user chooses an item from the combobox!

    Use this variable in form 3



  • Callan

    Thank you for responding. But i am affriad i am not sure how to declare the combobox as static public. I know that sounds stupid but i am learning as i go along and i am still not sure how or why somethings work, I just keep trying different things until they do!
  • Ragnvald

    Thank you all for your help think i may have this one now, but you can expect to see my name on this forum a few more times in the future!

    Thanks again


  • Mark Goldstein

    Thank you.

    Lessons are long and hard when you dont know anything to begin with!!


  • mameelas

    or you can declare your combobox as static public Form1 and then you can access it using Form1.comobox

  • Sniper167

    good luck



  • shax

    Ok thanks for your help that seem to have got over that little problem. But would you mind explaining, if i shouldn't be doing this in the gui code, what should i be doing
  • Paul Sanders

    You open Form1.designer.cs, in this file you find the declarations and initialisations for all components and controls you create in form1 designer.

    and in this file u edit the declaration of your combobox to static public, and in this way you can call this control anywhere in the project without creating an instance of form1.



  • An object reference is required for the nonstatic field, method, or property