高手请问怎么解决这个问题?
SqlCommand objCommand = new SqlCommand();
SqlConnection objconnection =
new SqlConnection(connection);
Categories categ=new Categories ();
categ = (Categories)cboCategory.SelectedItem ;
Suppliers supplier=new Suppliers ();
supplier =(Suppliers )cboSupplier .SelectedItem ;
objCommand.CommandText =
"INSERT INTO Products (ProductName,SupplierID,CategoryID," +
"QuantityPerUnit,UnitPrice"+
"UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued ) values" +
"(@ProdN,@ProdSupId,@ProdCateId,@ProdQPU,@prodUP, " +
"@prodUIS,@prodUOO,@prodRL,@prodDis)";
//objCommand.Parameters.AddWithValue("@proId",
//txtProductId .Text );
objCommand.Parameters.AddWithValue("@ProdN",
txtProductName .Text);
objCommand.Parameters.AddWithValue("@ProdSupId",
supplier .getSupplierId ());
objCommand.Parameters .AddWithValue ("@ProdCateId",
categ .getCategoryId ());
objCommand.Parameters .AddWithValue ("@ProdQPU",
txtQuantityUnit .Text );
objCommand.Parameters .AddWithValue ("@prodUP",
txtUnitPrice.Text );
objCommand.Parameters .AddWithValue ("@prodUIS",
txtUnitsInStock.Text );
objCommand.Parameters .AddWithValue ("@prodUOO",
txtUnitsOnOrder .Text );
objCommand.Parameters .AddWithValue ("@prodRL",
txtReorderLevel .Text );
objCommand.Parameters .AddWithValue ("@prodDis",
chbDiscontinued1.Checked);
//here's the try
try
{
//open the connection
objconnection.Open();
//execute the query
// dataCommand.Connection = dataConnection;
objCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
finally
{
objconnection.Close();
}
// refresh the list
//retrieveComputers();
// clear the fields
//refreshField();
}
else
{
MessageBox.Show("Please ensure that you have" +
" data in all the fields");
}
我需要向northwind数据库的product表存入数据,可是老是不行,还提示:ExecuteNonQuery: Connection property has not been initialized.究竟我错在哪里?