Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 08, 2010 01:13 PM by Sinead
Apr 21, 2010 12:03 PM|LINK
I'm trying to use a DataSet to maintain some rows for a table, and when I've finished my changes, send all changes to the database using a SqlDataAdapter.
I find if there are no rows in the table in the database then I am getting a 'Object reference not set to an instance of an object' when I try to access the table in the Dataset.
Is there a way to work with a Dataset like this ie. I start off with an empty table and I wish to add rows, to access the structure of the table rows, build rows, then add them and do the update on the SQLDataAdapter.
Here is my code:
protected SqlDataAdapter memberDA = new SqlDataAdapter();
protected DataSet memberDS
if (ViewState["memberDS"] != null)
return new DataSet();
ViewState["memberDS"] = value;
protected SqlDataAdapter getDataAdapterForMembers()
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SiteDBConn"].ConnectionString;
memberDA = new SqlDataAdapter("usp_GetMembers", conn);
memberDA.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder cb = new SqlCommandBuilder(memberDA);
protected void onClick_AddMember(object sender, EventArgs e)
SqlDataAdapter sqlDA = getDataAdapterForMembers();
sqlDA.FillSchema(memberDS, SchemaType.Source, "Member");
DataTable localMemDT = new DataTable(); //use to create FromRow and bind to repeater
localMemDT = herdMemberDS.Tables["Member"].Copy();
DataRow fromRow = localMemDT.NewRow();
DataRow toRow = memberDS.Tables["Member"].NewRow();
// Do work on from row
//copy from -> to
toRow.ItemArray = fromRow.ItemArray;
//add row and accept changes
//add row and do not accept changes
//int i = localMemDT.Rows.Count;
catch (Exception ex)
repMembers.DataSource = localMemDT;
Apr 21, 2010 01:55 PM|LINK
OK Apologies - this doesn't seem to have anything to do with no rows in the database. :o/
When I changed my declaration of my Dataset (memberDS )and removed it from the Viewstate I stopped getting the error.
Can anyone see anything wrong with how I am adding the Dataset to the viewstate in the original code?
And am I correct in thinking I need to put the DataSet in the Viewstate?
protected DataSet memberDS = new DataSet();
Sep 08, 2010 01:13 PM|LINK
Just a follow up to this post as I left it without any conclusion.
I ended up using table adaptors to facilitate wat I was tring to do. I was reluctant to give up my dataset/dataadaptor solution as I am fairly familiar with them and table adaptors were a learning curve.
Bottom line if you are trying to do maintain changes in memory and then commit to database - save yourself some time and energy and try table adaptors.