By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

DataSet in C#?

P: 6
How to make a own dataview in c#? Can someone please help me? I want to get id and users and everything else from database to be showed in the same place at the same time just as DataView. Please help me. Need help for this. I can't find anything that I can use on google. I have searched for Howto make a dataset in c# but I'm new to programming. So please help me. =)
Dec 2 '09 #1

✓ answered by sanjib65

If you want to show your data in the = manner, then I'd like to go for making a class of your own and setting properties there so that you can manipulate it later according your own wish.
Suppose I make a Car class like this :

Expand|Select|Wrap|Line Numbers
  1. public class Car
  2. {
  3.     //adding fields
  4.     private string _make;
  5.     private string _model;
  6.     private int _price;
  7.  
  8.     //adding properties
  9.     public string Make
  10.     {
  11.         get { return _make; }
  12.         set { _make = value; }
  13.     }
  14.     public string Model
  15.     {
  16.         get { return _model; }
  17.         set { _model = value; }
  18.     }
  19.     public int Price
  20.     {
  21.         get { return _price; }
  22.         set { _price = value; }
  23.     }
  24.     //construcor
  25.     public Car()
  26.     {
  27.         _make = "Default";
  28.         _model = "Defualt";
  29.         _price = 0;
  30.     }
  31.     //overloading
  32.     public Car(string MAKE, string MODEL, int PRICE)
  33.     {
  34.         _make = MAKE;
  35.         _model = MODEL;
  36.         _price = PRICE;
  37.     }
  38.  
Later I can add new values into the new instances of Cars by overloading the parameters. And show the values in various Label.Text....like this

Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         Car zen1 = new Car("2002", "Maruti alto1", 60000);
  4.         Label1.Text = "Year : " + zen1.Make;
  5.         Label2.Text = "Model : " + zen1.Model;
  6.         Label3.Text = "Price : " + zen1.Price.ToString();
  7.         Label7.Text = "----------------";
  8.         Car zen2 = new Car("2008", "Zen", 70000);
  9.         Label4.Text = "Year : " + zen2.Make;
  10.         Label5.Text = "Model : " + zen2.Model;
  11.         Label6.Text = "Price : " + zen2.Price.ToString();
  12.  
  13.     }
For the simple data manipulation, I'd like to go for the XML also. I can make an XMl file of my favourite author's list, like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <dsPubs xmlns="http://www.tempuri.org/dsPubs.xsd">
  3.   <xs:schema id="dsPubs" targetNamespace="http://www.tempuri.org/dsPubs.xsd" xmlns:mstns="http://www.tempuri.org/dsPubs.xsd" xmlns="http://www.tempuri.org/dsPubs.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  4.     <xs:element name="dsPubs" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
  5.       <xs:complexType>
  6.         <xs:choice minOccurs="0" maxOccurs="unbounded">
  7.           <xs:element name="authors">
  8.             <xs:complexType>
  9.               <xs:sequence>
  10.                 <xs:element name="au_id" type="xs:string" />
  11.                 <xs:element name="au_lname" type="xs:string" />
  12.                 <xs:element name="au_fname" type="xs:string" />
  13.                 <xs:element name="au_phone" type="xs:string" />
  14.               </xs:sequence>
  15.             </xs:complexType>
  16.           </xs:element>
  17.         </xs:choice>
  18.       </xs:complexType>
  19.       <xs:unique name="Constraint1" msdata:PrimaryKey="true">
  20.         <xs:selector xpath=".//mstns:authors" />
  21.         <xs:field xpath="mstns:au_id" />
  22.       </xs:unique>
  23.     </xs:element>
  24.   </xs:schema>
  25.   <authors>
  26.     <au_id>172-32-1176</au_id>
  27.     <au_lname>Tagore</au_lname>
  28.     <au_fname>Rabindranath</au_fname>
  29.     <au_phone>408 555-0123</au_phone>
  30.   </authors>
  31.   <authors>
  32.     <au_id>213-46-8915</au_id>
  33.     <au_lname>Cohelho</au_lname>
  34.     <au_fname>Paulo</au_fname>
  35.     <au_phone>415 555-0120</au_phone>
  36.   </authors>
  37.   <authors>
  38.     <au_id>100-01-1001</au_id>
  39.     <au_lname>Chekhov</au_lname>
  40.     <au_fname>Anton</au_fname>
  41.     <au_phone>688 123-1340</au_phone>
  42.   </authors>
  43.   <authors>
  44.     <au_id>102-02-1002</au_id>
  45.     <au_lname>Leo</au_lname>
  46.     <au_fname>Tolstoy</au_fname>
  47.     <au_phone>408 555-8569</au_phone>
  48.   </authors>
  49. </dsPubs>
  50. //as you see you can make this list longer, you can include other items here....
  51.  
Now you make a BuisObject class creating Dataset dynamically, like this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Web;
  3. using System.Data;
  4. namespace PubsClasses
  5. {
  6.     public class AuthorClass
  7.     {
  8.         private DataSet dsAuthors = new DataSet("ds1");
  9.         private String filePath =
  10.             HttpContext.Current.Server.MapPath
  11.                 ("~/App_Data/Authors.xml");
  12.         public AuthorClass()
  13.         {
  14.             dsAuthors.ReadXml(filePath, XmlReadMode.ReadSchema);
  15.         }
  16.         public DataSet GetAuthors()
  17.         {
  18.             return dsAuthors;
  19.         }
  20.         public void InsertAuthor(String au_id, String au_lname,
  21.     String au_fname, String au_phone)
  22.         {
  23.             DataRow workRow = dsAuthors.Tables[0].NewRow();
  24.             workRow.BeginEdit();
  25.             workRow[0] = au_id;
  26.             workRow[1] = au_lname;
  27.             workRow[2] = au_fname;
  28.             workRow[3] = au_phone;
  29.             workRow.EndEdit();
  30.             dsAuthors.Tables[0].Rows.Add(workRow);
  31.             dsAuthors.WriteXml(filePath, XmlWriteMode.WriteSchema);
  32.         }
  33.     }
  34.  
  35. }
The rest of the part to show the lists by data binding with this BuisnessCobject.cs is a cake walk. There are several handy Framework Controls that you can drag and make them data-bound.

Hope this will help you.

Share this Question
Share on Google+
8 Replies


P: 6
I see the form you refer to there but can't I make it my own? I don't want to use dataGridView. I want to make it my own with sql connection. But I can't get it to work. I want to play a loop thing I think.
Dec 2 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
You've lost me......
Dec 2 '09 #4

P: 6
Thanks for the replay any way. Is it some other ways to make a list from the database then the premaked programs? I want my own. =)
Dec 2 '09 #5

tlhintoq
Expert 2.5K+
P: 3,525
When you query the database you get a response that consists of the data matching your requirements, right? I would imagine you could display that data however you like: Make a chart of the numeric data by month... Song list by artist...
Dec 2 '09 #6

P: 6
Ok. thanks for the answer. =)
Dec 2 '09 #7

sanjib65
100+
P: 102
If you want to show your data in the = manner, then I'd like to go for making a class of your own and setting properties there so that you can manipulate it later according your own wish.
Suppose I make a Car class like this :

Expand|Select|Wrap|Line Numbers
  1. public class Car
  2. {
  3.     //adding fields
  4.     private string _make;
  5.     private string _model;
  6.     private int _price;
  7.  
  8.     //adding properties
  9.     public string Make
  10.     {
  11.         get { return _make; }
  12.         set { _make = value; }
  13.     }
  14.     public string Model
  15.     {
  16.         get { return _model; }
  17.         set { _model = value; }
  18.     }
  19.     public int Price
  20.     {
  21.         get { return _price; }
  22.         set { _price = value; }
  23.     }
  24.     //construcor
  25.     public Car()
  26.     {
  27.         _make = "Default";
  28.         _model = "Defualt";
  29.         _price = 0;
  30.     }
  31.     //overloading
  32.     public Car(string MAKE, string MODEL, int PRICE)
  33.     {
  34.         _make = MAKE;
  35.         _model = MODEL;
  36.         _price = PRICE;
  37.     }
  38.  
Later I can add new values into the new instances of Cars by overloading the parameters. And show the values in various Label.Text....like this

Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         Car zen1 = new Car("2002", "Maruti alto1", 60000);
  4.         Label1.Text = "Year : " + zen1.Make;
  5.         Label2.Text = "Model : " + zen1.Model;
  6.         Label3.Text = "Price : " + zen1.Price.ToString();
  7.         Label7.Text = "----------------";
  8.         Car zen2 = new Car("2008", "Zen", 70000);
  9.         Label4.Text = "Year : " + zen2.Make;
  10.         Label5.Text = "Model : " + zen2.Model;
  11.         Label6.Text = "Price : " + zen2.Price.ToString();
  12.  
  13.     }
For the simple data manipulation, I'd like to go for the XML also. I can make an XMl file of my favourite author's list, like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" standalone="yes"?>
  2. <dsPubs xmlns="http://www.tempuri.org/dsPubs.xsd">
  3.   <xs:schema id="dsPubs" targetNamespace="http://www.tempuri.org/dsPubs.xsd" xmlns:mstns="http://www.tempuri.org/dsPubs.xsd" xmlns="http://www.tempuri.org/dsPubs.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  4.     <xs:element name="dsPubs" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
  5.       <xs:complexType>
  6.         <xs:choice minOccurs="0" maxOccurs="unbounded">
  7.           <xs:element name="authors">
  8.             <xs:complexType>
  9.               <xs:sequence>
  10.                 <xs:element name="au_id" type="xs:string" />
  11.                 <xs:element name="au_lname" type="xs:string" />
  12.                 <xs:element name="au_fname" type="xs:string" />
  13.                 <xs:element name="au_phone" type="xs:string" />
  14.               </xs:sequence>
  15.             </xs:complexType>
  16.           </xs:element>
  17.         </xs:choice>
  18.       </xs:complexType>
  19.       <xs:unique name="Constraint1" msdata:PrimaryKey="true">
  20.         <xs:selector xpath=".//mstns:authors" />
  21.         <xs:field xpath="mstns:au_id" />
  22.       </xs:unique>
  23.     </xs:element>
  24.   </xs:schema>
  25.   <authors>
  26.     <au_id>172-32-1176</au_id>
  27.     <au_lname>Tagore</au_lname>
  28.     <au_fname>Rabindranath</au_fname>
  29.     <au_phone>408 555-0123</au_phone>
  30.   </authors>
  31.   <authors>
  32.     <au_id>213-46-8915</au_id>
  33.     <au_lname>Cohelho</au_lname>
  34.     <au_fname>Paulo</au_fname>
  35.     <au_phone>415 555-0120</au_phone>
  36.   </authors>
  37.   <authors>
  38.     <au_id>100-01-1001</au_id>
  39.     <au_lname>Chekhov</au_lname>
  40.     <au_fname>Anton</au_fname>
  41.     <au_phone>688 123-1340</au_phone>
  42.   </authors>
  43.   <authors>
  44.     <au_id>102-02-1002</au_id>
  45.     <au_lname>Leo</au_lname>
  46.     <au_fname>Tolstoy</au_fname>
  47.     <au_phone>408 555-8569</au_phone>
  48.   </authors>
  49. </dsPubs>
  50. //as you see you can make this list longer, you can include other items here....
  51.  
Now you make a BuisObject class creating Dataset dynamically, like this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Web;
  3. using System.Data;
  4. namespace PubsClasses
  5. {
  6.     public class AuthorClass
  7.     {
  8.         private DataSet dsAuthors = new DataSet("ds1");
  9.         private String filePath =
  10.             HttpContext.Current.Server.MapPath
  11.                 ("~/App_Data/Authors.xml");
  12.         public AuthorClass()
  13.         {
  14.             dsAuthors.ReadXml(filePath, XmlReadMode.ReadSchema);
  15.         }
  16.         public DataSet GetAuthors()
  17.         {
  18.             return dsAuthors;
  19.         }
  20.         public void InsertAuthor(String au_id, String au_lname,
  21.     String au_fname, String au_phone)
  22.         {
  23.             DataRow workRow = dsAuthors.Tables[0].NewRow();
  24.             workRow.BeginEdit();
  25.             workRow[0] = au_id;
  26.             workRow[1] = au_lname;
  27.             workRow[2] = au_fname;
  28.             workRow[3] = au_phone;
  29.             workRow.EndEdit();
  30.             dsAuthors.Tables[0].Rows.Add(workRow);
  31.             dsAuthors.WriteXml(filePath, XmlWriteMode.WriteSchema);
  32.         }
  33.     }
  34.  
  35. }
The rest of the part to show the lists by data binding with this BuisnessCobject.cs is a cake walk. There are several handy Framework Controls that you can drag and make them data-bound.

Hope this will help you.
Dec 3 '09 #8

P: 6
Thanx for the reply. I'll try to get it working. =)
Dec 3 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.