how to display records one after another in a textboxes...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • preethamsaroja
    New Member
    • Jun 2007
    • 6

    how to display records one after another in a textboxes...

    can anyone help in gettin the records one after another from database on a form using vb.net(sql server)..but the below code displays only first record in the textboxes...
    ---------------------------------------
    [code=vbnet]
    private sub next_btn.click( byval,e)
    Dim str As String = "select * from table1"
    'try to write "select distinct * from table1"
    Dim con As String = ConfigurationSe ttings.AppSetti ngs("preeconn")
    Dim cmd As New SqlCommand(str, New SqlConnection(c on))
    cmd.Connection. Open()
    Dim da As New SqlDataAdapter( str, con)
    Dim ds As New DataSet
    da.Fill(ds, "table1")
    Dim maxrws As Integer
    Dim i As Integer
    maxrws = ds.Tables("tabl e1").Rows.Cou nt 'used to get the count of number of rows in a table
    i = 0

    For i = 0 To maxrws - 1
    id_txt.Text = ds.Tables("tabl e1").Rows(i).It em("ID").ToStri ng()
    name_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Name").ToSt ring()
    age_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Age").ToStr ing()
    sex_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Sex").ToStr ing()
    area_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Area").ToSt ring()
    Next i
    'id_txt.Text = ds.Tables("tabl e1").Rows.Add
    'id_txt.Text = ds.Tables("tabl e1").Rows(i).It em("ID").ToStri ng()
    name_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Name").ToSt ring()
    age_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Age").ToStr ing()
    sex_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Sex").ToStr ing()
    area_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Area").ToSt ring()[/code]
  • gomzi
    Contributor
    • Mar 2007
    • 304

    #2
    Originally posted by preethamsaroja
    can anyone help in gettin the records one after another from database on a form using vb.net(sql server)..but the below code displays only first record in the textboxes...
    ---------------------------------------
    private sub next_btn.click( byval,e)
    Dim str As String = "select * from table1"
    'try to write "select distinct * from table1"
    Dim con As String = ConfigurationSe ttings.AppSetti ngs("preeconn")
    Dim cmd As New SqlCommand(str, New SqlConnection(c on))
    cmd.Connection. Open()
    Dim da As New SqlDataAdapter( str, con)
    Dim ds As New DataSet
    da.Fill(ds, "table1")
    Dim maxrws As Integer
    Dim i As Integer
    maxrws = ds.Tables("tabl e1").Rows.Cou nt 'used to get the count of number of rows in a table
    i = 0

    For i = 0 To maxrws - 1
    id_txt.Text = ds.Tables("tabl e1").Rows(i).It em("ID").ToStri ng()
    name_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Name").ToSt ring()
    age_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Age").ToStr ing()
    sex_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Sex").ToStr ing()
    area_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Area").ToSt ring()
    Next i
    'id_txt.Text = ds.Tables("tabl e1").Rows.Add
    'id_txt.Text = ds.Tables("tabl e1").Rows(i).It em("ID").ToStri ng()
    name_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Name").ToSt ring()
    age_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Age").ToStr ing()
    sex_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Sex").ToStr ing()
    area_txt.Text = ds.Tables("tabl e1").Rows(i).It em("Area").ToSt ring()
    Didn't have a deep look at the code, but i guess its because of you using = and not &= or +=.
    You will have to append the text to the one already in the textbox to get the result that you expect.
    If that aint the solution let me know, and i will look properly.

    Regards,
    Gomzi.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by preethamsaroja
      can anyone help in gettin the records one after another from database on a form using vb.net(sql server)..but the below code displays only first record in the textboxes...
      I suggest you use a StringBuilder to build a string with all the records in it for you (StringBuilder. Append(record)) ...and then set the TextBox.Text = StringBuilder.T oString.


      -Frinny

      Comment

      • preethamsaroja
        New Member
        • Jun 2007
        • 6

        #4
        Originally posted by gomzi
        Didn't have a deep look at the code, but i guess its because of you using = and not &= or +=.
        You will have to append the text to the one already in the textbox to get the result that you expect.
        If that aint the solution let me know, and i will look properly.

        Regards,
        Gomzi.
        sir,thank u..
        both of u for the quick reply-but,im not gettin the solution.&= or += doesnt works out..i even tried with while loop in place of forloop& also tried to declare" i" as global variable.but all of that went in vain,,,
        plz help m out asap.....

        Comment

        • gomzi
          Contributor
          • Mar 2007
          • 304

          #5
          Originally posted by preethamsaroja
          can anyone help in gettin the records one after another from database on a form using vb.net(sql server)..but the below code displays only first record in the textboxes...
          ---------------------------------------
          Hi Preethamsaroja. Your code from the for loop part doesn't make sense.
          Here's what your code does,
          loops from 0 to max-1 assigning the values from the dataset to the textboxes.
          But, everytime, you write to the same textbox, and so your data in the textbox is going to get overwritten.
          Also, once the loop gets executed, you store again some data in the textbox.
          So, in short, everytime it gets overwritten and you end up with one record(the last one here) in the textbox.

          Comment

          • preethamsaroja
            New Member
            • Jun 2007
            • 6

            #6
            sir,
            so,sir could u plz tell m-what i have to do(inspite,i declared variable as global& also im using if loop(everytime) ).the code is not working .could u plz tell m what i have to do......
            thank u..,.,

            Comment

            • gomzi
              Contributor
              • Mar 2007
              • 304

              #7
              Originally posted by preethamsaroja
              sir,
              so,sir could u plz tell m-what i have to do(inspite,i declared variable as global& also im using if loop(everytime) ).the code is not working .could u plz tell m what i have to do......
              thank u..,.,

              Ya. no probs. but, can you first tell as to what exactly you want?

              i.e. whether you want to display a record from the database in a form and display consecutive ones when the user presses a button

              OR

              you want to collect all the records from the database and append them to one another and display them in a form

              My guess is that you are trying to achieve the second one. Kindly clarify.

              Anyway....thoug ht will append this.......

              If you wanna achieve the second one,
              Just make these changes and see.

              Code:
              dim id as string =""
              dim name as string =""
              dim age as string =""
              dim sex as string =""
              dim area as string =""
              (Frinny suggested that you go for stringbuilder.. .Got the above idea from that)
              Code:
              For i = 0 To maxrws - 1
              
                id &= ds.Tables("table1").Rows(i).Item("ID").ToString()
              
                name &= ds.Tables("table1").Rows(i).Item("Name").ToString()
              
                age&= ds.Tables("table1").Rows(i).Item("Age").ToString()
              
                sex&= ds.Tables("table1").Rows(i).Item("Sex").ToString()
              
                area &= ds.Tables("table1").Rows(i).Item("Area").ToString()
              
              Next i
              
              id_txt.Text = id
              name_txt.Text = name
              age_txt.Text = age
              sex_txt.Text = sex
              area_txt.Text = area
              Forget the code after the for loop. It ain't necessary in my opinion.

              Comment

              Working...