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. Classes

Classes

Scheduled Pinned Locked Moved C#
database
6 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.
  • M Offline
    M Offline
    Mamphekgo
    wrote on last edited by
    #1

    Hi.i want to craete a class that will read data from database and display it on the texboxes.but it can not display it.so i did this try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT Name,Surname FROM Users WHERE UserName =@UserName"; SqlParameter Nam = new SqlParameter(); Nam.ParameterName = "@Name"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value = Name; cmdSearch.Parameters.Add(Nam); SqlParameter Sur = new SqlParameter(); Sur.ParameterName="@Surname"; Sur.Direction = System.Data.ParameterDirection.Output; Sur.Value = Surname; cmdSearch.Parameters.Add(Sur); SqlParameter Use = new SqlParameter("@UserName",System.Data.SqlDbType.VarChar,50,UserName); Use.Value = UserName; cmdSearch.Parameters.Add(Use); SqlParameter Lev = new SqlParameter(); Lev.ParameterName="@Level"; Lev.Direction = System.Data.ParameterDirection.Output; Lev.Value = Surname; cmdSearch.Parameters.Add(Lev); SqlDataReader dr =cmdSearch.ExecuteReader(); if(dr.Read() == true) { MessageBox.Show("Record found"); } else { MessageBox.Show("Record not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close();

    E 1 Reply Last reply
    0
    • M Mamphekgo

      Hi.i want to craete a class that will read data from database and display it on the texboxes.but it can not display it.so i did this try { SqlCommand cmdSearch = new SqlCommand(); cmdSearch.Connection = conn; cmdSearch.CommandText = "SELECT Name,Surname FROM Users WHERE UserName =@UserName"; SqlParameter Nam = new SqlParameter(); Nam.ParameterName = "@Name"; Nam.Direction = System.Data.ParameterDirection.Output; Nam.Value = Name; cmdSearch.Parameters.Add(Nam); SqlParameter Sur = new SqlParameter(); Sur.ParameterName="@Surname"; Sur.Direction = System.Data.ParameterDirection.Output; Sur.Value = Surname; cmdSearch.Parameters.Add(Sur); SqlParameter Use = new SqlParameter("@UserName",System.Data.SqlDbType.VarChar,50,UserName); Use.Value = UserName; cmdSearch.Parameters.Add(Use); SqlParameter Lev = new SqlParameter(); Lev.ParameterName="@Level"; Lev.Direction = System.Data.ParameterDirection.Output; Lev.Value = Surname; cmdSearch.Parameters.Add(Lev); SqlDataReader dr =cmdSearch.ExecuteReader(); if(dr.Read() == true) { MessageBox.Show("Record found"); } else { MessageBox.Show("Record not found"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close();

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      A question ends with a ? BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.

      A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

      J 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        A question ends with a ? BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.

        A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

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

        Ennis Ray Lynch, Jr. wrote:

        BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.

        Personally, I hate this syntax would use if(dr.Read()) - im interested why you think this should "never ever" be done though

        --- How to get answers to your questions[^]

        E 1 Reply Last reply
        0
        • J J4amieC

          Ennis Ray Lynch, Jr. wrote:

          BTW, never, ever, never ever, do if(dr.Read() == true) unless it is just a typo.

          Personally, I hate this syntax would use if(dr.Read()) - im interested why you think this should "never ever" be done though

          --- How to get answers to your questions[^]

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Because you will get flamed by coworkers :) Really it doesn't bother me too mucb but everyone else around here seems to pitch a fit about it. Of course, with a data reader it should be while(dr.Read()) :p as well

          A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

          J 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            Because you will get flamed by coworkers :) Really it doesn't bother me too mucb but everyone else around here seems to pitch a fit about it. Of course, with a data reader it should be while(dr.Read()) :p as well

            A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Ennis Ray Lynch, Jr. wrote:

            it should be while(dr.Read()) :p as well

            Only if you expect the datareader to have more than 1 row. If I wanted to ensure that I only ever read the first row, then if(dr.Read()) would be a necessity!

            --- How to get answers to your questions[^]

            E 1 Reply Last reply
            0
            • J J4amieC

              Ennis Ray Lynch, Jr. wrote:

              it should be while(dr.Read()) :p as well

              Only if you expect the datareader to have more than 1 row. If I wanted to ensure that I only ever read the first row, then if(dr.Read()) would be a necessity!

              --- How to get answers to your questions[^]

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #6

              If you are using if(dr.Read()) to ensure only one row your query is bad.

              A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

              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