Advice
-
I’m a bit of a nubie and looking for some advice, the code bellow works but looks like a bit of a hack. I know it's a 'WebMethod' its general enough. My concerns lie with the 'Populate product' section toward the bottom.
[WebMethod] public Product GetProductById(int Id) { #region oleDbQueery // Connect to dataSource OleDbDataAdapter myAdapter = new OleDbDataAdapter(); OleDbCommand myCommand = new OleDbCommand("ProductByIdQueery", oleDbConnection1); myCommand.CommandType = CommandType.StoredProcedure; OleDbParameter parameterid = new OleDbParameter("Id",OleDbType.Integer); parameterid.Value = Id; myCommand.Parameters.Add(parameterid); myAdapter.SelectCommand = oleDbSelectCommand1; myAdapter.SelectCommand = myCommand; // Populate dataSet #endregion DataSet ds = new DataSet(); oleDbConnection1.Open(); myAdapter.Fill(ds); oleDbConnection1.Close(); // Populate product Product myProduct = new Product(); myProduct.Id = Int32.Parse(ds.Tables[0].Rows[0]["Id"].ToString()); myProduct.Category = Int32.Parse(ds.Tables[0].Rows[0]["Category"].ToString()); myProduct.ShortTitle = ds.Tables[0].Rows[0]["ShortTitle"].ToString(); myProduct.FullTitle = ds.Tables[0].Rows[0]["FullTitle"].ToString(); myProduct.Price = double.Parse(ds.Tables[0].Rows[0]["Price"].ToString()); myProduct.CurrentQuantity = Int32.Parse(ds.Tables[0].Rows[0]["CurrentQuantity"].ToString()); // return myProduct; }
Sorry for the long snippit. My passed in 'Id' would refer to a unique db key so the dataSet 'should' only ever contain one row. Do I have to go via a dataSet to populate myProduct object? All comments greatly welcomed. tia -
I’m a bit of a nubie and looking for some advice, the code bellow works but looks like a bit of a hack. I know it's a 'WebMethod' its general enough. My concerns lie with the 'Populate product' section toward the bottom.
[WebMethod] public Product GetProductById(int Id) { #region oleDbQueery // Connect to dataSource OleDbDataAdapter myAdapter = new OleDbDataAdapter(); OleDbCommand myCommand = new OleDbCommand("ProductByIdQueery", oleDbConnection1); myCommand.CommandType = CommandType.StoredProcedure; OleDbParameter parameterid = new OleDbParameter("Id",OleDbType.Integer); parameterid.Value = Id; myCommand.Parameters.Add(parameterid); myAdapter.SelectCommand = oleDbSelectCommand1; myAdapter.SelectCommand = myCommand; // Populate dataSet #endregion DataSet ds = new DataSet(); oleDbConnection1.Open(); myAdapter.Fill(ds); oleDbConnection1.Close(); // Populate product Product myProduct = new Product(); myProduct.Id = Int32.Parse(ds.Tables[0].Rows[0]["Id"].ToString()); myProduct.Category = Int32.Parse(ds.Tables[0].Rows[0]["Category"].ToString()); myProduct.ShortTitle = ds.Tables[0].Rows[0]["ShortTitle"].ToString(); myProduct.FullTitle = ds.Tables[0].Rows[0]["FullTitle"].ToString(); myProduct.Price = double.Parse(ds.Tables[0].Rows[0]["Price"].ToString()); myProduct.CurrentQuantity = Int32.Parse(ds.Tables[0].Rows[0]["CurrentQuantity"].ToString()); // return myProduct; }
Sorry for the long snippit. My passed in 'Id' would refer to a unique db key so the dataSet 'should' only ever contain one row. Do I have to go via a dataSet to populate myProduct object? All comments greatly welcomed. tiaYou can use a DataTable instead of a DataSet. The
DataAdapter.Fill()
method works with a DataTable object. :) Free your mind... -
I’m a bit of a nubie and looking for some advice, the code bellow works but looks like a bit of a hack. I know it's a 'WebMethod' its general enough. My concerns lie with the 'Populate product' section toward the bottom.
[WebMethod] public Product GetProductById(int Id) { #region oleDbQueery // Connect to dataSource OleDbDataAdapter myAdapter = new OleDbDataAdapter(); OleDbCommand myCommand = new OleDbCommand("ProductByIdQueery", oleDbConnection1); myCommand.CommandType = CommandType.StoredProcedure; OleDbParameter parameterid = new OleDbParameter("Id",OleDbType.Integer); parameterid.Value = Id; myCommand.Parameters.Add(parameterid); myAdapter.SelectCommand = oleDbSelectCommand1; myAdapter.SelectCommand = myCommand; // Populate dataSet #endregion DataSet ds = new DataSet(); oleDbConnection1.Open(); myAdapter.Fill(ds); oleDbConnection1.Close(); // Populate product Product myProduct = new Product(); myProduct.Id = Int32.Parse(ds.Tables[0].Rows[0]["Id"].ToString()); myProduct.Category = Int32.Parse(ds.Tables[0].Rows[0]["Category"].ToString()); myProduct.ShortTitle = ds.Tables[0].Rows[0]["ShortTitle"].ToString(); myProduct.FullTitle = ds.Tables[0].Rows[0]["FullTitle"].ToString(); myProduct.Price = double.Parse(ds.Tables[0].Rows[0]["Price"].ToString()); myProduct.CurrentQuantity = Int32.Parse(ds.Tables[0].Rows[0]["CurrentQuantity"].ToString()); // return myProduct; }
Sorry for the long snippit. My passed in 'Id' would refer to a unique db key so the dataSet 'should' only ever contain one row. Do I have to go via a dataSet to populate myProduct object? All comments greatly welcomed. tia -
I’m a bit of a nubie and looking for some advice, the code bellow works but looks like a bit of a hack. I know it's a 'WebMethod' its general enough. My concerns lie with the 'Populate product' section toward the bottom.
[WebMethod] public Product GetProductById(int Id) { #region oleDbQueery // Connect to dataSource OleDbDataAdapter myAdapter = new OleDbDataAdapter(); OleDbCommand myCommand = new OleDbCommand("ProductByIdQueery", oleDbConnection1); myCommand.CommandType = CommandType.StoredProcedure; OleDbParameter parameterid = new OleDbParameter("Id",OleDbType.Integer); parameterid.Value = Id; myCommand.Parameters.Add(parameterid); myAdapter.SelectCommand = oleDbSelectCommand1; myAdapter.SelectCommand = myCommand; // Populate dataSet #endregion DataSet ds = new DataSet(); oleDbConnection1.Open(); myAdapter.Fill(ds); oleDbConnection1.Close(); // Populate product Product myProduct = new Product(); myProduct.Id = Int32.Parse(ds.Tables[0].Rows[0]["Id"].ToString()); myProduct.Category = Int32.Parse(ds.Tables[0].Rows[0]["Category"].ToString()); myProduct.ShortTitle = ds.Tables[0].Rows[0]["ShortTitle"].ToString(); myProduct.FullTitle = ds.Tables[0].Rows[0]["FullTitle"].ToString(); myProduct.Price = double.Parse(ds.Tables[0].Rows[0]["Price"].ToString()); myProduct.CurrentQuantity = Int32.Parse(ds.Tables[0].Rows[0]["CurrentQuantity"].ToString()); // return myProduct; }
Sorry for the long snippit. My passed in 'Id' would refer to a unique db key so the dataSet 'should' only ever contain one row. Do I have to go via a dataSet to populate myProduct object? All comments greatly welcomed. tia