Results 1 to 11 of 11

Thread: textboxes and fill

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    textboxes and fill

    hi there.

    instead of using a dataset/datalist, is it possible to fill TEXTBOXES instead?

    basically i have a button, this button issues or gets the record ID.

    when user clicks on it, i want it to get other details relating to that record. what i then want it to do is simply get the rest of the details and put them textboxes

    any ideas?

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    One way would be to read from a database and assign the values straight to the text property of the boxes
    VB Code:
    1. txtBox.Text = dataSource("name")
    Though that could get a bit out of hand if you had a lot of textboxes.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    could u explain a bit more please?

    ok so i have the code for the connection...then what?

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    I'm off now but here's an example from an old project you can look at for now.
    VB Code:
    1. Dim cmdSelect As SqlCommand
    2.         Dim dtrReader As SqlDataReader
    3.         'Create SQL command and add parameters
    4.         cmdSelect = New SqlCommand("up_parm_sel_player", objConn)
    5.         cmdSelect.CommandType = CommandType.StoredProcedure
    6.         cmdSelect.Parameters.Add("@player_id", intPlayerID)
    7.         dtrReader = cmdSelect.ExecuteReader(CommandBehavior.SingleRow)
    8.         While dtrReader.Read
    9.             txtEmail.text = dtrReader("email_address")
    10.             txtFirst.text = dtrReader("first_name")
    11.             txtLast.text = dtrReader("Last_name")
    12.         End While

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    hey thanks for that The other thing is, this table uses joins, i have the SQL Statement to do this but exactly where do i put this in the code with the one above?

    do i add it and execute it then do the stuff as above? or what?

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    VB Code:
    1. cmdSelect = New SqlCommand("up_parm_sel_player", objConn)
    "up_parm_sel_player" in this case is a stored procedure and this is where you'd put your SQL string.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks

    ok it seems to do it but whenever there is a DBNULL value it cant assign it to a string (textbox)

    what should i do in this situation?
    Last edited by Techno; Aug 24th, 2004 at 07:56 AM.

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    IF you don't want to use a dataset, datatable, or a datalist that binds to the datareader... then you could just populate an object array.

    VB Code:
    1. dtr = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)
    2. Dim obj(dtr.FieldCount-1) As Object
    3. While dtr.Read
    4.   If dtr("keyColumn") = passedKeyArgument Then
    5.     obj = dtr.GetValues()
    6.     Exit While
    7.   End If
    8. End While
    9. 'close the reader
    10. dtr.Close()
    11.  
    12. 'now obj is an array of values from the datareader
    13. 'you can test for DBNull's easily enough
    14.  
    15. If Not obj(1) is DBNull.Value Then TextBox1.Text = DirectCast(obj(1),String)
    16. 'or it may be test for if Obj(1) Is Nothing, i forget

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    any other better way than an array?

  10. #10
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    It depends on exactly what you're doing and ultimately it's you're choice.

    I think reading a db straight to textboxes and then writing back to the db from the same textboxes is 'ok' for a simple 'Edit details' page but it depends on your requirements.

    As for the null issue when reading from the database you can just use 'If isDbnull(whatever)...' to place blank strings in your textboxes.

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You could use a enumerator to fill a dbrecord, but in reality, it works the same as an array, much like a datarow in a datatable...

    What word do you feel most comfortable with?

    VB Code:
    1. Dim dr As System.Data.SqlClient.SqlDataReader
    2.         dr = sqlcommand1.ExecuteReader(CommandBehavior.CloseConnection)
    3.         Dim dbe As New System.Data.Common.DbEnumerator(dr)
    4.         Dim dbr As System.Data.Common.DbDataRecord
    5.         dbe.MoveNext()
    6.         dr.Read
    7.         dbr = dbe.Current
    8.         If not dbr(0) is dbnull.value then textbox1.text = dbr(0)
    9.         if not dbr("CompanyName") is DbNull.Value then textbox2.text= dbr("CompanyName")
    10.         dr.Close()
    11.  
    12.     End Sub

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