Results 1 to 6 of 6

Thread: not inserting, or reading sql rows in order

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jan 11
    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 04:59 AM. Reason: Added Highlight Tags

  2. #2
    Junior Member
    Join Date
    Jan 11
    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
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,878

    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
    Junior Member
    Join Date
    Jan 11
    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
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 01
    Location
    Adelaide, Australia
    Posts
    9,722

    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 23rd, 2011 at 11:31 PM. Reason: Adding more!
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    DON'T BUMP YOUR POSTS!!! Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  6. #6
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: not inserting, or reading sql rows in order

    Moved To Database Development
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

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