Results 1 to 11 of 11

Thread: [RESOLVED] Bind data record to a text box.

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Resolved [RESOLVED] Bind data record to a text box.

    Ok what is wrong with this code? I am trying to bind a record to a textbox using a Dropdown selected index change. I get the error, any help would be greatly appreciated. if there is a better way to do this I am open to anything at this point as I've spent considerable time on google and other forums looking for a solution. Thanks in advance!

    Conversion failed when converting the nvarchar value 'DrFirstName DrLastName,Ph.D.' to data type int. is it because I concatenate the name and prefix of the DR into the Dropdown list like this,

    Code:
    SELECT ID,EmailAddress, DrName + ',' + PreFix AS DrName FROM DrNames
    Here is the dropdownlist code that I want to populate the textbox with the emailaddress in the table associated with Dr name and ID.
    Code:
    Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
            Dim dt As New DataTable()
    
            If DropDownList1.SelectedItem.Text = "OTHER,..." Then
                txtRx1.Visible = True
                DropDownList1.Visible = False
            End If
            Dim connString As String = ConfigurationManager.ConnectionStrings("TCONNConnectionString").ConnectionString
            Dim con As New SqlConnection(connString)
            Dim cmd As New SqlCommand
    
           Dim strQuery As String = "SELECT ID, EmailAddress, DrName from DrNames WHERE DrName = @DrName"
            cmd.Parameters.AddWithValue("@DrName", DropDownList1.SelectedItem.Value)
    
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            MsgBox(strQuery)
            cmd.Connection = con
            Try
                con.Open()
                Dim sdr As SqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    txtEmailAddress.Text = sdr("EmailAddress").ToString()
                End While
            Catch ex As Exception
                Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
    
        End Sub
    Last edited by Troy Mac; Feb 11th, 2014 at 09:58 PM. Reason: Changed strQuery string
    TMacPherson
    MIS Systems Engineer
    [email protected]


Tags for this Thread

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