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. problem in querying database

problem in querying database

Scheduled Pinned Locked Moved C#
helpdatabaseworkspace
5 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.
  • D Offline
    D Offline
    DownBySpj
    wrote on last edited by
    #1

    hi, i m having problem in reading values from database. something silly i m doing but can't figure it right now. Any help is welcome public void getColumnNames(string col) { string constr = System.Configuration.ConfigurationManager.AppSettings["conskypak"]; String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; SqlConnection cn = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(cmdstr,cn); SqlDataReader dr; cn.Open(); dr = cmd.ExecuteReader(); while (!dr.Read()) { string name = ""; name =(string)dr[0]; } cn.Close(); }

    N J S 3 Replies Last reply
    0
    • D DownBySpj

      hi, i m having problem in reading values from database. something silly i m doing but can't figure it right now. Any help is welcome public void getColumnNames(string col) { string constr = System.Configuration.ConfigurationManager.AppSettings["conskypak"]; String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; SqlConnection cn = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(cmdstr,cn); SqlDataReader dr; cn.Open(); dr = cmd.ExecuteReader(); while (!dr.Read()) { string name = ""; name =(string)dr[0]; } cn.Close(); }

      N Offline
      N Offline
      Nafiseh Salmani
      wrote on last edited by
      #2

      change this: String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; to this : String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col.Trim() + " ' "; it might be because of space which is with string.

      D 1 Reply Last reply
      0
      • D DownBySpj

        hi, i m having problem in reading values from database. something silly i m doing but can't figure it right now. Any help is welcome public void getColumnNames(string col) { string constr = System.Configuration.ConfigurationManager.AppSettings["conskypak"]; String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; SqlConnection cn = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(cmdstr,cn); SqlDataReader dr; cn.Open(); dr = cmd.ExecuteReader(); while (!dr.Read()) { string name = ""; name =(string)dr[0]; } cn.Close(); }

        J Offline
        J Offline
        jeyapandian
        wrote on last edited by
        #3

        try with this while (dr.Read())

        Where there is a will,there is a way.

        1 Reply Last reply
        0
        • N Nafiseh Salmani

          change this: String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; to this : String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col.Trim() + " ' "; it might be because of space which is with string.

          D Offline
          D Offline
          DownBySpj
          wrote on last edited by
          #4

          thanx for all. it did not solved my problem. But u just directed me to think in the right direction. I just deleted all the spaces between after the recordcode and my problem was solved

          1 Reply Last reply
          0
          • D DownBySpj

            hi, i m having problem in reading values from database. something silly i m doing but can't figure it right now. Any help is welcome public void getColumnNames(string col) { string constr = System.Configuration.ConfigurationManager.AppSettings["conskypak"]; String cmdstr = "SELECT columnname FROM TableDescription where RecordCode=' " + col + " ' "; SqlConnection cn = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(cmdstr,cn); SqlDataReader dr; cn.Open(); dr = cmd.ExecuteReader(); while (!dr.Read()) { string name = ""; name =(string)dr[0]; } cn.Close(); }

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            It's not directly related to your problem, but you should make use of parameterized queries instead of building your queries by string concatenation. This way your code is protected against so called injection attacks and in my opinion they also make the code much more readable especially in cases where many parameters are used in one query. Take a look at the following article here at CP: SQL Injection Attacks and Some Tips on How to Prevent Them[^]


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            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