Results 1 to 6 of 6

Thread: not inserting, or reading sql rows in order

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    not inserting, or reading sql rows in order

    i have a button and 2 text boxes to insert data
    i have a button and 2 text boxes to read the data
    it seems that when i insert a row, it is not being inserted as the very next row. or at least it is not reading it in order

    1st. I add rows with the values (2 columns in table) 1 and 1, then 2 and 2, then 3 and 3
    2nd. I read the rows, the do not come up in order 1 and 1, then 2 and 2, then 3 and 3. I will get somethign like 1 and 1, then 3 and 3, then 2 and 2,
    it does read it the same way each time i read it.


    to insert row i...
    vb.net Code:
    1. Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
    2.  
    3.         'code below inserts row
    4.         Dim connetionString As String
    5.         Dim connection As SqlConnection
    6.         Dim adapter As New SqlDataAdapter
    7.         Dim sql As String
    8.         connetionString = "Data Source=.\SQLEXPRESS;Initial Catalog=MemberList;Integrated Security=True"
    9.         connection = New SqlConnection(connetionString)
    10.         Try
    11.             connection.Open()
    12.             sql = "insert into MemberList (MemberName, MemberAge) values('" & txtInsertName.Text & "', '" & txtInsertAge.Text & "')"
    13.             adapter.InsertCommand = New SqlCommand(sql, connection)
    14.             adapter.InsertCommand.ExecuteNonQuery()
    15.             MsgBox("Row inserted !! ")
    16.         Catch ex As Exception
    17.             MsgBox(ex.ToString)
    18.         End Try
    19.     End Sub
    to read the rows (text boxes change before each msgbox) i...
    vb.net Code:
    1. Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
    2.         'code below reads rows and displays data
    3.  
    4.         Dim sqlConn As New SqlConnection("server=.\SQLEXPRESS;Integrated Security=true; Database=memberlist;")
    5.         sqlConn.Open()
    6.         Dim sqlComm As New SqlCommand("SELECT * FROM memberlist", sqlConn)
    7.         Dim dr As SqlDataReader = sqlComm.ExecuteReader()
    8.         While dr.Read
    9.             txtReadName.Text = (dr("MemberName"))
    10.             txtReadAge.Text = (dr("MemberAge"))
    11.             MsgBox("Next")
    12.         End While
    13.         sqlConn.Close()
    14.     End Sub
    Last edited by Hack; Jan 24th, 2011 at 05:59 AM. Reason: Added Highlight Tags

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Re: not inserting, or reading sql rows in order

    I do realize that I am connecting to the database 2 different ways, when its working ill examine how each works and figure out the best method and clean it up a bit.


    thanks very much for your time and help.

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: not inserting, or reading sql rows in order

    A table in a relational database is just a set of records, it doesn't have a concept of the "order" the rows were added. If you want a specific ordering, specify an ORDER BY in your SQL SELECT. Otherwise the DB is free to return the rows in whatever order it feels like (as in, the quickest way it can figure to access the table).

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

    Re: not inserting, or reading sql rows in order

    well then, i thank you for your time. i have learned something.

    maybe you can tell me what the proper way of connecting to the database and sending - retrieving data is.

    thanks

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: not inserting, or reading sql rows in order

    Have a look in the Database Development FAQ.

    Edit:

    Could you please remember to wrap your code in [highlight="VB"][/highlight] tags. Thank you!
    Last edited by Nightwalker83; Jan 24th, 2011 at 12:31 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: not inserting, or reading sql rows in order

    Moved To Database Development

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width