Results 1 to 4 of 4

Thread: databind to a textbox

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    databind to a textbox

    howde me again,

    looking to databind to a textbow.
    one column in my table. has anyone done this before ive had a go below but as usual, nothin happens, prob missin a line or 2, i want to do this so the data can be edited and then saved.

    thanks in advance
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            loadtextboxs()
        End Sub
    
    
        Private Sub loadtextboxs()
            'connect to database
    
            'Dim oSQLConn As SqlConnection = New SqlConnection
            oSQLConn.ConnectionString = "Data Source=(local);" & _
            "Initial Catalog=TaT;" & _
            "Integrated Security=SSPI"
            oSQLConn.Open()
    
    
            'databind the infromation
            'If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
    
            daCompMsg.SelectCommand = New SqlCommand("SELECT confirm_msg from tb_comp_msg ", oSQLConn)
            daCompMsg.Fill(dsCompMsg)
    
            'DataGrid()
            ' txt_msg1.DataSource = dsCompMsg.Tables(0)
    
            txt_msg1.DataBind()
            oSQLConn.Close()

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: databind to a textbox

    As TextBoxes can only hold one value they don't have DataSource and DataBind() attributes.

    Try using ExecuteScalar() on the SqlCommand object you create to return just one value and then load it into the TextBox.

    Code:
    Dim cmdSelect As SqlCommand
    
    cmdSelect = New SqlCommand("SELECT confirm_msg from tb_comp_msg ", oSQLConn)
    
    txt_msg1.Text = cmdSelect.ExecuteScalar().ToString()
    Give a shout if you have any problems.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: databind to a textbox

    that code rocks
    i might need it to do more though

    when i get stuck ill post back here and edit my title
    thanks
    it works 60% of the time, all the time.

  4. #4

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: databind to a textbox

    i want now to go a little deeper,

    i want the textbox to display to the current user,
    who has logged in using company code column

    ie display the message where company code = current user


    VB Code:
    1. Dim cmdselect As SqlCommand
    2.         cmdselect = New SqlCommand("SELECT msg_default from tb_comp_detail ", oSQLConn)
    3.         txtDefault.Text = cmdselect.ExecuteScalar().ToString()

    login done on windows forms and msg_default is in the same table as companycode
    it works 60% of the time, all the time.

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