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

  • Question

  • Hi, I got an error "An object reference is required for the nonstatic field, method, or property 'DataSet1.Customers.get' "
    with the following code. Could any one tell me to fix this error?

    I got eror for this statement

    DataSet1.CustomersDataTable mytable =DataSet1.Customers;  in the code

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
             if (!IsPostBack)
            string ConnectionString = "Data Source=GU177;AttachDbFilename=C:\\NORTHWND.MDF;Integrated Security=True";
            int CategoryID = 3;
            SqlDataAdapter adap = new SqlDataAdapter();
            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = ConnectionString;

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"SELECT * FROM Categories WHERE ([CategoryID] = 3)";
            cmd.Connection = conn;

            DataSet1 ds = new DataSet1();
            adap.SelectCommand = cmd;
            adap.Fill(ds, "Categories");

            DataGrid1.DataSource = ds.Categories;
        protected void Button1_Click(object sender, EventArgs e)
            if (IsValid)
                DataSet1 dsi = new DataSet1();
                DataSet1.CustomersDataTable mytable =DataSet1.Customers;             
                DataSet1.CustomersRow myrow = mytable.NewCustomersRow();

                SqlConnection MYconnection = new SqlConnection();
                MYconnection.ConnectionString = "Data Source=GU177;AttachDbFilename=C:\\NORTHWND.MDF;Integrated Security=True";

                SqlCommand insertcmd = new SqlCommand();
                insertcmd.CommandText = "INSERT INTO Customers VALUES(@CustomerID,@CompanyName,@ContactName,@ContactTitle,@Address,@City,@Region,@PostalCode,@Country,@Phone,@Fax) ";
                insertcmd.Connection = MYconnection;

                insertcmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 50, "CustomerID");
                insertcmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 50, "CompanyName");
                insertcmd.Parameters.Add("@ContactName", SqlDbType.NVarChar, 50, "ContactName");
                insertcmd.Parameters.Add("@ContactTitle", SqlDbType.NVarChar, 50, "ContactTitle");
                insertcmd.Parameters.Add("@Address", SqlDbType.NVarChar, 50, "Address");
                insertcmd.Parameters.Add("@City", SqlDbType.NVarChar, 50, "City");
                insertcmd.Parameters.Add("@Region", SqlDbType.NVarChar, 50, "Region");
                insertcmd.Parameters.Add("@PostalCode", SqlDbType.NVarChar, 50, "PostalCode");
                insertcmd.Parameters.Add("@Country", SqlDbType.NVarChar, 50, "Country");
                insertcmd.Parameters.Add("@Phone", SqlDbType.NVarChar, 50, "Phone");
                insertcmd.Parameters.Add("@Fax", SqlDbType.NVarChar, 50, "Fax");


                myrow.CustomerID = "116";
                myrow.CompanyName = Convert.ToString(TextBox1.Text);
                myrow.ContactName = Convert.ToString(TextBox1.Text);
                myrow.ContactTitle = Convert.ToString(TextBox1.Text);
                myrow.Address = Convert.ToString(TextBox1.Text);
                myrow.City = Convert.ToString(DropDownList1.SelectedValue);
                myrow.Region = Convert.ToString(TextBox1.Text);
                myrow.PostalCode = Convert.ToString(120000);
                myrow.Country = Convert.ToString(ListBox1.SelectedValue);
                myrow.Phone = "967575757";
                myrow.Fax = "12123123";


                SqlDataAdapter adap1 = new SqlDataAdapter();
                adap1.InsertCommand = insertcmd;
                adap1.Update(dsi, "Customers");

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"SELECT * FROM Customers";
                cmd.Connection = MYconnection;

                adap1.SelectCommand = cmd;
                adap1.Fill(dsi, "Customers");

                DataGrid1.DataSource = dsi.Customers;

    Tuesday, November 10, 2009 11:44 AM


  • Hi Arun,

    Since DataSet1 is a type not instance, Customers can't be accessed by it.
    However, you can access like this,

                DataSet1.CustomersDataTable mytable =dsi.Customers;             

    Best Regards
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun_Feng Monday, November 16, 2009 7:49 AM
    Thursday, November 12, 2009 8:27 AM