I have created a data table using disconnected class, also created the data set,xml file, now I want to load that table in a grid. I wrote all my code under program.cs, but when I tried to access the data set object from the form load method, the data set object is not recognized. The code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace DisconnectedClassDemo
{
public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
DataTable studentInfo = new DataTable("studentinfo");
DataColumn StudentID = new DataColumn("studentID");
StudentID.DataType = typeof(int);
StudentID.Caption="StudentID";
StudentID.AutoIncrement = true;
StudentID.AutoIncrementSeed = 1;
StudentID.AutoIncrementStep = 1;
DataColumn Name = new DataColumn("StudentName", typeof(string));
Name.MaxLength = 50;
Name.AllowDBNull = false;
Name.Caption = "StudentName";
DataColumn Roll = new DataColumn("StudentRoll", typeof(int));
Roll.Caption = "StudnetRoll";
studentInfo.Columns.Add(StudentID);
studentInfo.Columns.Add(Name);
studentInfo.Columns.Add(Roll);
studentInfo.PrimaryKey = new DataColumn[] { StudentID };
//DataRow rowobj = studentInfo.NewRow();
//rowobj["studentName"] = "Badhon";
//rowobj["studentRoll"] = "004";
//studentInfo.Rows.Add(rowobj);
DataSet ds = new DataSet("dataset");
ds.Tables.Add(studentInfo);
ds.WriteXmlSchema("D:\\Student.xsd");
ds.WriteXml("D:\\student.xml");
ds.ReadXmlSchema("d:\\student.xsd");
ds.ReadXml("D:\\student.xml");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DisconnectedClassDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//dataGridView1.DataSource=
//dataGridView1.DataMember=
}
}
}
Why are you writing the data set to the file system? Aren’t you trying to create an in memory dataset and the access that dataset? Try declaring the dataset on the form itself.
Try this: