1 Replies - 4759 Views - Last Post: 31 December 2008 - 07:37 AM Rate Topic: -----

#1 satyabhaskar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-December 08

Object reference not set to an instance of an object

Posted 27 December 2008 - 12:41 PM

hi,
im trying to insert the data into dependent tables namely tblAuthors and tblBooks.... my code is as follows
 DataSet oDS = new DataSet();
		SqlConnection conn = new SqlConnection("server=localhost;database=dbProject;uid=sa;pwd=");
		conn.Open();
		// Create the DataTable "Orders" in the Dataset and the OrdersDataAdapter

		SqlDataAdapter oAuthorDataAdapter = new
		SqlDataAdapter(new SqlCommand("SELECT * FROM tblAuthor", conn));

		oAuthorDataAdapter.InsertCommand = new SqlCommand("proc_InsertAu", conn);
		SqlCommand cmdInsert = oAuthorDataAdapter.InsertCommand;
		cmdInsert.CommandType = CommandType.StoredProcedure;

		cmdInsert.Parameters.Add(new SqlParameter("@AuthorId", SqlDbType.Int));
		cmdInsert.Parameters["@AuthorID"].Direction = ParameterDirection.Output;
		cmdInsert.Parameters["@AuthorID"].SourceColumn = "AuthorID";

		cmdInsert.Parameters.Add(new SqlParameter("@AuthorName",
			SqlDbType.VarChar, 20,"Authorname"));

		cmdInsert.Parameters.Add(new SqlParameter("@AuthorCountry",
									 SqlDbType.VarChar, 20,"AuthorCountry"));

		oAuthorDataAdapter.FillSchema(oDS, SchemaType.Source);

		DataTable pTable = oDS.Tables["Table"];
		pTable.TableName = "tblAuhtor";

		// Create the DataTable "OrderDetails" in the

		// Dataset and the OrderDetailsDataAdapter


		SqlDataAdapter oBooksDataAdapter = new
			  SqlDataAdapter(new SqlCommand("SELECT * FROM tblBooks", conn));

		oBooksDataAdapter.InsertCommand = new
			  SqlCommand("proc_InsertBk", conn);

		cmdInsert = oBooksDataAdapter.InsertCommand;
		cmdInsert.CommandType = CommandType.StoredProcedure;

		cmdInsert.Parameters.Add(new SqlParameter("@AuthorId", SqlDbType.Int));
		cmdInsert.Parameters["@AuthorID"].SourceColumn = "AuthorId";

		cmdInsert.Parameters.Add(new SqlParameter("@BookNumber", SqlDbType.BigInt));
		cmdInsert.Parameters["@BookNumber"].SourceColumn = "BookNumber";
		cmdInsert.Parameters.Add(new SqlParameter("@ISBN", SqlDbType.VarChar, 25));
		cmdInsert.Parameters["@ISBN"].SourceColumn = "ISBN";
		cmdInsert.Parameters.Add(new SqlParameter("@Price", SqlDbType.Decimal));
		cmdInsert.Parameters["@Price"].SourceColumn = "Price";

		cmdInsert.Parameters.Add(new SqlParameter("@CdStatus", SqlDbType.Char));
		cmdInsert.Parameters["@CdStatus"].SourceColumn = "CdStatus";
		cmdInsert.Parameters.Add(new SqlParameter("@BookTitle", SqlDbType.VarChar, 100));
		cmdInsert.Parameters["@BookTitle"].SourceColumn = "BookTitle";
		oBooksDataAdapter.FillSchema(oDS, SchemaType.Source);

		pTable = oDS.Tables["Table"];
		pTable.TableName = "tblBooks";

		// Create the relationship between the two tables


		oDS.Relations.Add(new DataRelation("ParentChild",
		oDS.Tables["tblAuthor"].Columns["AuthorID"],
		oDS.Tables["tblBooks"].Columns["AuthorID"]));
		// Insert the Data

		DataRow oAuthorRow = oDS.Tables["tblAuthor"].NewRow();
		oAuthorRow["AuthorName"] = txtAuthorName.Text;
		oAuthorRow["AuthorCountry"] = TxtAuthCountry.Text;
		oDS.Tables["tblAuthor"].Rows.Add(oAuthorRow);

		DataRow oBooksRow = oDS.Tables["tblBooks"].NewRow();
		oBooksRow["BookNumber"] = txtBookNO.Text;
		oBooksRow["ISBN"] = txtIsbn.Text;
		oBooksRow["Price"] = txtPrice.Text;
		oBooksRow["BookTitle"] = txtBt.Text;
		oBooksRow["CdStatus"] = txtCD.Text;

		oBooksRow.SetParentRow(oAuthorRow);
		oDS.Tables["tblBooks"].Rows.Add(oBooksRow);

		oAuthorDataAdapter.Update(oDS, "tblAuthors");
		oBooksDataAdapter.Update(oDS, "tblBooks");

		conn.Close();



im getting error Object reference not set to an instance of an object near creation of relation betwwen two table...can i know y this error is occuring...

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Object reference not set to an instance of an object

Posted 31 December 2008 - 07:37 AM

can you verify that the table names in the dataset tables collection are indeed tblAuthor and tblBooks, prior to trying to set the relation?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1