none
developer RRS feed

  • Question

  • 
    
    

    trying to fill text boxes from a sql database.  getting err... value of System.data,datarow cannot be converted to string

      txtdbo.value = RXTemp(PatientDOB
    
    Monday, October 16, 2017 9:39 PM

All replies

  • Hello,

    In VB.NET there is no value property for a TextBox. Here is an example for VB.NET where I create a DataTable, add columns then a row then access the field first name

    Dim dt As New DataTable
    dt.Columns.Add(New DataColumn With {.ColumnName = "FirstName"})
    dt.Columns.Add(New DataColumn With {.ColumnName = "LastName"})
    dt.Rows.Add(New Object() {"Karen", "Payne"})
    TextBox1.Text = dt.Rows(0).Field(Of String)("FirstName")

    Same as if we query the table in the database

    Public Sub AboutData(ByVal pLastName As String)
        Dim dt As New DataTable
        Using cn As New SqlClient.SqlConnection With
            {
                .ConnectionString = "TODO"
            }
            Using cmd As New SqlClient.SqlCommand With
                {
                    .Connection = cn,
                    .CommandText = "SELECT FirstName FROM Customers WHERE LastName = @LastName"
                }
                cmd.Parameters.AddWithValue("@LastName", pLastName)
                cn.Open()
                dt.Load(cmd.ExecuteReader)
            End Using
        End Using
    
        If dt.Rows.Count > 0 Then
            TextBox1.Text = dt.Rows(0).Field(Of String)("FirstName")
        End If
    End Sub
    Of course there are TableAdapters and DataAdapters but the principle is the same. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 16, 2017 11:27 PM
    Moderator
  • Hi HAD,

    If you want to use some data that from sql database to fill textbox, please confirm it is a value of a field in a row, not a entire row.

    Like this:

    https://stackoverflow.com/questions/15874063/display-records-using-textbox-from-sql-database

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, October 25, 2017 6:52 AM
    Tuesday, October 17, 2017 6:30 AM
    Moderator