Solved

Newbie to ADO.NET

Posted on 2004-10-13
12
328 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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