Solved

Newbie to ADO.NET

Posted on 2004-10-13
12
324 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
COnsume rest client 6 33
XML & .net 5 40
What .NET URL re-routing tool did I use? 2 31
free scanner TWAIN can be operated with a Web application 9 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
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…

943 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

4 Experts available now in Live!

Get 1:1 Help Now