开发者

Loading data from SQL Server row by row to textboxes

开发者 https://www.devze.com 2023-02-18 23:27 出处:网络
I am trying to retrieve data row by row from my SQL Server and load them into my respective textboxes, I was doing the below code but of course it doesn\'t work as the For Each loop will load every si

I am trying to retrieve data row by row from my SQL Server and load them into my respective textboxes, I was doing the below code but of course it doesn't work as the For Each loop will load every single textboxes with each data retrieved, ran out of ideas. Appreciate if someone can give me a boost here. Thanks.

Private Sub retrieve_Data()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        Try
            con.ConnectionString = "Data Source=HPEnvy-HP; Initial Catalog=Cinema; User Id=<id>; Password=<password>;"
            con.Open()
            cmd.Connection = con

            cmd.CommandText = "SELECT [movie_ID], [movie_Title] FROM [Movie_Table] ORDER BY [MOVIE_ID] "

            Dim lrd As SqlDataReader = cmd.ExecuteReader()

            While lrd.Read()

                Dim reader As String = lrd(1).ToString
                Dim arrLoad As New ArrayList


                arrLoad.Add(lrd(1).ToString)
                For i = 0 To arrLoad.Count - 1
                    For Each cCtrl As Control In Panel1.Controls
                        If TypeOf cCtrl Is TextBox Then
                            Dim txtBox As New TextBox
                            txtBox = cCtrl
                            txtBox.Text = arrLoad.Item(i)
                        End If
                    Next
                Next
    End While

        Catch ex As Exception
            MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
        Finally
开发者_开发问答            con.Close()
        End Try
    End Sub


you just "new" your textbox, you didn't add your textbox to your form. you should add some code like that: this.Controls.Add(textbox); or this.Panel1.Controls.Add(textbox);

0

精彩评论

暂无评论...
验证码 换一张
取 消