Sunday, December 26, 2021

WinForm ADO.NET DataSet in Hindi

NOTE:-

CODE:


using System;
using System.Data;
using System.Windows.Forms;
namespace DataSetWF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void LoadDataButton_Click(object sender, EventArgs e)
        {
            try
            {
                //DataSet is collection of DataTable objects in memory
                DataSet dataSet = new DataSet("InMemoryDB");
                DataTable dataTable1 = new DataTable("InMemoryTable");
                DataColumn dataColumn1 = new DataColumn("Column1", typeof(int));
                DataColumn dataColumn2 = new DataColumn("Column2", typeof(string));
                DataColumn dataColumn3 = new DataColumn("Column3", typeof(float));
                DataColumn dataColumn4 = new DataColumn("Column4", typeof(string));
                //DataTable is collection of DataColumn objects
                //Columns is property of DataTable with return type DataColumnCollection 
                dataTable1.Columns.Add(dataColumn1);
                dataTable1.Columns.Add(dataColumn2);
                dataTable1.Columns.Add(dataColumn3);
                dataTable1.Columns.Add(dataColumn4);
                //DataTable is collection of DataRow objects
                //Rows is property of DataTable with return type DataRowCollection 
                dataTable1.Rows.Add(1, "Shrishesh", 2.12, "Delhi");
                dataTable1.Rows.Add(2, "Amit", 3.12, "Delhi");
                dataTable1.Rows.Add(3, "Rajesh", 2.4, "Patna");
                dataTable1.Rows.Add(4, "Priyanka", 4.52, "Goa");
                dataSet.Tables.Add(dataTable1);
                //DataSet is collection of DataTable objects
                DataGridView1.DataSource = dataSet.Tables["InMemoryTable"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            }
        }

        private void LoadDataButton2_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridView1.DataSource = null;
                //We learn about DataColumn in the following code
                DataSet schoolDB = new DataSet("School");
                DataTable tbl_Student = new DataTable("Student");

                DataColumn id = new DataColumn();
                id.ColumnName = "ID";
                id.DataType = typeof(int);
                id.AllowDBNull = false;
                id.AutoIncrement = true;
                id.AutoIncrementSeed = 1;
                id.AutoIncrementStep = 1;
                tbl_Student.Columns.Add(id);

                DataColumn sname = new DataColumn();
                sname.DataType = typeof(System.String);
                sname.AllowDBNull = false;
                sname.ColumnName = "Student Name";
                tbl_Student.Columns.Add(sname);

                DataColumn age = new DataColumn();
                age.DataType = typeof(int);
                age.AllowDBNull = true;
                age.ColumnName = "Age";
                tbl_Student.Columns.Add(age);

                DataColumn city = new DataColumn();
                age.DataType = typeof(System.String);
                age.AllowDBNull = true;
                age.ColumnName = "City";
                tbl_Student.Columns.Add(city);           
                
                tbl_Student.Rows.Add(null, "Ajeet", 17, "Agra");
                tbl_Student.Rows.Add(null, "Rajesh", 14, "Faridabad");
                tbl_Student.Rows.Add(null, "Priyanka", 15, "Patna");
                tbl_Student.Rows.Add(null, "Abhijit", 15, "Delhi");
                
                schoolDB.Tables.Add(tbl_Student);
                DataGridView1.DataSource = schoolDB.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            }
        }
    }
}

No comments:

Post a Comment

Hot Topics