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:
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'code below inserts row
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As New SqlDataAdapter
Dim sql As String
connetionString = "Data Source=.\SQLEXPRESS;Initial Catalog=MemberList;Integrated Security=True"
connection = New SqlConnection(connetionString)
Try
connection.Open()
sql = "insert into MemberList (MemberName, MemberAge) values('" & txtInsertName.Text & "', '" & txtInsertAge.Text & "')"
adapter.InsertCommand = New SqlCommand(sql, connection)
adapter.InsertCommand.ExecuteNonQuery()
MsgBox("Row inserted !! ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
to read the rows (text boxes change before each msgbox) i...
vb.net Code:
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
'code below reads rows and displays data
Dim sqlConn As New SqlConnection("server=.\SQLEXPRESS;Integrated Security=true; Database=memberlist;")
sqlConn.Open()
Dim sqlComm As New SqlCommand("SELECT * FROM memberlist", sqlConn)
Dim dr As SqlDataReader = sqlComm.ExecuteReader()
While dr.Read
txtReadName.Text = (dr("MemberName"))
txtReadAge.Text = (dr("MemberAge"))
MsgBox("Next")
End While
sqlConn.Close()
End Sub
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.
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).
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
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!
Re: not inserting, or reading sql rows in order
Moved To Database Development