Displaying the data to the front end

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweatha
    New Member
    • Mar 2008
    • 44

    Displaying the data to the front end

    Hi

    I have to display the data from SQL Server table to the frontend textboxes during the page load. For that I have given the coding as,


    Imports System.Data
    Imports System.Data.Sql Client
    Partial Class Default2
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim str, str1 As String
    str = "user id=sa;password= cast;database=j sc;server=AUROR A-SERVER"
    con = New SqlConnection(s tr)
    Try
    con.Open()
    Catch
    End Try
    str1 = "select * from login"
    cmd = New SqlCommand(str1 , con)
    Dim dr1 As SqlDataReader
    dr1 = cmd.ExecuteRead er()
    While (dr1.Read())
    txtLoginId2 = dr1.GetValue(0)
    txtPassword1 = dr1.GetValue(1)
    txtConfirmPassw ord = dr1.GetValue(2)
    txtFirstName = dr1.GetValue(3)
    txtLastName = dr1.GetValue(4)
    txtAddress = dr1.GetValue(5)
    txtAddress2 = dr1.GetValue(6)
    txtAddress3 = dr1.GetValue(7)
    txtCity = dr1.GetValue(8)
    txtCountry = dr1.GetValue(9)
    txtProvince = dr1.GetValue(10 )
    txtPostalCode = dr1.GetValue(11 )
    txtPhone = dr1.GetValue(12 )
    txtEmail1 = dr1.GetValue(13 )
    txtHow = dr1.GetValue(14 )
    End While
    End Sub
    End Class

    But if I run the project it shows the error as

    Unable to cast object of type 'System.String' to type 'System.Web.UI. WebControls.Tex tBox'.

    Invalid CastException was unhandled by user code.
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi sweatha,

    You need to use the Text property of your controls e.g.
    Code:
    txtLoginId2.Text = dr1.GetValue(0)
    Hope this helps,

    Dr B

    PS Please remember to use the code tags (the button with the # on it) to surround your code blocks - it formats them and makes your posts much easier to read. Thanks.

    Comment

    • sweatha
      New Member
      • Mar 2008
      • 44

      #3
      Originally posted by While dr1.Read()
      Hi sweatha,

      You need to use the Text property of your controls e.g.
      Code:
      txtLoginId2.Text = dr1.GetValue(0)
      Hope this helps,

      Dr B

      PS Please remember to use the code tags (the button with the # on it) to surround your code blocks - it formats them and makes your posts much easier to read. Thanks.
      Thankz. Its working now.

      Comment

      Working...