run time data table

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace DSApp
{
class MyDataSet
{
DataSet ds;
SqlDataAdapter da;
SqlConnection con;

public MyDataSet()
{
ds = new DataSet();
// createStudent();
ConnectDatabase();
}
void ConnectDatabase()
{
con = new SqlConnection("server=UNISRV;uid=cs;pwd=cs;database=csharp");
con.Open();
da = new SqlDataAdapter("select * from student", con);
da.Fill(ds, "student");

SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.InsertCommand = cb.GetInsertCommand();
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
}
void createStudent()
{
DataTable dt = new DataTable("Student");

DataColumn dc1 = new DataColumn("rno", typeof(int));
DataColumn dc2 = new DataColumn("nm", typeof(string));
DataColumn dc3 = new DataColumn("phy", typeof(int));
DataColumn dc4 = new DataColumn("che", typeof(int));
DataColumn dc5 = new DataColumn("mat", typeof(int));

dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);

dt.PrimaryKey = new DataColumn[] { dc1 };

ds.Tables.Add(dt);
}
public DataRow GetNewRow()
{
return ds.Tables["student"].NewRow();
}
public void AddRow(DataRow dr)
{
ds.Tables[0].Rows.Add(dr);
}
public int GetCount()
{
return ds.Tables[0].Rows.Count;
}
public DataRow GetRow(int i)
{
return ds.Tables[0].Rows[i];
}
public void Update()
{
da.Update(ds, "student");
}
public void Close()
{
con.Close();
}
public void DeleteRow(int n)
{
ds.Tables[0].Rows[n].Delete();
Update();
}
}
}


Attachments

  • run time data table (23797-11647-data_table.txt)
  • Comments

    Author: pavan dwivedi12 Dec 2008 Member Level: Silver   Points : 2

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class CreatDataTable : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    DataTable table = new DataTable();




    //DataColumn col1 = new DataColumn("ID");
    DataColumn col2 = new DataColumn("Name");
    DataColumn col3 = new DataColumn("Checked");
    DataColumn col4 = new DataColumn("Description");
    DataColumn col5 = new DataColumn("Price");
    DataColumn col6 = new DataColumn("Brand");
    DataColumn col7 = new DataColumn("Remarks");



    //col1.DataType = System.Type.GetType("System.BigInt");
    col2.DataType = System.Type.GetType("System.String");
    col3.DataType = System.Type.GetType("System.Boolean");
    col4.DataType = System.Type.GetType("System.String");
    col5.DataType = System.Type.GetType("System.Double");
    col6.DataType = System.Type.GetType("System.String");
    col7.DataType = System.Type.GetType("System.String");



    //table.Columns.Add(col1);
    table.Columns.Add(col2);
    table.Columns.Add(col3);
    table.Columns.Add(col4);
    table.Columns.Add(col5);
    table.Columns.Add(col6);
    table.Columns.Add(col7);



    DataRow row = table.NewRow();



    //row[col1] = 1100;
    row[col2] = "Computer Set";
    row[col3] = true;
    row[col4] = "New computer set";
    row[col5] = 32000.00;
    row[col6] = "NEW BRAND-1100";
    row[col7] = "Purchased on July 30,2008";



    table.Rows.Add(row);



    //GridView GV = new GridView();

    GV.DataSource = table;
    GV.DataBind();


    }
    }

    regard
    pavan



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: