Solved

Newbie to ADO.NET

Posted on 2004-10-13
12
322 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
Comment Utility
              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
Comment Utility
Opps mydatagrid does not have a property of DataBind
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
 
LVL 3

Expert Comment

by:Realmrat
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:Realmrat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad ya got it working.  =]

 - Joe
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now