Rogue Lotus 4
Rogue Lotus 4

Reputation: 39

How do I make DataBindings update in runtime?

Noob programmer here, go easy on me if the answer was straight up obvious. Couldn't really find the right title but it's the closest one I got.

To get straight to the point, I have a textbox in my program, and whatever I type in the textbox should go straight to a table in my database, and then when I click a specific button, whatever I last put in the table should show up on the label beside the textbox via DataBindings (at least, that's how I want it to go).

It works for the most part. When I type something in and click my button, the text shows up on the label. But when I do it a second time , the label doesn't update to what I last typed (it only updates when I stop and start running the program again.)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    connection.Open()

    Dim test1 As String = "SELECT TOP 1 Letters From NewTable ORDER BY Id DESC"

    ExecuteQuery(test1)

    Dim command As New SqlCommand(test1, connection)

    Dim adapter As New SqlDataAdapter(test1, connection)

    adapter.Fill(TestDatabaseAgainDataSet1, "NewTable")
    randomlabel.DataBindings.Clear()
    randomlabel.DataBindings.Add("Text", TestDatabaseAgainDataSet1.NewTable, "Letters")

    command.ExecuteNonQuery()

    connection.Close()
End Sub

I know there are some easier ways for user-generated text to show up on a label, but I kinda need it so the last thing I typed is still there when I click the button.

Upvotes: 0

Views: 73

Answers (1)

tezzo
tezzo

Reputation: 11115

If I understand correctly, you only have problem on the second part of your project: whatever you last put in the table should show up on the label beside the textbox.

If you are on SQL Server you can use SqlDataReader class that provides a way of reading a forward-only stream of rows from a SQL Server database.

Dim cmdLastLetter As SqlCommand = conn.CreateCommand()

cmdLastLetter.CommandText = "SELECT TOP 1 Letters From NewTable ORDER BY Id DESC"

Dim drLastLetter As SqlDataReader = cmdLastLetter.ExecuteReader()

If drLastLetter.Read() Then
    Me.Label1.Text = drLastLetter("Letter")
End If

drLastLetter.Close()
cmdLastLetter.Dispose()

If you are using another database consult this document: you have to use another class but the concept is the same.

Upvotes: 1

Related Questions