Dr ZIZO
Dr ZIZO

Reputation: 343

Dataset not recognized in new form

I have used my Dataset all over the project without any problem.

When I Add a new form , the Dataset is not recognized in it - JUST THE NEW FORM.

It says : The name 'mydataset' does not exist in the current context

I have checked using System.Data.SqlServerCe; using System.Data.SqlClient; is in the form.cs.

I tried to declare new one in form.cs and it doesn't work.

form.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace O2
{
public partial class ProductSelect : Form
{
    public ProductSelect()
    {
        InitializeComponent();
    }

    private void ProductSelect_Load(object sender, EventArgs e)
    {
        //DatabaseDataSet ds = new DatabaseDataSet();

        foreach (DataRow row in mydataset.Products.Rows)
        {
            listBox1.Items.Add(row["Product_Name"]);
        }

    }
}
}

Any help ?

Edit : form2 where mydataset works perfect (without declaring it) :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace O2
{
public partial class SalesBill : Form
{

    public SalesBill()
    {
        InitializeComponent();
    }



    private void SalesBill_Load(object sender, EventArgs e)
    {
     foreach (DataRow row in databaseDataSet.Clients.Rows)
        {
            listBox1.Items.Add(row["Client_Name"]);
        }

    }
  }
 }

Upvotes: 0

Views: 1141

Answers (3)

Reza Aghaei
Reza Aghaei

Reputation: 125312

  • If you are using the designer for configuring the form and elements on it, from toolbox in Data section, drag an DataSet and drop it on form. You can select either typed dataset or untyped dataset

  • If you want to create and use dataset from code, consider declaring your dataset variable and create an instance of your typed or untyped dataset.

open form 2 at designer you probably will see a component with name databaseDataSet in component tray of it. It seems you are using a typed dataset.

And if you look at Data Source window (Shift+Alt+D) you will see a node of DatabaseDataset with some child node that are your tables.

You can drag clients table and drop it on form.

Upvotes: 3

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

You can declare DataSet as global

public static readonly DataSet MyDataSet = new DataSet();    

And access in other forms as

CLassName.MyDataSet; // ClassName is that in which you declare your dataset

But the recommended way is that, you need to add DataSet from toolbox or from new item dialog and add required tables into it.

enter image description here

Upvotes: 1

thewisegod
thewisegod

Reputation: 1542

In .NET you declare a dataset like this:

DataSet myDataSet = new DataSet();

Upvotes: 1

Related Questions