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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. retreiving values from datatable

retreiving values from datatable

Scheduled Pinned Locked Moved C#
databasecsharpsalestutorial
4 Posts 3 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.
  • R Offline
    R Offline
    Rabia_Arif
    wrote on last edited by
    #1

    Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:

    dtTableRecords_out = null;
    string strQueryString = null;
    SqlCommand objSqlCommand = null;
    SqlConnection objSqlConnection = null;
    DataSet objDataSet = null;
    SqlDataAdapter objDataAdapter = null;
    SqlDataReader objDataReader = null;
    string day = (DateTime.Now.AddDays(1).Day.ToString());
    string month = (DateTime.Now.Month.ToString());
    string year = (DateTime.Now.Year.ToString());
    string date_nextDay = month + '/' + day + '/' + year;

            try
            {
                objDataSet = new DataSet();
                objDataAdapter = new SqlDataAdapter();
                objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString());
                strQueryString = "select \* from view\_reservation where  date = " + date\_nextDay;
                objSqlCommand = new SqlCommand(strQueryString, objSqlConnection);
    
                objDataAdapter.SelectCommand = objSqlCommand;
                objSqlConnection.Open();
                objDataSet.Clear();
    
                objDataAdapter.Fill(objDataSet);
                objSqlConnection.Close();
                dtTableRecords\_out = objDataSet.Tables\[0\];
    
               if (strQueryString != null)
                {
                    Console.WriteLine("Service is reserved for tommorow");
                }
                else
                {
                    Console.WriteLine("no reervation");
    
                }
    
            }
    
            finally
            {
    
                if (objSqlConnection.State == ConnectionState.Open)
                {
                    objSqlConnection.Close();
                }
    
            }
            return strQueryString;
    

    }

    but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block

    H P 2 Replies Last reply
    0
    • R Rabia_Arif

      Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:

      dtTableRecords_out = null;
      string strQueryString = null;
      SqlCommand objSqlCommand = null;
      SqlConnection objSqlConnection = null;
      DataSet objDataSet = null;
      SqlDataAdapter objDataAdapter = null;
      SqlDataReader objDataReader = null;
      string day = (DateTime.Now.AddDays(1).Day.ToString());
      string month = (DateTime.Now.Month.ToString());
      string year = (DateTime.Now.Year.ToString());
      string date_nextDay = month + '/' + day + '/' + year;

              try
              {
                  objDataSet = new DataSet();
                  objDataAdapter = new SqlDataAdapter();
                  objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString());
                  strQueryString = "select \* from view\_reservation where  date = " + date\_nextDay;
                  objSqlCommand = new SqlCommand(strQueryString, objSqlConnection);
      
                  objDataAdapter.SelectCommand = objSqlCommand;
                  objSqlConnection.Open();
                  objDataSet.Clear();
      
                  objDataAdapter.Fill(objDataSet);
                  objSqlConnection.Close();
                  dtTableRecords\_out = objDataSet.Tables\[0\];
      
                 if (strQueryString != null)
                  {
                      Console.WriteLine("Service is reserved for tommorow");
                  }
                  else
                  {
                      Console.WriteLine("no reervation");
      
                  }
      
              }
      
              finally
              {
      
                  if (objSqlConnection.State == ConnectionState.Open)
                  {
                      objSqlConnection.Close();
                  }
      
              }
              return strQueryString;
      

      }

      but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I think that you have made an error in your if condition. How can strQueryString be null? Just a few lines above you set it to "select * from view_reservation where date = " + date_nextDay;, and you never reset it! From a very quick look at your code I suspect that what you need is something like:

      if (dtTableRecords\_out.Rows.Count > 0)
      {
          Console.WriteLine("Service is reserved for tommorow");
      }
      else
      {
          Console.WriteLine("no reervation");
      }
      

      Two other things that might make your code better/more secure. Please research the using construct, this will enable you to do things like:

      using (objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString()))
      {
          strQueryString = "select \* from view\_reservation where  date = " + date\_nextDay;
          objSqlCommand = new SqlCommand(strQueryString, objSqlConnection);
      
          objDataAdapter.SelectCommand = objSqlCommand;
          objSqlConnection.Open();
          objDataSet.Clear();
      
          objDataAdapter.Fill(objDataSet);
          objSqlConnection.Close();    //<=============== delete this line, no longer required
      }
      

      Using using ensures that objSqlConnection gets closed and disposed, guaranteed, regardless of exceptions or any other problem short of a power-cut. Also research using parameterized queries Building SQL statements the way you are currently doing, is prone to all sorts of malicious exploits. Good Luck! :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      R 1 Reply Last reply
      0
      • R Rabia_Arif

        Hello. I m using C# and sql server2005 I want to retrieve values from Datatable but dt kno how to do this. My code is as follows,it checks from view(since it is the combination of specific columns from 2 tables)that whether the a particular service is reserved for the next day or not:

        dtTableRecords_out = null;
        string strQueryString = null;
        SqlCommand objSqlCommand = null;
        SqlConnection objSqlConnection = null;
        DataSet objDataSet = null;
        SqlDataAdapter objDataAdapter = null;
        SqlDataReader objDataReader = null;
        string day = (DateTime.Now.AddDays(1).Day.ToString());
        string month = (DateTime.Now.Month.ToString());
        string year = (DateTime.Now.Year.ToString());
        string date_nextDay = month + '/' + day + '/' + year;

                try
                {
                    objDataSet = new DataSet();
                    objDataAdapter = new SqlDataAdapter();
                    objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString());
                    strQueryString = "select \* from view\_reservation where  date = " + date\_nextDay;
                    objSqlCommand = new SqlCommand(strQueryString, objSqlConnection);
        
                    objDataAdapter.SelectCommand = objSqlCommand;
                    objSqlConnection.Open();
                    objDataSet.Clear();
        
                    objDataAdapter.Fill(objDataSet);
                    objSqlConnection.Close();
                    dtTableRecords\_out = objDataSet.Tables\[0\];
        
                   if (strQueryString != null)
                    {
                        Console.WriteLine("Service is reserved for tommorow");
                    }
                    else
                    {
                        Console.WriteLine("no reervation");
        
                    }
        
                }
        
                finally
                {
        
                    if (objSqlConnection.State == ConnectionState.Open)
                    {
                        objSqlConnection.Close();
                    }
        
                }
                return strQueryString;
        

        }

        but the if statement is not executiong correctly. I want the result(eg service_name or customer_id) from the query for further processing. Hope that u ppl will provide me with an answer.I am sorry i forgot to put my code inside code block

        P Offline
        P Offline
        Patrik karlin
        wrote on last edited by
        #3

        1. if your using .net 3.5 then use linq to sql here -> http://www.asp.net/learn/linq-videos/ 2. why are you checking wherewer the strQueryString is null or nott why whuld that mather your outputting to dtTableRecords_out the first element of objDataSet.Tables[0] isent that what your going for.

        1 Reply Last reply
        0
        • H Henry Minute

          I think that you have made an error in your if condition. How can strQueryString be null? Just a few lines above you set it to "select * from view_reservation where date = " + date_nextDay;, and you never reset it! From a very quick look at your code I suspect that what you need is something like:

          if (dtTableRecords\_out.Rows.Count > 0)
          {
              Console.WriteLine("Service is reserved for tommorow");
          }
          else
          {
              Console.WriteLine("no reervation");
          }
          

          Two other things that might make your code better/more secure. Please research the using construct, this will enable you to do things like:

          using (objSqlConnection = new SqlConnection(ConnectionString.GetSQLConnectionString()))
          {
              strQueryString = "select \* from view\_reservation where  date = " + date\_nextDay;
              objSqlCommand = new SqlCommand(strQueryString, objSqlConnection);
          
              objDataAdapter.SelectCommand = objSqlCommand;
              objSqlConnection.Open();
              objDataSet.Clear();
          
              objDataAdapter.Fill(objDataSet);
              objSqlConnection.Close();    //<=============== delete this line, no longer required
          }
          

          Using using ensures that objSqlConnection gets closed and disposed, guaranteed, regardless of exceptions or any other problem short of a power-cut. Also research using parameterized queries Building SQL statements the way you are currently doing, is prone to all sorts of malicious exploits. Good Luck! :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          R Offline
          R Offline
          Rabia_Arif
          wrote on last edited by
          #4

          thankyou people 4 ur interest Yes i usually use parameter classes for sql queries.I will use that here after the correct operation of my code. I have also tried the code of what u suggested but still output is incorerct.Although whenever i execute the query in sql it returns me some rows. I have also tried with SqlDataReader but DataReader remains null:(

          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