Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataSet Problem !

DataSet Problem !

Scheduled Pinned Locked Moved C#
helpdatabasequestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hi guys. I've added a database in my project. VS2008 SP1 creates a DataSet with Database1DataSet name. Now I want to update the DataBase with this code :

    private void saveButton_Click(object sender, RoutedEventArgs e)
    {
    Database1DataSet myDataSet = new Database1DataSet();

    DataRow myDataRow = myDataSet.Tables\["events"\].NewRow();//on this line 
    myDataRow\["event"\] = "new text";
    myDataRow\["date"\] = mdCalendar.Time();
    myDataSet.Tables\["events"\].Rows.Add(myDataRow);
    

    }

    but this error has occurred :

    Object reference not set to an instance of an object.

    I'm sure the events exist in the DataBase. What's wrong with it ? Thanks in advance.

    W J 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hi guys. I've added a database in my project. VS2008 SP1 creates a DataSet with Database1DataSet name. Now I want to update the DataBase with this code :

      private void saveButton_Click(object sender, RoutedEventArgs e)
      {
      Database1DataSet myDataSet = new Database1DataSet();

      DataRow myDataRow = myDataSet.Tables\["events"\].NewRow();//on this line 
      myDataRow\["event"\] = "new text";
      myDataRow\["date"\] = mdCalendar.Time();
      myDataSet.Tables\["events"\].Rows.Add(myDataRow);
      

      }

      but this error has occurred :

      Object reference not set to an instance of an object.

      I'm sure the events exist in the DataBase. What's wrong with it ? Thanks in advance.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Mohammad Dayyan wrote:

      Database1DataSet myDataSet = new Database1DataSet();

      Since you're creating a new, fresh dataset it may not have any tables in it (depending what you do in the constructor for Database1DataSet). Without knowing your program logic most likely you should refer to an existing dataset which is defined somewhere in the program (at form level, a static object etc.)

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      • M Mohammad Dayyan

        Hi guys. I've added a database in my project. VS2008 SP1 creates a DataSet with Database1DataSet name. Now I want to update the DataBase with this code :

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
        Database1DataSet myDataSet = new Database1DataSet();

        DataRow myDataRow = myDataSet.Tables\["events"\].NewRow();//on this line 
        myDataRow\["event"\] = "new text";
        myDataRow\["date"\] = mdCalendar.Time();
        myDataSet.Tables\["events"\].Rows.Add(myDataRow);
        

        }

        but this error has occurred :

        Object reference not set to an instance of an object.

        I'm sure the events exist in the DataBase. What's wrong with it ? Thanks in advance.

        J Offline
        J Offline
        Jason C Bourne
        wrote on last edited by
        #3

        You create a .Net DataSet in memory, and immediately you do: myDataSet.Tables["events"].NewRow();//on this line Don't you think that your forgot to create a DataTable called "events" ? ;-) You are not accessing any Database here, you are instantiating a DataSet object and you attempt to access a DataTable that you don't have created. I suggest reading some article about ADO.Net, it explains very well how to connect to a Database, retrieve data from it encaspulated in a DataSet, update data and stuff like that.

        Jean-Christophe Grégoire

        M 1 Reply Last reply
        0
        • J Jason C Bourne

          You create a .Net DataSet in memory, and immediately you do: myDataSet.Tables["events"].NewRow();//on this line Don't you think that your forgot to create a DataTable called "events" ? ;-) You are not accessing any Database here, you are instantiating a DataSet object and you attempt to access a DataTable that you don't have created. I suggest reading some article about ADO.Net, it explains very well how to connect to a Database, retrieve data from it encaspulated in a DataSet, update data and stuff like that.

          Jean-Christophe Grégoire

          M Offline
          M Offline
          Mohammad Dayyan
          wrote on last edited by
          #4

          Thanks. I've done it.

          //table's name = eventsTable
          Database1DataSetTableAdapters.eventsTableTableAdapter eventsTableTableAdapte =
          new WpfApplication1.Database1DataSetTableAdapters.eventsTableTableAdapter();
          Database1DataSet.eventsTableDataTable eventsTableDataTable =
          new Database1DataSet.eventsTableDataTable();
          eventsTableTableAdapte.Fill(eventsTableDataTable);
          DataRow newDataRow = eventsTableDataTable.NewRow();
          newDataRow["event"] = "event8" ;
          newDataRow["date"] = 654982;
          eventsTableDataTable.Rows.Add(newDataRow);
          eventsTableTableAdapte.Update(eventsTableDataTable);

          eventsTableTableAdapte.Dispose();
          eventsTableDataTable.Dispose();

          But I don't know why the changes haven't applied to the DataBase :confused: Could you guide me ?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups