Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO.net

Status
Not open for further replies.

callsupport1

Programmer
Jul 20, 2009
4
GB
Hi can you please help a novice. I am trying to learn ADO.NET and have got this far:-

Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Sage200_DemoData;" _
& "Integrated Security=true"

' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT * from dbo.SLCustomerAccount;"

' Specify the parameter value.
Dim paramValue As Integer = 5

' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)

' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)

i would like to asign a fields contence to a text box but don't know what to do next. I thought it might be something like :-

textbox1.text = Command.field"name".value

please can you help.

Thanks
Stephen

 
Here's one way:

Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Sage200_DemoData;" _
& "Integrated Security=true"

' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT * from dbo.SLCustomerAccount;"

' Specify the parameter value.
Dim paramValue As Integer = 5

' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)

' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
[red]
Dim da As SqlDataAdapter
Dim dt As DataTable

da = New SqlDataAdpater(command)
dt = New DataTable

connection.Open()

da.Fill(dt)

'this gets the value for FieldName from the first row
TextBox1.Text = dt.Rows(0).Item("FieldName")
[/red]

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 

Are you pulling a set of values, one line of data or just one value out of the database? There are different ways to go about each. If you need a whole set you would use a datatable to store the results. One row of data could use a datareader and if you're looking for just one value you could fill the textbox straight from the command with something like
Code:
Textbox1.Text = command.ExecuteScalar()

Also, what is the paramValue variable doing? It looks like you're trying to filter out the results, but the parameter is never joined to the command and the query isn't written to use any parameters. The way it is now you're always going to get back the first row in the SLCustomerAccount table.
 
Thanks for all the help Guys.

What i am trying to do is connect to a data source SQL and a Database "Sage200_DemoData" and a Table "SLCustomerAccout".

I would like to asign some fields from the SLCustomerAccount Table to Text Boxes and then Navigate Forward, Backward, First and Last. Edit the data then Save it back to the Table. I use to be able to do this no probs in vb6 but have not got it sorted in vb2005 yet.

Any help on this would be gratefully recieved.

Regards
Stephen
 
This was a big resource to me when learning ADO .NET, and I still use it as a reference.

Look especially at the "Retrieving and Modifying Data in ADO.NET" section.

Good luck, and post again if you have any more questions.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top