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. setting label.Text by the SqlDataReader's value

setting label.Text by the SqlDataReader's value

Scheduled Pinned Locked Moved C#
sharepointdatabasecomquestion
12 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.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that

    cmd = new SqlCommand("sp_Deneme", conn);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    conn.Open();

            cmd.ExecuteReader();
            dt = new DataTable();
            DataRow row = dt.NewRow();
    
            for (int i = 0; i < reader.FieldCount; i++)
            {
                dt.Columns.Add(i.ToString(), Type.GetType("System.String"));
                dt.Rows\[0\]\[i\] = reader\[i\].ToString();
            }
            label1.Text = dt.Rows\[0\]\[1\].ToString();
            label2.Text = dt.Rows\[0\]\[2\].ToString();
            label3.Text = dt.Rows\[0\]\[3\].ToString();
    

    but there is no change..my way is wrong ?

    vemedya.com

    H P 2 Replies Last reply
    0
    • E Erdinc27

      hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that

      cmd = new SqlCommand("sp_Deneme", conn);
      cmd.CommandType = System.Data.CommandType.StoredProcedure;
      conn.Open();

              cmd.ExecuteReader();
              dt = new DataTable();
              DataRow row = dt.NewRow();
      
              for (int i = 0; i < reader.FieldCount; i++)
              {
                  dt.Columns.Add(i.ToString(), Type.GetType("System.String"));
                  dt.Rows\[0\]\[i\] = reader\[i\].ToString();
              }
              label1.Text = dt.Rows\[0\]\[1\].ToString();
              label2.Text = dt.Rows\[0\]\[2\].ToString();
              label3.Text = dt.Rows\[0\]\[3\].ToString();
      

      but there is no change..my way is wrong ?

      vemedya.com

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

      You are not getting the data. All you have done is set things up ready to get the data. You need to get the reader to do a Read() and in order to do that you will need to have a SqlDataReader member to use in the call. change:

              cmd.ExecuteReader();
      

      to

              SqlDataReader reader = cmd.ExecuteReader();
              reader.Read();
      

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

      1 Reply Last reply
      0
      • E Erdinc27

        hey guys..i have a SqlTable which consists of 3 columns and 1 row..i have 3 labels on my form and try to change these labels' texts with the datas in my database i tried something like that

        cmd = new SqlCommand("sp_Deneme", conn);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        conn.Open();

                cmd.ExecuteReader();
                dt = new DataTable();
                DataRow row = dt.NewRow();
        
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    dt.Columns.Add(i.ToString(), Type.GetType("System.String"));
                    dt.Rows\[0\]\[i\] = reader\[i\].ToString();
                }
                label1.Text = dt.Rows\[0\]\[1\].ToString();
                label2.Text = dt.Rows\[0\]\[2\].ToString();
                label3.Text = dt.Rows\[0\]\[3\].ToString();
        

        but there is no change..my way is wrong ?

        vemedya.com

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        You didn't tell the system which procedure to execute. Why use a DataTable? Just copy the values from the DataReader to the labels. On the other hand, you shouldn't have the data access in the UI layer, but that's another matter.

        modified on Saturday, December 11, 2010 12:52 PM

        E 1 Reply Last reply
        0
        • P PIEBALDconsult

          You didn't tell the system which procedure to execute. Why use a DataTable? Just copy the values from the DataReader to the labels. On the other hand, you shouldn't have the data access in the UI layer, but that's another matter.

          modified on Saturday, December 11, 2010 12:52 PM

          E Offline
          E Offline
          Erdinc27
          wrote on last edited by
          #4

          create Proc [dbo].[sp_Deneme]
          as
          Begin
          select * from Rehber
          End

          hey friend..i use that procedure to get datas and i tried like that and it worked..

                  cmd = new SqlCommand("sp\_Deneme", conn); 
                  cmd.CommandType = System.Data.CommandType.StoredProcedure; 
                  conn.Open();
                  reader = cmd.ExecuteReader();
                  while (reader.Read())
                  {
                      label1.Text = reader.GetInt32(0).ToString();
                      label2.Text = reader.GetString(1);
                      label3.Text = reader.GetString(2);
                  }        
          

          vemedya.com

          P 1 Reply Last reply
          0
          • E Erdinc27

            create Proc [dbo].[sp_Deneme]
            as
            Begin
            select * from Rehber
            End

            hey friend..i use that procedure to get datas and i tried like that and it worked..

                    cmd = new SqlCommand("sp\_Deneme", conn); 
                    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        label1.Text = reader.GetInt32(0).ToString();
                        label2.Text = reader.GetString(1);
                        label3.Text = reader.GetString(2);
                    }        
            

            vemedya.com

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            That's looking better.

            L 1 Reply Last reply
            0
            • P PIEBALDconsult

              That's looking better.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              No comment on SELECT * ? And what about numbered fields as in GetString(1) ? :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              P 1 Reply Last reply
              0
              • L Luc Pattyn

                No comment on SELECT * ? And what about numbered fields as in GetString(1) ? :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Luc Pattyn wrote:

                No comment on SELECT * ?

                I use that all the time.

                Luc Pattyn wrote:

                GetString(1) ?

                Eh, yeah, I never use the Get methods, I generally just cast to the type, but whatever; it's better than it was.

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Luc Pattyn wrote:

                  No comment on SELECT * ?

                  I use that all the time.

                  Luc Pattyn wrote:

                  GetString(1) ?

                  Eh, yeah, I never use the Get methods, I generally just cast to the type, but whatever; it's better than it was.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  if the fields get reordered, the code breaks. which wouldn't happen when the fields were named explicitly: SELECT fieldName1,fieldName2,fieldName3 ... The alternative is more cumbersome: reader.GetString(reader.GetOrdinal("fieldName2")) :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    if the fields get reordered, the code breaks. which wouldn't happen when the fields were named explicitly: SELECT fieldName1,fieldName2,fieldName3 ... The alternative is more cumbersome: reader.GetString(reader.GetOrdinal("fieldName2")) :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    if the fields get reordered

                    Who'd do a silly thing like that? IDataReader fields can be accessed by name too. Item [([(String ])]) Gets the column with the specified name. (Inherited from IDataRecord.) This is from the WCF experiment I was playing with last spring:

                                dr = this.con.ExecuteReader
                                (
                                    @"
                                    SELECT \* 
                                    FROM JunkName 
                                    WHERE Id=@Param0
                                    "
                                ,
                                    ID
                                ) ;
                    
                                if ( dr.Read() )
                                {
                                    result = new Junk.Types.Record()
                                    {
                                        ID              = (System.Guid)              dr \[ "ID" \]
                                    ,
                                        LastName        = (string)                   dr \[ "LastName" \]
                                    ,
                                        FirstName       = (string)                   dr \[ "FirstName" \]
                                    ,
                                        Gender          = (Junk.Types.Gender) (byte) dr \[ "Gender" \]
                                    ,
                                        FavouriteColour = Colour.Parse ( (string)    dr \[ "Colour" \] ) 
                                    ,
                                        Created         = (System.DateTimeOffset)    dr \[ "Created" \]
                                    } ;
                                }
                    
                    L 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Luc Pattyn wrote:

                      if the fields get reordered

                      Who'd do a silly thing like that? IDataReader fields can be accessed by name too. Item [([(String ])]) Gets the column with the specified name. (Inherited from IDataRecord.) This is from the WCF experiment I was playing with last spring:

                                  dr = this.con.ExecuteReader
                                  (
                                      @"
                                      SELECT \* 
                                      FROM JunkName 
                                      WHERE Id=@Param0
                                      "
                                  ,
                                      ID
                                  ) ;
                      
                                  if ( dr.Read() )
                                  {
                                      result = new Junk.Types.Record()
                                      {
                                          ID              = (System.Guid)              dr \[ "ID" \]
                                      ,
                                          LastName        = (string)                   dr \[ "LastName" \]
                                      ,
                                          FirstName       = (string)                   dr \[ "FirstName" \]
                                      ,
                                          Gender          = (Junk.Types.Gender) (byte) dr \[ "Gender" \]
                                      ,
                                          FavouriteColour = Colour.Parse ( (string)    dr \[ "Colour" \] ) 
                                      ,
                                          Created         = (System.DateTimeOffset)    dr \[ "Created" \]
                                      } ;
                                  }
                      
                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      PIEBALDconsult wrote:

                      reordered. Who'd do a silly thing like that?

                      Could happen by accident, after a refactoring; or maybe a convoluted backup/restore operation.

                      PIEBALDconsult wrote:

                      IDataReader fields can be accessed by name too.

                      Sure, that is what you and I would do. All but referring by ordinal. So I did expect your objection to it. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      P 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        PIEBALDconsult wrote:

                        reordered. Who'd do a silly thing like that?

                        Could happen by accident, after a refactoring; or maybe a convoluted backup/restore operation.

                        PIEBALDconsult wrote:

                        IDataReader fields can be accessed by name too.

                        Sure, that is what you and I would do. All but referring by ordinal. So I did expect your objection to it. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Luc Pattyn wrote:

                        I did expect

                        I try to do the unexpected and not the expected -- much like the Spanish Inquisition... oh, but maybe you expected that.

                        L 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Luc Pattyn wrote:

                          I did expect

                          I try to do the unexpected and not the expected -- much like the Spanish Inquisition... oh, but maybe you expected that.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          PIEBALDconsult wrote:

                          maybe you expected

                          you'll never now. :laugh:

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          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