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

New Topic/Question
Reply




MultiQuote




|