Solved

Newbie to ADO.NET

Posted on 2004-10-13
12
330 Views
Last Modified: 2010-04-15
Hi im really brand spanking new tho this c# / ado.net game.

Im trying (really) to build a small app using vs2003 and trying to get to grips with dataapaters,connections,datasets etc....

Im coding it by hand instead of using the wizard, so i get a better understanding.
but im having difficulty populating my datagrid, ive place a datgrid on the form which i have called "mydatagrid", have not added anything in the properties(datasource, etc..)

static void Main(string[] args)
            {

                  //Connection String
                  string connectionString = @"provider=Microsoft.JET.OLEDB.4.0;data source=\\server\company\hotelcosts.mdb";

                  //Command String
                  string commandString = @"SELECT * FROM tblHotel";

                  //Adapter

                  OleDbDataAdapter dataAdapter = new
                  OleDbDataAdapter(commandString,connectionString);

                  DataSet myDataSet = new DataSet();
                  dataAdapter.Fill (myDataSet);
                  DataTable table = myDataSet.Tables["tblHotel"];

                  DataGrid mydatagrid = new DataGrid();
                  mydatagrid.DataSource = myDataSet;

                  ComboBox mycombobox = new ComboBox();
                  mycombobox.DataSource = myDataSet;

                  
                  Application.Run(new Form1());
            }

Thanks
0
Comment
Question by:ziwez0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
12 Comments
 
LVL 3

Expert Comment

by:Realmrat
ID: 12296758
              DataGrid mydatagrid = new DataGrid();
               mydatagrid.DataSource = myDataSet;
          -> mydatagrid.DataBind();

               ComboBox mycombobox = new ComboBox();
               mycombobox.DataSource = myDataSet;
          -> mycombobox.DataBind();


Might be the easy fix.  Pulling up VS to test myself.  =]

- Joe
0
 
LVL 2

Author Comment

by:ziwez0
ID: 12296822
Opps mydatagrid does not have a property of DataBind
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 12296879
Hello

  DataBind method only available for asp.net DataGrid, not the winforms one
if you are creating winforms application, I think you need to define DataMember property

                DataGrid mydatagrid = new DataGrid();
               mydatagrid.DataSource = myDataSet;
               mydatagrid.DataMember = myDataSet.Tables[0];

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Realmrat
ID: 12296933
Ha, you can tell I'm used to ASP.NET.  =P

Anyway, here's your problem.  Where you have your code, is in the wrong place.  You need to put that code where the "Form" is being built.  This is in the Form1 constructor.  In VS you'll see the InitializeComponent() method, then after that a //TODO: Message.  Create a small fuction to load the data from the database.  Ala:


 private void LoadFromDatabase()
{
               //Connection String
               string connectionString = @"provider=Microsoft.JET.OLEDB.4.0;data source=\\server\company\hotelcosts.mdb";

               //Command String
               string commandString = @"SELECT * FROM tblHotel";

               //Adapter

               OleDbDataAdapter dataAdapter = new
               OleDbDataAdapter(commandString,connectionString);

               DataSet myDataSet = new DataSet();
               dataAdapter.Fill (myDataSet);
               DataTable table = myDataSet.Tables["tblHotel"];

               DataGrid mydatagrid = new DataGrid();
               mydatagrid.DataSource = myDataSet;
}



so, the Form1 constructor will look like such:

public Form1()
{
          //
          //  Required for Windows Form Designer support
         //
         InitializeComponent();

         LoadFromDatabase();
}


Should all be peachy!  Then there's a variable to set to have a specific table be viewed when the form is loaded.  I'll find that.  For now, just click on the + when the form loads, and you'll see all the rows returned.

- Joe
0
 
LVL 3

Expert Comment

by:Realmrat
ID: 12296996
As said you can set the default table to view using DataMember:

mydatagrid.DataMember = "tblHotel";

Also the LoadFromDatabase() function has to be a part of the Form1 Class.  So make sure to put it in it.  Easiest to just paste it under the constructor.

public Form1()
{
   // ...
}

private void LoadFromDatabase()
{
   //...
}


 - Joe
0
 
LVL 3

Expert Comment

by:Realmrat
ID: 12297093
Er, don't use:

mydatagrid.DataMember = "tblHotel";

The program will crash ... heheh.  Just use:

mydatagrid.DataMember = "Table";

  - Joe
0
 
LVL 3

Accepted Solution

by:
Realmrat earned 150 total points
ID: 12297242
And here's some fun stuff for the combobox - just add it at the end of the LoadFromDatabase function:


// This will add the column value to the combobox
foreach( DataRow row in myDataSet.Tables[0].Rows )
{
       mycombobox.Items.Add( row["myColumnName"] );
}



Enjoy!

  - Joe
0
 
LVL 2

Author Comment

by:ziwez0
ID: 12318293
Hey Joe, managed to do it....

public frmconfig()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  string cs = @"provider=Microsoft.JET.OLEDB.4.0;data source=hotelcosts.mdb";
                  
                  OleDbConnection conn = new OleDbConnection(cs);
                  
                  //SELECT tblcost.hotelid, tblhotel.hotelname, tblcost.dateid FROM (tblhotel INNER JOIN tblcost ON tblhotel.hotelid = tblcost.hotelid)
                  OleDbDataAdapter hotels = new OleDbDataAdapter("Select hotelid, hotelname FROM tblhotel ORDER BY hotelname",conn);
                  //OleDbDataAdapter costs = new OleDbDataAdapter("Select * from tblcost",conn);      
                  OleDbDataAdapter costs = new OleDbDataAdapter("Select tblcost.hotelid, tbldate.thedate, tblcost.cost, tblcost.cost AS Total FROM tblcost INNER JOIN tbldate ON tblcost.dateid = tbldate.dateid ORDER BY tbldate.dateid",conn);
                  //OleDbDataAdapter Calc = new OleDbDataAdapter("Select");      
                  DataSet ds = new DataSet();
                  
                  hotels.Fill(ds,"Hotels");
                  costs.Fill(ds,"Costs");

                  DataRelation dr = new DataRelation("HotelCosts",ds.Tables["Hotels"].Columns["hotelid"],ds.Tables["Costs"].Columns["hotelid"]);
            
                  ds.Relations.Add(dr);

                  comboBox1.DataSource = ds.Tables["Hotels"];
                  comboBox1.DisplayMember = "hotelname";

                  dataGrid1.SetDataBinding(ds.Tables["Hotels"],"HotelCosts");
                  
            }
0
 
LVL 3

Expert Comment

by:Realmrat
ID: 12324993
Glad ya got it working.  =]

 - Joe
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question