gibbo1715

Sorry all but I cant find the answer to this at the mo

I want to populate a number of textboxes from my database

I know the following will work as I will only have one entry but im sure there is a better way

(Record_ID is my primary key)

thanks

gibbo

Dim MyCommand4 As New Data.SqlClient.SqlCommand("Select * from Roles where Record_ID = '4'", MyConnection)

MyConnection.Open()

MyCommand4.ExecuteNonQuery()

Dim dr As Data.IDataReader

dr = MyCommand4.ExecuteReader

Do While dr.Read()

TextBox1.Text = (dr.Item("Comments"))

Loop

dr.Close()

MyConnection.Close()




Re: Visual Basic Express Edition best way to populate a number of textboxes from my database

js06

I see that noone has answered.

I was just wondering, how do you want to make it better

(visually, less code, does it not do exactly what you want) i was just a little confused by "better"

I probably can't help much with code on this issue since i haven't seen any better examples in my searches but i may have some ideas to use a datagridview and databound boxes. But, it seems like you already have what you want, you are just looking for "something else" i guess






Re: Visual Basic Express Edition best way to populate a number of textboxes from my database

stlarmon

Your code is kind of confusing. Your executing a nonQuery which should be used if you're not returning data, and then you're calling a reader and looping through 1 row.

I would try something like this:

Dim MyCommand4 As New Data.SqlClient.SqlCommand("Select * from Roles where Record_ID = '4'", MyConnection)

MyConnection.Open()

Dim myReader as Data.SqlClient.SqlDataReader = MyCommand4.executeReader

If myReader.Read then

Textbox1.text = myreader("Comments")

End If

myconnection.close

In the myreader If Clause you can add any other textboxes that need to be populated from the query.

Hope this helps






Re: Visual Basic Express Edition best way to populate a number of textboxes from my database

spotty

How about the following a suggestions, two different ways - one using the ADO.NET classes directly and the other using the wizards to generate the controls and typed dataset.

ADO.NET examples

http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/ADOPlusOverview.aspx

Look at "Populate a DataSet from a Database"

This will provide you with a dataset which you can then use databinding with. THis should may populating the textboxes based upon values in the dataset really quite easy.

You can use the wizards to create the controls and set up the binding for you. Reading throuhg this article on setting up simple databinding should help you

http://www.startvbdotnet.com/ado/Simplebinding.aspx

Also, to improve the SQL, set it up as a stored procedure/query in the database which takes a parameter and then provide the command parameter rather than simply creating a SQL text string. As this will yield better performance as the SQL statement would already be pre-compiled and would likely yield better security as you not simply doing string concatenation to generate the SQL statement which opens you up to a potential SQL injection attack (Very bad !!!)