DataSet Problem !
-
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. -
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.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[^]
-
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.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
-
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
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 ?