Results 1 to 6 of 6

Thread: not inserting, or reading sql rows in order

Threaded View

  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

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