Jump to content
Sign in to follow this  
greengiant912

C# Assignment.

Recommended Posts

Alright so I have a really basic program that is connecting to a access db file (mdb), and I am supposed to have it load the database into a datagrid with a click event. I also need it to save the updates with a click event. I am currently having an issue where it wont load the data into the datagrid when the button (btnLoad) is pressed. I have checked it with breakpoints and everything seems to work fine, no errors or anything.

 

Me and my professor looked it over and couldn't figure it out. It seems to us that

this.dataDatabaseOutput.SetDataBinding(memberDs, "customer"); 

line and the SetDataBinging isn't being reconized as vaild syntax for some reason. It dose error out when I goto complie and run it saying:

 

Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'SetDataBinding' and no extension method 'SetDataBinding' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) Q:\SP11\CSC112\Fuller,Brian-Chapter14\exercise14.2\exercise14.2\Form1.cs 74 41 exercise14.2

 

Here is my code for the assignment.

/*Brian Fuller exercise 14.2
* CSC112
* 
* This program pulls user data from an access database (customers.mdb), and will load the information upon btnLoad,
* The program will also let a user update the information within the datagrid, and save that information.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace exercise14._2
{
   public partial class DataBaseEditForm : Form
   {
       private string sConnection;
       private OleDbCommand dbCmd;
       private OleDbDataReader dbReader;
       private string sql;
       private OleDbDataAdapter memberDataAdap;
       private DataSet memberDs;
       private OleDbCommandBuilder cBuilder;
       private OleDbConnection dbConn;



       public DataBaseEditForm()
       {
           InitializeComponent();
       }

       private void btnUpdate_Click(object sender, EventArgs e)
       {
           try
           {
               cBuilder = new OleDbCommandBuilder(memberDataAdap);
               memberDataAdap.Update(memberDs, "customer");
           }
           catch (System.Exception exc)
           {
               MessageBox.Show(exc.Message);
           }

       }

       private void btnLoad_Click(object sender, EventArgs e)
       {
           try
           {
               sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                   "Data Source=customer.mdb";
               OleDbConnection dbConn;
               dbConn = new OleDbConnection(sConnection);
               dbConn.Open();

               sql = "select FirstName, LastName, CustomerNumber, CustomerLoc from customer order by LastName";

               dbCmd = new OleDbCommand();
               dbCmd.CommandText = sql;
               dbCmd.Connection = dbConn;

               memberDataAdap = new OleDbDataAdapter();
               memberDataAdap.SelectCommand = dbCmd;
               memberDs = new DataSet();
               memberDataAdap.Fill(memberDs, "customer");

               this.dataDatabaseOutput.SetDataBinding(memberDs, "customer");

           }
           catch (System.Exception exc)
           {
               MessageBox.Show(exc.Message);
           }
       }



   }
}


Edited by greengiant912

Share this post


Link to post
Share on other sites

Rough guess.

 

Code example taken from a book with little to no modification on your part yet.

Code likely written in .Net 1

You are using a C# editor from MS probably named VS2005 or higher which defaults to .Net 2 (or higher).

 

If so you will need to change the targer framework in your project compilation options.

Share this post


Link to post
Share on other sites

Figured it out, our book was showing stuff from a previous version, even though the book was written for the newest version... lame.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...