Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Advice

Advice

Scheduled Pinned Locked Moved C#
databasequestionlounge
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gadgetfbi
    wrote on last edited by
    #1

    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

    G B L 3 Replies Last reply
    0
    • G gadgetfbi

      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

      G Offline
      G Offline
      Guillermo Rivero
      wrote on last edited by
      #2

      You can use a DataTable instead of a DataSet. The DataAdapter.Fill() method works with a DataTable object. :) Free your mind...

      1 Reply Last reply
      0
      • G gadgetfbi

        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

        B Offline
        B Offline
        Bo Hunter
        wrote on last edited by
        #3

        Try a DataReader. Thank You Bo Hunter

        1 Reply Last reply
        0
        • G gadgetfbi

          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

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          oR better, create a typed DataSet. leppie::AllocCPArticle("Zee blog");
          Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups