-
Mar 14th, 2008, 02:32 PM
#1
Thread Starter
Junior Member
How do you Assign Values to TextBox
Hello Everyone:
I want to grab information from my SQL Database and put that selected item into a textbox so that it can be manipulated later. The problem i seem to have is that I can't select text information. it seems to only work when i try and pull an integer out of the database. Is there a tutorial or any information on this matter.
On the same note, I also was wondering if ExecuteReader() returns values? if so then can I use ExecuteREader() to grab the information and then put it into the textbox?
So basically I Generate the SQL Statement and Connection
Private Sub ActivityChangeDataRetreive(ByVal ActivityName As String)
Dim ActivityConnection As New SqlClient.SqlConnection(connstring)
Dim ActivityCommand As New SqlClient.SqlCommand("Select chrAtvName From wrkActivity Where intActivityID = @ID")
I Assign the Connection to the SQL Command:
ActivityCommand.Connection = ActivityConnection
I then Assign the Parameter a Value (This was after I had it grab the ID that is returned when the user selects a certain activity ...("@ID", g_intActivityID)
ActivityCommand.Parameters.AddWithValue("@ID", 44)
I open, Execute, and close the connection assigning the returned value to a global string variable:
ActivityConnection.Open()
G_ActivityChangeID1 = ActivityCommand.ExecuteNonQuery()
ActivityConnection.Close()
textbox1.Text = G_ActivityChangeID1
End Sub
Should I have executeNonQuery() here or would it be better to have ExecuteReader() here instead, that way I can grab multiple values?
Thanks for viewing guys:
-NUStudent-
-
Mar 14th, 2008, 02:41 PM
#2
Re: How do you Assign Values to TextBox
You can't use ExecuteNonQuery to retrieve data. What you need is to either use ExecuteScaler (which retrieves only the first thing returned, but that may be what you want) or ExecuteReader which activates the DataReader so you can go through the results line-by-line.
In your example, ExecuteScaler may be the best way since you appear or only be retrieving one field from one data record.
-
Mar 14th, 2008, 02:42 PM
#3
Re: How do you Assign Values to TextBox
If you only have one value to pull out, you'll want to use ExecuteScalar instead.
ExecuteNonQuery doesn't return a value, it returns the number of rows that were affected by the command.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Mar 14th, 2008, 02:54 PM
#4
Re: How do you Assign Values to TextBox
You need to read the documentation for the SqlCommand class. ExecuteNonQuery() will return the number of records affected by the query, and that's why you see an integer value in your textbox.
What you need is to declare a SqlDataReader object and SqlCommand.ExecuteReader to get an reader object. You then use this reader to read the data. Something like this
Code:
Dim g_intActivityID As Integer = 44 'You need to set this variable to the user selected value
Dim ActivityConnection As New SqlClient.SqlConnection(connstring)
Dim ActivityCommand As New SqlClient.SqlCommand("Select chrAtvName From wrkActivity Where intActivityID = @ID")
ActivityCommand.Connection = ActivityConnection
ActivityCommand.Parameters.AddWithValue("@ID", g_intActivityID)
ActivityConnection.Open()
Dim reader As SqlClient.SqlDataReader = ActivityCommand.ExecuteReader()
reader.Read()
Dim obj As Object = reader.GetValue(0)
If Not IsDBNull(obj) Then
TextBox1.Text = obj.ToString()
End If
ActivityConnection.Close()
-
Mar 14th, 2008, 02:54 PM
#5
Thread Starter
Junior Member
Re: How do you Assign Values to TextBox
Thanks for the help,
Another question I have then is: If I assign the value to a global variable then will I be able to access that Global Variable later in another form?
And if I use datareader, do I select the values and assign them to the variables as follows?:
Dim ActivityName As String
Dim ActivityDesc As String
ActivityName = reader("chrActivityName")
ActivityDesc = reader("chrActivitydesc")
-
Mar 14th, 2008, 03:30 PM
#6
Re: How do you Assign Values to TextBox
1) Yes, but it's better to make a property on the second form and pass the variable to it that way. Truly global variables sometimes get cleaned up if not used for a while and disposed in my experience.
If you have a lot of variables that you want accessible from many forms, I'd put all those variables in a class. Make an instance of the class on program start and pass that class around to all my forms.
2) Yea, similar. If the field names of your table are: "chrActivityName" and "chrActivitydesc" then you can do:
reader.Read()
ActivityName = CStr(reader.Item("chrActivityName"))
ActivityDesc = CStr(reader.Item("chrActivitydesc"))
That will return those fields on the very first row the datareader retrieves.
-
Mar 14th, 2008, 05:50 PM
#7
Re: How do you Assign Values to TextBox
A public member of a module would make an effective global. Of course, since a module is just a static class with static members, it's a form of what Jenner suggested.
My usual boring signature: Nothing
-
Mar 14th, 2008, 06:19 PM
#8
Thread Starter
Junior Member
Re: How do you Assign Values to TextBox
Okay on the same note, If i select values that have the same primary key and I want to be able to select each individual selection that is related to that particular primary key, how do I select them?
EX.
In the table the values would read like this after a select statement of : "Select * from wrktblActivitySponsor Where intActivityID = 147"
intActivityID l intSponsorID
147 l 3
147 l 6
147 l 8
________________________
How do i select the 3, the 6, and the 8 from that table and be able to manipulate them individually? Would it be the same way as values 3 starting at 1, and the 6 at 3, and the 8 at 5 (being the compiler reads values returned starting at 0)?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|