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. How to display a column value from database to label

How to display a column value from database to label

Scheduled Pinned Locked Moved C#
databasesysadmintutorial
3 Posts 2 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.
  • M Offline
    M Offline
    Mads115
    wrote on last edited by
    #1

    HI In my windows application i am creating a registration form.. after registration i want to display the register number in Label Cntrl from database(MSSQL2005)... i tried this but not working SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("insert into myTable values ('"+textBox1.Text+"') ", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { label1.Text= dr["id"].ToString(); } dr.Close(); Thankx in advance

    P 1 Reply Last reply
    0
    • M Mads115

      HI In my windows application i am creating a registration form.. after registration i want to display the register number in Label Cntrl from database(MSSQL2005)... i tried this but not working SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("insert into myTable values ('"+textBox1.Text+"') ", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { label1.Text= dr["id"].ToString(); } dr.Close(); Thankx in advance

      P Offline
      P Offline
      Prasanth MS
      wrote on last edited by
      #2

      Problem I traced is 1) The SQL Command is "INSERT" instead of select command. Insert Command insert data into tables and return the number of records inserted. 2) DataReader returns object. You have to convert the same into the data type you want eg. code { object obj = dr.read(); Convert.ToString(obj); } SO YOUR REQUIRED CODE SHALL BE LIKE FOLLOWS ---- SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM myTable", con); SqlDataReader dr = cmd.ExecuteReader(); while (!dr.IsDbNull()) { Object obj = dr.Read(); label1.Text= obj.ToSring(); } dr.Close(); --- try and reply.

      modified on Saturday, May 30, 2009 4:09 AM

      M 1 Reply Last reply
      0
      • P Prasanth MS

        Problem I traced is 1) The SQL Command is "INSERT" instead of select command. Insert Command insert data into tables and return the number of records inserted. 2) DataReader returns object. You have to convert the same into the data type you want eg. code { object obj = dr.read(); Convert.ToString(obj); } SO YOUR REQUIRED CODE SHALL BE LIKE FOLLOWS ---- SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM myTable", con); SqlDataReader dr = cmd.ExecuteReader(); while (!dr.IsDbNull()) { Object obj = dr.Read(); label1.Text= obj.ToSring(); } dr.Close(); --- try and reply.

        modified on Saturday, May 30, 2009 4:09 AM

        M Offline
        M Offline
        Mads115
        wrote on last edited by
        #3

        Thank you Prasanth it works :)

        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