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.

An object reference is required for the nonstatic field, method, or property
exal
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
Jan Meeusen
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.
Tigers21
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
JoseEjecutin
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!
soheila
AndyPham
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.
JacobRonnie
good luck
ozibryan
Mike V T
Thank you.
Lessons are long and hard when you dont know anything to begin with!!
OrpheusTheBard