Solved

Newbie to ADO.NET

Posted on 2004-10-13
12
331 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
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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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