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. Web Development
  3. ASP.NET
  4. how to retreave data?

how to retreave data?

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestion
14 Posts 6 Posters 15 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.
  • P Offline
    P Offline
    pandapatin
    wrote on last edited by
    #1

    im trying to retreave an information from a database after the person have logged in.but i can insert the data to a specific place that im intent to put it.Im using session to do that by passing the parameter to campare it. this is the code i used: Dim MyConn As SqlConnection Dim cmdSelect As SqlCommand userName = Session("userName") If userName <> "" Then MyConn = New SqlConnection(ConfigurationManager.AppSettings("psmConnectionString")) cmdSelect = New SqlCommand("sp_GetUser", MyConn) cmdSelect.CommandType = CommandType.StoredProcedure Dim parameternoPekerja As New SqlParameter("@noPekerja", SqlDbType.VarChar) parameternoPekerja.Value = userName cmdSelect.Parameters.Add(parameternoPekerja) MyConn.Open() Dim dr As SqlDataReader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection) If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error Is it wrong?

    E K S H 4 Replies Last reply
    0
    • P pandapatin

      im trying to retreave an information from a database after the person have logged in.but i can insert the data to a specific place that im intent to put it.Im using session to do that by passing the parameter to campare it. this is the code i used: Dim MyConn As SqlConnection Dim cmdSelect As SqlCommand userName = Session("userName") If userName <> "" Then MyConn = New SqlConnection(ConfigurationManager.AppSettings("psmConnectionString")) cmdSelect = New SqlCommand("sp_GetUser", MyConn) cmdSelect.CommandType = CommandType.StoredProcedure Dim parameternoPekerja As New SqlParameter("@noPekerja", SqlDbType.VarChar) parameternoPekerja.Value = userName cmdSelect.Parameters.Add(parameternoPekerja) MyConn.Open() Dim dr As SqlDataReader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection) If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error Is it wrong?

      E Offline
      E Offline
      enjoycrack
      wrote on last edited by
      #2

      Not got ur clue yet :confused: But what problem when the code is executed???? << >>

      P 1 Reply Last reply
      0
      • E enjoycrack

        Not got ur clue yet :confused: But what problem when the code is executed???? << >>

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

        the error is : Invalid attempt to read when no data is present.

        E 1 Reply Last reply
        0
        • P pandapatin

          the error is : Invalid attempt to read when no data is present.

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

          it looks like ur datareader has no rows in there, you have to make sure the datareader has data before retrieving from it just try to debug and see which line causes this problem << >>

          P 1 Reply Last reply
          0
          • E enjoycrack

            it looks like ur datareader has no rows in there, you have to make sure the datareader has data before retrieving from it just try to debug and see which line causes this problem << >>

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

            the line is: txtnama.Text = CStr(dr("nama")). how to make sure that the datareader has row?did't if check it with: If dr.HasRows = False Then ' error mesej Else txtnama.Text = CStr(dr("nama"))

            E 1 Reply Last reply
            0
            • P pandapatin

              the line is: txtnama.Text = CStr(dr("nama")). how to make sure that the datareader has row?did't if check it with: If dr.HasRows = False Then ' error mesej Else txtnama.Text = CStr(dr("nama"))

              E Offline
              E Offline
              enjoycrack
              wrote on last edited by
              #6

              put the break point there, debug and open the quick watch to see the datareader object....And one more thisng, the field name is correct or not? << >>

              P 1 Reply Last reply
              0
              • E enjoycrack

                put the break point there, debug and open the quick watch to see the datareader object....And one more thisng, the field name is correct or not? << >>

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

                the name field is correct.it return nuthing.one thing how should the stored proc look?

                E 1 Reply Last reply
                0
                • P pandapatin

                  the name field is correct.it return nuthing.one thing how should the stored proc look?

                  E Offline
                  E Offline
                  enjoycrack
                  wrote on last edited by
                  #8

                  it's obviously that no data in DB, so before you assing the value retrieved from datareader, you have to check if it's null or not... << >>

                  P 1 Reply Last reply
                  0
                  • E enjoycrack

                    it's obviously that no data in DB, so before you assing the value retrieved from datareader, you have to check if it's null or not... << >>

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

                    there is a data in the db.can u show me the write stored proc?My stored proc look like this : dbo.sp_getUser ( @noPekerja varchar(50) ) AS SELECT lec.* FROM lec WHERE (lec.noPekerja = @noPekerja) Is it rite?

                    E 1 Reply Last reply
                    0
                    • P pandapatin

                      there is a data in the db.can u show me the write stored proc?My stored proc look like this : dbo.sp_getUser ( @noPekerja varchar(50) ) AS SELECT lec.* FROM lec WHERE (lec.noPekerja = @noPekerja) Is it rite?

                      E Offline
                      E Offline
                      enjoycrack
                      wrote on last edited by
                      #10

                      the way you check your store is that you call this store in sql query analyzer and check the returned result is correct or not... << >>

                      1 Reply Last reply
                      0
                      • P pandapatin

                        im trying to retreave an information from a database after the person have logged in.but i can insert the data to a specific place that im intent to put it.Im using session to do that by passing the parameter to campare it. this is the code i used: Dim MyConn As SqlConnection Dim cmdSelect As SqlCommand userName = Session("userName") If userName <> "" Then MyConn = New SqlConnection(ConfigurationManager.AppSettings("psmConnectionString")) cmdSelect = New SqlCommand("sp_GetUser", MyConn) cmdSelect.CommandType = CommandType.StoredProcedure Dim parameternoPekerja As New SqlParameter("@noPekerja", SqlDbType.VarChar) parameternoPekerja.Value = userName cmdSelect.Parameters.Add(parameternoPekerja) MyConn.Open() Dim dr As SqlDataReader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection) If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error Is it wrong?

                        K Offline
                        K Offline
                        Kas_Aspnet
                        wrote on last edited by
                        #11

                        You should not use the CommandBehavior.CloseConnection in your case. Remove this and close the reader explicitly after getting all the data from the reader. Happy programming. Kas_Aspnet

                        I 1 Reply Last reply
                        0
                        • P pandapatin

                          im trying to retreave an information from a database after the person have logged in.but i can insert the data to a specific place that im intent to put it.Im using session to do that by passing the parameter to campare it. this is the code i used: Dim MyConn As SqlConnection Dim cmdSelect As SqlCommand userName = Session("userName") If userName <> "" Then MyConn = New SqlConnection(ConfigurationManager.AppSettings("psmConnectionString")) cmdSelect = New SqlCommand("sp_GetUser", MyConn) cmdSelect.CommandType = CommandType.StoredProcedure Dim parameternoPekerja As New SqlParameter("@noPekerja", SqlDbType.VarChar) parameternoPekerja.Value = userName cmdSelect.Parameters.Add(parameternoPekerja) MyConn.Open() Dim dr As SqlDataReader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection) If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error Is it wrong?

                          S Offline
                          S Offline
                          store_cold
                          wrote on last edited by
                          #12

                          If dr.HasRows = False Then ' error mesej Else 'if there is only one row while dr.read txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr.item("jawatan")) txtjabatan.Text = CInt(dr.item("jabatan")) txtbidang.Text = CInt(dr.item("jabatan")) txtjabatan.Text = CInt(dr.item("jabatan")) end while 'if there is only one row while dr.nextresult txtnama.Text += CInt(dr("nama")) txtjawatan.Text += CInt(dr.item("jawatan")) txtjabatan.Text += CInt(dr.item("jabatan")) txtbidang.Text += CInt(dr.item("jabatan")) txtjabatan.Text += CInt(dr.item("jabatan")) end while End If Else :)

                          1 Reply Last reply
                          0
                          • P pandapatin

                            im trying to retreave an information from a database after the person have logged in.but i can insert the data to a specific place that im intent to put it.Im using session to do that by passing the parameter to campare it. this is the code i used: Dim MyConn As SqlConnection Dim cmdSelect As SqlCommand userName = Session("userName") If userName <> "" Then MyConn = New SqlConnection(ConfigurationManager.AppSettings("psmConnectionString")) cmdSelect = New SqlCommand("sp_GetUser", MyConn) cmdSelect.CommandType = CommandType.StoredProcedure Dim parameternoPekerja As New SqlParameter("@noPekerja", SqlDbType.VarChar) parameternoPekerja.Value = userName cmdSelect.Parameters.Add(parameternoPekerja) MyConn.Open() Dim dr As SqlDataReader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection) If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error Is it wrong?

                            H Offline
                            H Offline
                            Horscht i
                            wrote on last edited by
                            #13

                            If the problem is that the TextBoxes don`t get filled with data (this was my problem few days ago), try: SqlDataReader Reader = null; SqlConn.Open(); Reader = SelectData.ExecuteReader(); while (Reader.Read()) { Txt_ProjectTitle.Text = Convert.ToString(Reader[1]).Trim(); ButtonListProjectType.SelectedValue = Convert.ToString(Reader[2]); DropDownList_ProjectStatus.SelectedValue = Convert.ToString(Reader[3]); [and so on...] } Reader.Close(); SqlConn.Close(); It is C# code though, but should be easy to read. I think you just have to put this while loop around the if/else commands. while (dr.Read()) { If dr.HasRows = False Then ' error mesej Else txtnama.Text = CInt(dr("nama")) txtjawatan.Text = CInt(dr("jawatan")) txtjabatan.Text = CInt(dr("jabatan")) txtbidang.Text = CInt(dr("jabatan")) txtjabatan.Text = CInt(dr("jabatan")) End If Else 'error } -- modified at 7:54 Tuesday 14th March, 2006

                            1 Reply Last reply
                            0
                            • K Kas_Aspnet

                              You should not use the CommandBehavior.CloseConnection in your case. Remove this and close the reader explicitly after getting all the data from the reader. Happy programming. Kas_Aspnet

                              I Offline
                              I Offline
                              Ista
                              wrote on last edited by
                              #14

                              Kas_Aspnet wrote:

                              You should not use the CommandBehavior.CloseConnection in your case

                              Yes he should 1 line of code equals many bugs. So don't write any!!

                              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